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
-
.active? ⇒ Boolean
deprecated
Deprecated.
Use .active.any? instead
-
.available(display_on = nil, store: nil) ⇒ Object
deprecated
Deprecated.
Use active, available_to_users, and available_to_admin scopes instead.
- .const_missing(name) ⇒ Object
-
.find_with_destroyed(*args) ⇒ Object
deprecated
Deprecated.
Use .with_discarded.find instead
- .model_name ⇒ Object
-
.providers ⇒ Object
deprecated
Deprecated.
Use Spree::Config.environment.payment_methods instead
Instance Method Summary collapse
- #auto_capture? ⇒ Boolean
-
#deprecated_method_type_override ⇒ Object
:nodoc: If method_type has been overridden, call it and return the value, otherwise return nil.
-
#display_on ⇒ Object
deprecated
Deprecated.
Use #available_to_users and #available_to_admin instead
-
#display_on=(value) ⇒ Object
deprecated
Deprecated.
Use #available_to_users= and #available_to_admin= instead
-
#gateway ⇒ Object
(also: #provider)
Represents the gateway of this payment method.
- #method_type ⇒ Object
-
#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
display_includes, page, preference, #preferences
Methods included from Core::Permalinks
#generate_permalink, #save_permalink
Class Method Details
.active? ⇒ Boolean
Use .active.any? instead
108 109 110 111 |
# File 'app/models/spree/payment_method.rb', line 108 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
Use active, available_to_users, and available_to_admin scopes instead.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/spree/payment_method.rb', line 81 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
25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/models/spree/payment_method.rb', line 25 def self.const_missing(name) if name == :DISPLAY Spree::Deprecation.warn( "#{self}::DISPLAY has been deprecated and will be removed in Solidus v3.", caller ) const_set(:DISPLAY, [:both, :front_end, :back_end]) else super end end |
.find_with_destroyed(*args) ⇒ Object
Use .with_discarded.find instead
114 115 116 117 |
# File 'app/models/spree/payment_method.rb', line 114 def find_with_destroyed(*args) Spree::Deprecation.warn "#{self}.find_with_destroyed is deprecated. Use #{self}.with_discarded.find instead" unscoped { find(*args) } end |
.model_name ⇒ Object
103 104 105 |
# File 'app/models/spree/payment_method.rb', line 103 def model_name ModelName.new(self, Spree) end |
.providers ⇒ Object
Use Spree::Config.environment.payment_methods instead
74 75 76 77 78 |
# File 'app/models/spree/payment_method.rb', line 74 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
239 240 241 |
# File 'app/models/spree/payment_method.rb', line 239 def auto_capture? auto_capture.nil? ? Spree::Config[:auto_capture] : auto_capture end |
#deprecated_method_type_override ⇒ Object
:nodoc: If method_type has been overridden, call it and return the value, otherwise return nil
213 214 215 216 217 218 |
# File 'app/models/spree/payment_method.rb', line 213 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_on ⇒ Object
Use #available_to_users and #available_to_admin instead
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'app/models/spree/payment_method.rb', line 174 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
Use #available_to_users= and #available_to_admin= instead
166 167 168 169 170 171 |
# File 'app/models/spree/payment_method.rb', line 166 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 |
#gateway ⇒ Object 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:
-
- purchase
- capture
- void
- credit
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/models/spree/payment_method.rb', line 132 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 |
#method_type ⇒ Object
220 221 222 223 |
# File 'app/models/spree/payment_method.rb', line 220 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 |
#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
152 153 154 |
# File 'app/models/spree/payment_method.rb', line 152 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
207 208 209 |
# File 'app/models/spree/payment_method.rb', line 207 def partial_name deprecated_method_type_override || type.demodulize.underscore end |
#payment_profiles_supported? ⇒ Boolean
225 226 227 |
# File 'app/models/spree/payment_method.rb', line 225 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)
161 162 163 |
# File 'app/models/spree/payment_method.rb', line 161 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
235 236 237 |
# File 'app/models/spree/payment_method.rb', line 235 def reusable_sources(_order) [] end |
#source_required? ⇒ Boolean
229 230 231 |
# File 'app/models/spree/payment_method.rb', line 229 def source_required? true end |
#store_credit? ⇒ Boolean
271 272 273 |
# File 'app/models/spree/payment_method.rb', line 271 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
248 249 250 |
# File 'app/models/spree/payment_method.rb', line 248 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.
263 264 265 266 267 268 269 |
# File 'app/models/spree/payment_method.rb', line 263 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 |