Class: Spree::PaymentMethod

Inherits:
Base
  • Object
show all
Includes:
Spree::Preferences::Persistable, Spree::Preferences::StaticallyConfigurable, SoftDeletable
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, UnsupportedPaymentMethod

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Spree::Preferences::StaticallyConfigurable

#preference_source=, #preferences, #preferences=

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Class Method Details

.find_sti_class(type_name) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'app/models/spree/payment_method.rb', line 67

def find_sti_class(type_name)
  super(type_name)
rescue ActiveRecord::SubclassNotFound
  raise UnsupportedPaymentMethod, "Found invalid payment type '#{type_name}'.\n"\
    "This may happen after switching payment service provider, when payment methods "\
    "reference old types that are not supported any more.\n"\
    "If that is the case, consider running 'rake payment_method:deactivate_unsupported_payment_methods' "\
    "to fix the issue."
end

.model_nameObject



63
64
65
# File 'app/models/spree/payment_method.rb', line 63

def model_name
  ModelName.new(self, Spree)
end

Instance Method Details

#auto_capture?Boolean

Returns:

  • (Boolean)


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

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

#gatewayObject

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


90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/spree/payment_method.rb', line 90

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

#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



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

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


140
141
142
# File 'app/models/spree/payment_method.rb', line 140

def partial_name
  type.demodulize.underscore
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


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

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)


117
118
119
# File 'app/models/spree/payment_method.rb', line 117

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



154
155
156
# File 'app/models/spree/payment_method.rb', line 154

def reusable_sources(_order)
  []
end

#source_required?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'app/models/spree/payment_method.rb', line 148

def source_required?
  true
end

#store_credit?Boolean

Returns:

  • (Boolean)


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

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:



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

def supports?(_source)
  true
end

#try_void(payment) ⇒ ActiveMerchant::Billing::Response|FalseClass

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.

This default implementation will void the payment if void succeeds, otherwise it returns false.

Parameters:

Returns:

  • (ActiveMerchant::Billing::Response|FalseClass)


184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/spree/payment_method.rb', line 184

def try_void(payment)
  void_attempt = if payment.payment_method.payment_profiles_supported?
    void(payment.transaction_id, payment.source, { originator: payment })
  else
    void(payment.transaction_id, { originator: payment })
  end

  return void_attempt if void_attempt.success?

  false
end