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
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, false
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
Instance Method Details
#auto_capture? ⇒ Boolean
145 146 147 |
# File 'app/models/spree/payment_method.rb', line 145 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
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/spree/payment_method.rb', line 77 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
95 96 97 |
# File 'app/models/spree/payment_method.rb', line 95 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
127 128 129 |
# File 'app/models/spree/payment_method.rb', line 127 def partial_name type.demodulize.underscore end |
#payment_profiles_supported? ⇒ Boolean
131 132 133 |
# File 'app/models/spree/payment_method.rb', line 131 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)
104 105 106 |
# File 'app/models/spree/payment_method.rb', line 104 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
141 142 143 |
# File 'app/models/spree/payment_method.rb', line 141 def reusable_sources(_order) [] end |
#source_required? ⇒ Boolean
135 136 137 |
# File 'app/models/spree/payment_method.rb', line 135 def source_required? true end |
#store_credit? ⇒ Boolean
177 178 179 |
# File 'app/models/spree/payment_method.rb', line 177 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
154 155 156 |
# File 'app/models/spree/payment_method.rb', line 154 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.
169 170 171 172 173 174 175 |
# File 'app/models/spree/payment_method.rb', line 169 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 |