Class: Spree::PaymentMethod

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

Overview

A base class which is used for implementing payment methods.

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Spree::Preferences::StaticallyConfigurable

#preference_source=, #preferences, #preferences=

Methods included from ParanoiaDeprecations

#paranoia_delete, #paranoia_destroy

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Spree::Preferences::Preferable

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

Class Method Details

.active?Boolean

Deprecated.

Use .active.any? instead

Returns:

  • (Boolean)


106
107
108
109
# File 'app/models/spree/payment_method.rb', line 106

def active?
  Spree::Deprecation.warn "#{self}.active? is deprecated. Use #{self}.active.any? instead"
  where(type: to_s, active: true).count > 0
end

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

Deprecated.

Use active, available_to_users, and available_to_admin scopes instead.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/spree/payment_method.rb', line 79

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 |payment|
    store.nil? || store.payment_methods.empty? || store.payment_methods.include?(payment)
  end
end

.const_missing(name) ⇒ Object



27
28
29
30
31
32
33
# File 'app/models/spree/payment_method.rb', line 27

def self.const_missing(name)
  if name == :DISPLAY
    const_set(:DISPLAY, [:both, :front_end, :back_end])
  else
    super
  end
end

.find_with_destroyed(*args) ⇒ Object

Deprecated.

Use .with_deleted.find instead



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

def find_with_destroyed(*args)
  Spree::Deprecation.warn "#{self}.find_with_destroyed is deprecated. Use #{self}.with_deleted.find instead"
  unscoped { find(*args) }
end

.model_nameObject



101
102
103
# File 'app/models/spree/payment_method.rb', line 101

def model_name
  ModelName.new(self, Spree)
end

.providersObject

Deprecated.

Use Spree::Config.environment.payment_methods instead



72
73
74
75
76
# File 'app/models/spree/payment_method.rb', line 72

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'
  Spree::Config.environment.payment_methods
end

Instance Method Details

#auto_capture?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'app/models/spree/payment_method.rb', line 237

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



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

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

#display_onObject

Deprecated.

Use #available_to_users and #available_to_admin instead



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/models/spree/payment_method.rb', line 172

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



164
165
166
167
168
169
# File 'app/models/spree/payment_method.rb', line 164

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


130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/spree/payment_method.rb', line 130

def gateway
  gateway_options = options
  gateway_options.delete :login if gateway_options.key?(:login) && gateway_options[:login].nil?

  # All environments except production considered to be test
  test_server = gateway_options[:server] != 'production'
  test_mode = gateway_options[:test_mode]

  gateway_options[:test] = (test_server || test_mode)

  @gateway ||= gateway_class.new(gateway_options)
end

#method_typeObject



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

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



150
151
152
# File 'app/models/spree/payment_method.rb', line 150

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

5. app/views/spree/api/payments/source_views/_{partial_name}.json.jbuilder
The view that represents your payment method on orders through the api


205
206
207
# File 'app/models/spree/payment_method.rb', line 205

def partial_name
  deprecated_method_type_override || type.demodulize.underscore
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'app/models/spree/payment_method.rb', line 223

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)


159
160
161
# File 'app/models/spree/payment_method.rb', line 159

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

#reusable_sources(_order) ⇒ Object

Custom gateways can redefine this method to return reusable sources for an order. See Spree::PaymentMethod::CreditCard#reusable_sources as an example



233
234
235
# File 'app/models/spree/payment_method.rb', line 233

def reusable_sources(_order)
  []
end

#source_required?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'app/models/spree/payment_method.rb', line 227

def source_required?
  true
end

#store_credit?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'app/models/spree/payment_method.rb', line 269

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:



246
247
248
# File 'app/models/spree/payment_method.rb', line 246

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)


261
262
263
264
265
266
267
# File 'app/models/spree/payment_method.rb', line 261

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