Class: Spree::PaymentMethod
- 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
Defined Under Namespace
Classes: BogusCreditCard, Check, CreditCard, ModelName, SimpleBogusCreditCard, StoreCredit, UnsupportedPaymentMethod
Class Method Summary collapse
Instance Method Summary collapse
- #auto_capture? ⇒ Boolean
-
#gateway ⇒ Object
Represents the gateway of this payment method.
-
#options ⇒ Object
Represents all preferences as a Hash.
-
#partial_name ⇒ Object
Used as partial name for your payment method.
- #payment_profiles_supported? ⇒ Boolean
-
#payment_source_class ⇒ Object
The class that will store payment sources (re)usable with this payment method.
-
#reusable_sources(_order) ⇒ Object
Custom gateways can redefine this method to return reusable sources for an order.
- #source_required? ⇒ Boolean
- #store_credit? ⇒ Boolean
-
#supports?(_source) ⇒ Boolean
Check if given source is supported by this payment method.
-
#try_void(payment) ⇒ ActiveMerchant::Billing::Response|FalseClass
Used by Spree::Payment#cancel!.
Methods included from Spree::Preferences::StaticallyConfigurable
#preference_source=, #preferences, #preferences=
Methods inherited from Base
Methods included from Core::Permalinks
#generate_permalink, #save_permalink
Class Method Details
.find_sti_class(type_name) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'app/models/spree/payment_method.rb', line 65 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:deprecate_unsupported_payment_methods' "\ "to fix the issue." end |
Instance Method Details
#auto_capture? ⇒ Boolean
156 157 158 |
# File 'app/models/spree/payment_method.rb', line 156 def auto_capture? auto_capture.nil? ? Spree::Config[:auto_capture] : auto_capture end |
#gateway ⇒ Object
Represents the gateway of this payment method
The gateway is responsible for communicating with the providers API.
It implements methods for:
-
- purchase
- capture
- void
- credit
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/models/spree/payment_method.rb', line 88 def gateway = .delete :login if .key?(:login) && [:login].nil? # All environments except production considered to be test test_server = [:server] != 'production' test_mode = [:test_mode] [:test] = (test_server || test_mode) @gateway ||= gateway_class.new() end |
#options ⇒ Object
Represents all preferences as a Hash
Each preference is a key holding the value(s) and gets passed to the gateway via gateway_options
106 107 108 |
# File 'app/models/spree/payment_method.rb', line 106 def preferences.to_hash end |
#partial_name ⇒ Object
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
138 139 140 |
# File 'app/models/spree/payment_method.rb', line 138 def partial_name type.demodulize.underscore end |
#payment_profiles_supported? ⇒ Boolean
142 143 144 |
# File 'app/models/spree/payment_method.rb', line 142 def payment_profiles_supported? false end |
#payment_source_class ⇒ Object
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)
115 116 117 |
# File 'app/models/spree/payment_method.rb', line 115 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
152 153 154 |
# File 'app/models/spree/payment_method.rb', line 152 def reusable_sources(_order) [] end |
#source_required? ⇒ Boolean
146 147 148 |
# File 'app/models/spree/payment_method.rb', line 146 def source_required? true end |
#store_credit? ⇒ Boolean
194 195 196 |
# File 'app/models/spree/payment_method.rb', line 194 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
165 166 167 |
# File 'app/models/spree/payment_method.rb', line 165 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.
182 183 184 185 186 187 188 189 190 191 192 |
# File 'app/models/spree/payment_method.rb', line 182 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 |