Class: Spree::PaymentMethod

Inherits:
Base
  • Object
show all
Includes:
Spree::Preferences::StaticallyConfigurable
Defined in:
app/models/spree/payment_method.rb

Overview

A base class which is used for implementing payment methods.

See github.com/solidusio/solidus_gateway/ for offically supported payment method implementations.

Uses STI (single table inheritance) to store all implemented payment methods in one table (spree_payment_methods).

This class is not meant to be instantiated. Please create instances of concrete payment methods.

Direct Known Subclasses

BillingIntegration, Check, CreditCard, StoreCredit

Defined Under Namespace

Classes: BogusCreditCard, Check, CreditCard, ModelName, SimpleBogusCreditCard, StoreCredit

Constant Summary

DISPLAY =
[:both, :front_end, :back_end]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Spree::Preferences::StaticallyConfigurable

#preference_source=, #preferences, #preferences=

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Spree::Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Class Method Details

.active?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/models/spree/payment_method.rb', line 90

def active?
  where(type: to_s, active: true).count > 0
end

.available(display_on = nil, store: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/spree/payment_method.rb', line 64

def available(display_on = nil, store: nil)
  Spree::Deprecation.warn "Spree::PaymentMethod.available is deprecated."\
    "Please use .active, .available_to_users, and .available_to_admin scopes instead."\
    "For payment methods associated with a specific store, use Spree::PaymentMethod.available_to_store(your_store)"\
    " as the base applying any further filtering"

  display_on = display_on.to_s

  available_payment_methods =
    case display_on
    when 'front_end'
      active.available_to_users
    when 'back_end'
      active.available_to_admin
    else
      active.available_to_users.available_to_admin
    end
  available_payment_methods.select do |p|
    store.nil? || store.payment_methods.empty? || store.payment_methods.include?(p)
  end
end

.find_with_destroyed(*args) ⇒ Object



94
95
96
# File 'app/models/spree/payment_method.rb', line 94

def find_with_destroyed(*args)
  unscoped { find(*args) }
end

.model_nameObject



86
87
88
# File 'app/models/spree/payment_method.rb', line 86

def model_name
  ModelName.new(self, Spree)
end

.providersObject



58
59
60
61
62
# File 'app/models/spree/payment_method.rb', line 58

def providers
  Spree::Deprecation.warn 'Spree::PaymentMethod.providers is deprecated and will be deleted in Solidus 3.0. ' \
    'Please use Rails.application.config.spree.payment_methods instead'
  Rails.application.config.spree.payment_methods
end

Instance Method Details

#auto_capture?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'app/models/spree/payment_method.rb', line 211

def auto_capture?
  auto_capture.nil? ? Spree::Config[:auto_capture] : auto_capture
end

#deprecated_method_type_overrideObject

:nodoc: If method_type has been overridden, call it and return the value, otherwise return nil



185
186
187
188
189
190
# File 'app/models/spree/payment_method.rb', line 185

def deprecated_method_type_override
  if method(:method_type).owner != Spree::PaymentMethod
    Spree::Deprecation.warn "overriding PaymentMethod#method_type is deprecated and will be removed from Solidus 3.0 (override partial_name instead)", caller
    method_type
  end
end

#display_onObject

Deprecated.

Use #available_to_users and #available_to_admin instead



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/spree/payment_method.rb', line 149

def display_on
  Spree::Deprecation.warn "Spree::PaymentMethod#display_on is deprecated."\
    "Please use #available_to_users and #available_to_admin instead."
  if available_to_users? && available_to_admin?
    ''
  elsif available_to_users?
    'front_end'
  elsif available_to_admin?
    'back_end'
  else
    'none'
  end
end

#display_on=(value) ⇒ Object

Deprecated.

Use #available_to_users= and #available_to_admin= instead



141
142
143
144
145
146
# File 'app/models/spree/payment_method.rb', line 141

def display_on=(value)
  Spree::Deprecation.warn "Spree::PaymentMethod#display_on= is deprecated."\
    "Please use #available_to_users= and #available_to_admin= instead."
  self.available_to_users = value.blank? || value == 'front_end'
  self.available_to_admin = value.blank? || value == 'back_end'
end

#gatewayObject Also known as: provider

Represents the gateway of this payment method

The gateway is responsible for communicating with the providers API.

It implements methods for:

- authorize
- purchase
- capture
- void
- credit


111
112
113
114
115
116
117
118
# File 'app/models/spree/payment_method.rb', line 111

def gateway
  gateway_options = options
  gateway_options.delete :login if gateway_options.key?(:login) && gateway_options[:login].nil?
  if gateway_options[:server]
    ActiveMerchant::Billing::Base.mode = gateway_options[:server].to_sym
  end
  @gateway ||= gateway_class.new(gateway_options)
end

#method_typeObject



192
193
194
195
# File 'app/models/spree/payment_method.rb', line 192

def method_type
  Spree::Deprecation.warn "method_type is deprecated and will be removed from Solidus 3.0 (use partial_name instead)", caller
  partial_name
end

#optionsObject

Represents all preferences as a Hash

Each preference is a key holding the value(s) and gets passed to the gateway via gateway_options

Returns:

  • Hash



127
128
129
# File 'app/models/spree/payment_method.rb', line 127

def options
  preferences.to_hash
end

#partial_nameObject

Used as partial name for your payment method

Currently your payment method needs to provide these partials:

1. app/views/spree/checkout/payment/_{partial_name}.html.erb
The form your customer enters the payment information in during checkout

2. app/views/spree/checkout/existing_payment/_{partial_name}.html.erb
The payment information of your customers reusable sources during checkout

3. app/views/spree/admin/payments/source_forms/_{partial_name}.html.erb
The form an admin enters payment information in when creating orders in the backend

4. app/views/spree/admin/payments/source_views/_{partial_name}.html.erb
The view that represents your payment method on orders in the backend


179
180
181
# File 'app/models/spree/payment_method.rb', line 179

def partial_name
  deprecated_method_type_override || type.demodulize.downcase
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'app/models/spree/payment_method.rb', line 197

def payment_profiles_supported?
  false
end

#payment_source_classObject

The class that will store payment sources (re)usable with this payment method

Used by Spree::Payment as source (e.g. Spree::CreditCard in the case of a credit card payment method).

Returning nil means the payment method doesn't support storing sources (e.g. Spree::PaymentMethod::Check)

Raises:

  • (::NotImplementedError)


136
137
138
# File 'app/models/spree/payment_method.rb', line 136

def payment_source_class
  raise ::NotImplementedError, "You must implement payment_source_class method for #{self.class}."
end

#reusable_sources(_order) ⇒ Object

Custom gateways should redefine this method. See Gateway implementation as an example



207
208
209
# File 'app/models/spree/payment_method.rb', line 207

def reusable_sources(_order)
  []
end

#source_required?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'app/models/spree/payment_method.rb', line 201

def source_required?
  true
end

#store_credit?Boolean

Returns:

  • (Boolean)


243
244
245
# File 'app/models/spree/payment_method.rb', line 243

def store_credit?
  is_a? Spree::PaymentMethod::StoreCredit
end

#supports?(_source) ⇒ Boolean

Check if given source is supported by this payment method

Please implement validation logic in your payment method implementation

Returns:

  • (Boolean)

See Also:



220
221
222
# File 'app/models/spree/payment_method.rb', line 220

def supports?(_source)
  true
end

#try_void(_payment) ⇒ ActiveMerchant::Billing::Response, false

Used by Spree::Payment#cancel!

Implement `try_void` on your payment method implementation to handle void attempts. In that method return a ActiveMerchant::Billing::Response object if the void succeeds. Return false or nil if the void is not possible anymore - because it was already processed by the bank. Solidus will refund the amount of the payment in this case.

Returns:

  • (ActiveMerchant::Billing::Response)

    with true if the void succeeded

  • (ActiveMerchant::Billing::Response)

    with false if the void failed

  • (false)

    if it can't be voided at this time

Raises:

  • (::NotImplementedError)


235
236
237
238
239
240
241
# File 'app/models/spree/payment_method.rb', line 235

def try_void(_payment)
  raise ::NotImplementedError,
    "You need to implement `try_void` for #{self.class.name}. In that " \
    'return a ActiveMerchant::Billing::Response object if the void succeeds '\
    'or `false|nil` if the void is not possible anymore. ' \
    'Solidus will refund the amount of the payment then.'
end