Class: Spree::PaymentMethod
- Inherits:
-
Object
- Object
- Spree::PaymentMethod
- Includes:
- CaptureMethod, HasCustomFields, HasListPosition, Metadata, ProviderListing, SecretPreferences, Security::PaymentMethods, SingleStoreResource, StorePreferences
- Defined in:
- app/models/spree/payment_method.rb,
app/models/spree/payment_method/session_instrumentation.rb
Direct Known Subclasses
Defined Under Namespace
Modules: SessionInstrumentation Classes: Check, StoreCredit, WebhookSignatureError
Constant Summary
Constants included from CaptureMethod
CaptureMethod::CAPTURE_METHODS, CaptureMethod::DEFAULT_CAPTURE_METHOD
Class Method Summary collapse
- .find_with_destroyed(*args) ⇒ Object
-
.inherited(subclass) ⇒ void
Provider subclasses override the payment-session methods wholesale, so tracing instrumentation must be prepended to each subclass — prepended to this base class it would sit below the override and never run.
-
.provider_listing ⇒ Hash
The listing attributes, plus whether the method is third-party.
- .providers ⇒ Object
-
.third_party? ⇒ Boolean
Whether this method is backed by an external payment provider (Stripe, Adyen…) rather than handled by the store itself (check, store credit).
Instance Method Summary collapse
-
#auto_capture? ⇒ Boolean
deprecated
Deprecated.
Use #capture_at_checkout?; removed in 6.3. Deliberately does not warn — it runs on every payment and would flood the logs.
-
#available_for_order?(order) ⇒ Boolean
Custom PaymentMethod/Gateway can redefine this method to check method availability for concrete order.
- #available_for_store?(store) ⇒ Boolean
-
#available_on_front_end? ⇒ Boolean
deprecated
Deprecated.
Use #storefront_visible?; removed in 6.3.
-
#can_capture?(_payment) ⇒ Boolean
Whether the payment can be captured in its current state.
-
#can_void?(_payment) ⇒ Boolean
Whether the payment can be voided in its current state.
-
#cancel(_response, _payment = nil, refund: true) ⇒ Object
Settles a payment for a canceled order.
-
#cancel_payment_session(payment_session:) ⇒ Boolean
Cancels a session nobody is going to complete, so the provider can no longer take money against it.
-
#cancel_voids?(payment) ⇒ Boolean
Whether #cancel voids the payment.
-
#complete_payment_session(payment_session:, params: {}) ⇒ Object
Completes a payment session via the provider.
-
#complete_payment_setup_session(setup_session:, params: {}) ⇒ Object
Completes a payment setup session via the provider.
- #confirmation_required? ⇒ Boolean
-
#create_payment_session(order:, amount: nil, external_data: {}) ⇒ Object
Creates a payment session via the provider.
-
#create_payment_setup_session(customer:, external_data: {}) ⇒ Object
Creates a payment setup session via the provider for saving a payment method.
- #default_name ⇒ Object
-
#display_on ⇒ Object
deprecated
Deprecated.
Use #storefront_visible; removed in 6.3.
-
#display_on=(value) ⇒ Object
deprecated
Deprecated.
Use #storefront_visible=; removed in 6.3.
-
#gateway_amount(amount, currency) ⇒ Spree::Money, Integer
The amount in the form this payment method's gateway calls take.
- #method_type ⇒ Object
-
#modification_record(action, reference) ⇒ Spree::PaymentCaptureEvent, ...
The record a modification outcome settles, or nil while it is not written yet.
-
#parse_webhook_event(raw_body, headers) ⇒ Hash?
Parses an incoming webhook payload from the payment provider.
- #payment_icon_name ⇒ Object
- #payment_profiles_supported? ⇒ Boolean
-
#payment_session_class ⇒ Object
The class used for payment sessions with this payment method.
-
#payment_setup_session_class ⇒ Object
The class used for payment setup sessions with this payment method.
-
#payment_source_class ⇒ Object
The class that will process payments for this payment type, used for @payment.source e.g.
- #provider_class ⇒ Object
- #public_preferences ⇒ Object
-
#resolved_capture_method ⇒ String
The method's own choice when it has one, otherwise the store's.
-
#reusable_sources(_order) ⇒ Object
Custom gateways should redefine this method.
-
#risk_codes_for(_source) ⇒ Hash?
Risk codes the gateway can derive from a payment source — the address and card-verification check results the provider recorded when the source was created.
- #session_required? ⇒ Boolean
-
#setup_session_supported? ⇒ Boolean
Whether this payment method supports setup sessions (saving payment methods for future use).
- #show_in_admin? ⇒ Boolean
- #source_required? ⇒ Boolean
- #store_credit? ⇒ Boolean
- #supports?(_source) ⇒ Boolean
-
#update_payment_session(payment_session:, amount: nil, external_data: {}) ⇒ Object
Updates an existing payment session via the provider.
-
#webhook_url ⇒ String?
Returns the webhook URL for this payment method.
Methods included from CaptureMethod
#capture_at_checkout?, #capture_manually?, #capture_on_dispatch?
Methods included from SecretPreferences
Methods included from Metadata
#private_metadata, #private_metadata=
Methods included from StorePreferences
current, #preference_store, read, #store_preference
Class Method Details
.find_with_destroyed(*args) ⇒ Object
283 284 285 |
# File 'app/models/spree/payment_method.rb', line 283 def self.find_with_destroyed(*args) unscoped { find(*args) } end |
.inherited(subclass) ⇒ void
This method returns an undefined value.
Provider subclasses override the payment-session methods wholesale, so tracing instrumentation must be prepended to each subclass — prepended to this base class it would sit below the override and never run.
42 43 44 45 |
# File 'app/models/spree/payment_method.rb', line 42 def self.inherited(subclass) super subclass.prepend(Spree::PaymentMethod::SessionInstrumentation) end |
.provider_listing ⇒ Hash
Returns the listing attributes, plus whether the method is third-party.
96 97 98 |
# File 'app/models/spree/payment_method.rb', line 96 def self.provider_listing super.merge(third_party: third_party?) end |
.providers ⇒ Object
78 79 80 |
# File 'app/models/spree/payment_method.rb', line 78 def self.providers Spree.payment_methods end |
.third_party? ⇒ Boolean
Whether this method is backed by an external payment provider (Stripe, Adyen…) rather than handled by the store itself (check, store credit). Only third-party methods are listed under Settings → Integrations.
91 92 93 |
# File 'app/models/spree/payment_method.rb', line 91 def self.third_party? false end |
Instance Method Details
#auto_capture? ⇒ Boolean
Use #capture_at_checkout?; removed in 6.3. Deliberately does not warn — it runs on every payment and would flood the logs.
333 334 335 |
# File 'app/models/spree/payment_method.rb', line 333 def auto_capture? capture_at_checkout? end |
#available_for_order?(order) ⇒ Boolean
Custom PaymentMethod/Gateway can redefine this method to check method availability for concrete order.
383 384 385 |
# File 'app/models/spree/payment_method.rb', line 383 def available_for_order?(order) !order.covered_by_store_credit? end |
#available_for_store?(store) ⇒ Boolean
387 388 389 390 391 |
# File 'app/models/spree/payment_method.rb', line 387 def available_for_store?(store) return true if store.blank? store_id == store.id end |
#available_on_front_end? ⇒ Boolean
Use #storefront_visible?; removed in 6.3.
398 399 400 401 402 403 |
# File 'app/models/spree/payment_method.rb', line 398 def available_on_front_end? Spree::Deprecation.warn( 'Spree::PaymentMethod#available_on_front_end? is deprecated and will be removed in Spree 6.3. Use #storefront_visible? instead.' ) storefront_visible? end |
#can_capture?(_payment) ⇒ Boolean
Whether the payment can be captured in its current state.
373 374 375 |
# File 'app/models/spree/payment_method.rb', line 373 def can_capture?(_payment) true end |
#can_void?(_payment) ⇒ Boolean
Whether the payment can be voided in its current state. Gateways that cannot void a captured payment override this.
367 368 369 |
# File 'app/models/spree/payment_method.rb', line 367 def can_void?(_payment) true end |
#cancel(_response, _payment = nil, refund: true) ⇒ Object
Settles a payment for a canceled order. A gateway releases what it never
drew on; what it does with money already taken depends on refund:,
which is false when the operator asked to keep it — an adapter that
cannot void a captured charge must then leave it alone rather than
refunding anyway.
350 351 352 |
# File 'app/models/spree/payment_method.rb', line 350 def cancel(_response, _payment = nil, refund: true) raise ::NotImplementedError, 'You must implement cancel method for this payment method.' end |
#cancel_payment_session(payment_session:) ⇒ Boolean
Cancels a session nobody is going to complete, so the provider can no longer take money against it. Called for the sessions left pending when an order is placed. The default leaves the session untouched: a gateway that cannot cancel at the provider must not report as canceled a session that can still be paid. Adyen is the one exception: it cancels locally, which is a real cancel only for a session never made payable.
Raise Spree::Core::GatewayError only for a refusal, such as a session that was already paid. A timeout, a rate limit, an outage, or rejected credentials is raised as itself. Callers that treat those as a refusal leave a session the buyer can still pay.
178 179 180 |
# File 'app/models/spree/payment_method.rb', line 178 def cancel_payment_session(payment_session:) false end |
#cancel_voids?(payment) ⇒ Boolean
Whether #cancel voids the payment. A gateway that cannot void a
captured charge refunds it through Refunds::Create instead, or leaves it
standing when refund: is false, so the payment stays completed with
any refund recorded against it.
360 361 362 |
# File 'app/models/spree/payment_method.rb', line 360 def cancel_voids?(payment) can_void?(payment) end |
#complete_payment_session(payment_session:, params: {}) ⇒ Object
Completes a payment session via the provider. Override in gateway subclasses to implement provider-specific session completion.
Responsibilities:
- Verify payment status with the provider
- Create/update the Spree::Payment record
- Patch order data from provider (e.g. wallet billing address)
- Transition payment session to completed/failed
Must NOT complete the order — that is handled by Carts::Complete (called by the frontend or by the webhook handler).
158 159 160 |
# File 'app/models/spree/payment_method.rb', line 158 def complete_payment_session(payment_session:, params: {}) raise ::NotImplementedError, 'You must implement complete_payment_session method for this gateway.' end |
#complete_payment_setup_session(setup_session:, params: {}) ⇒ Object
Completes a payment setup session via the provider. Override in gateway subclasses to implement provider-specific setup session completion.
267 268 269 |
# File 'app/models/spree/payment_method.rb', line 267 def complete_payment_setup_session(setup_session:, params: {}) raise ::NotImplementedError, "#{self.class.name} does not implement #complete_payment_setup_session" end |
#confirmation_required? ⇒ Boolean
287 288 289 |
# File 'app/models/spree/payment_method.rb', line 287 def confirmation_required? false end |
#create_payment_session(order:, amount: nil, external_data: {}) ⇒ Object
Creates a payment session via the provider. Override in gateway subclasses to implement provider-specific session creation.
137 138 139 |
# File 'app/models/spree/payment_method.rb', line 137 def create_payment_session(order:, amount: nil, external_data: {}) raise ::NotImplementedError, 'You must implement create_payment_session method for this gateway.' end |
#create_payment_setup_session(customer:, external_data: {}) ⇒ Object
Creates a payment setup session via the provider for saving a payment method. Override in gateway subclasses to implement provider-specific setup session creation.
261 262 263 |
# File 'app/models/spree/payment_method.rb', line 261 def create_payment_setup_session(customer:, external_data: {}) raise ::NotImplementedError, "#{self.class.name} does not implement #create_payment_setup_session" end |
#default_name ⇒ Object
275 276 277 |
# File 'app/models/spree/payment_method.rb', line 275 def default_name self.class.name.demodulize.titleize.gsub(/Gateway/, '').strip end |
#display_on ⇒ Object
Use #storefront_visible; removed in 6.3.
406 407 408 409 |
# File 'app/models/spree/payment_method.rb', line 406 def display_on Spree::Deprecation.warn('Spree::PaymentMethod#display_on is deprecated and will be removed in Spree 6.3. Use #storefront_visible instead.') storefront_visible? ? 'both' : 'back_end' end |
#display_on=(value) ⇒ Object
Use #storefront_visible=; removed in 6.3.
412 413 414 415 |
# File 'app/models/spree/payment_method.rb', line 412 def display_on=(value) Spree::Deprecation.warn('Spree::PaymentMethod#display_on= is deprecated and will be removed in Spree 6.3. Use #storefront_visible= instead.') self.storefront_visible = value.to_s != 'back_end' end |
#gateway_amount(amount, currency) ⇒ Spree::Money, Integer
The amount in the form this payment method's gateway calls take.
424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'app/models/spree/payment_method.rb', line 424 def gateway_amount(amount, currency) return Spree::Money.new(amount, currency: currency) if accepts_money_amounts warn_about_hundredths hundredths = Spree::Money::Rounding.to_hundredths(amount) # A hundredth is the smallest amount these gateways can be sent; zero # reads as "the whole amount" to many of them. if hundredths.zero? && !amount.zero? raise Spree::Core::GatewayError, "#{self.class.name} cannot take an amount below a hundredth of #{currency}" end hundredths end |
#method_type ⇒ Object
271 272 273 |
# File 'app/models/spree/payment_method.rb', line 271 def method_type type.demodulize.downcase end |
#modification_record(action, reference) ⇒ Spree::PaymentCaptureEvent, ...
The record a modification outcome settles, or nil while it is not written yet. Reads only this method's payments.
The default matches the reference against what core records from a
requested? answer: a capture event's or refund's transaction_id, and
for a void the response code of a payment waiting on it (or already
void, for a repeated delivery). Override when the provider names
records otherwise.
224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'app/models/spree/payment_method.rb', line 224 def modification_record(action, reference) own_payments = Spree::Payment.where(payment_method_id: id) case action.to_sym when :capture_succeeded, :capture_failed Spree::PaymentCaptureEvent.where(payment_id: own_payments.select(:id)).find_by(transaction_id: reference) when :void_succeeded, :void_failed own_payments.where(status: %w[requested void]).find_by(response_code: reference) when :refund_succeeded, :refund_failed Spree::Refund.where(payment_id: own_payments.select(:id)).find_by(transaction_id: reference) end end |
#parse_webhook_event(raw_body, headers) ⇒ Hash?
Parses an incoming webhook payload from the payment provider. Override in gateway subclasses to implement provider-specific webhook parsing.
208 209 210 |
# File 'app/models/spree/payment_method.rb', line 208 def parse_webhook_event(raw_body, headers) raise ::NotImplementedError, 'You must implement parse_webhook_event method for this gateway.' end |
#payment_icon_name ⇒ Object
279 280 281 |
# File 'app/models/spree/payment_method.rb', line 279 def payment_icon_name type.demodulize.gsub(/(^Spree::Gateway::|Gateway$)/, '').downcase.gsub(/\s+/, '').strip end |
#payment_profiles_supported? ⇒ Boolean
291 292 293 |
# File 'app/models/spree/payment_method.rb', line 291 def payment_profiles_supported? false end |
#payment_session_class ⇒ Object
The class used for payment sessions with this payment method. Override in gateway subclasses to provide a provider-specific session class that inherits from Spree::PaymentSession (STI). nil means the payment method doesn't support payment sessions.
117 118 119 |
# File 'app/models/spree/payment_method.rb', line 117 def payment_session_class nil end |
#payment_setup_session_class ⇒ Object
The class used for payment setup sessions with this payment method. Override in gateway subclasses to provide a provider-specific session class.
255 256 257 |
# File 'app/models/spree/payment_method.rb', line 255 def payment_setup_session_class nil end |
#payment_source_class ⇒ Object
The class that will process payments for this payment type, used for @payment.source e.g. CreditCard in the case of a the Gateway payment type nil means the payment method doesn't require a source e.g. check
107 108 109 110 111 |
# File 'app/models/spree/payment_method.rb', line 107 def payment_source_class return unless source_required? raise ::NotImplementedError, 'You must implement payment_source_class method for this gateway.' end |
#provider_class ⇒ Object
100 101 102 |
# File 'app/models/spree/payment_method.rb', line 100 def provider_class raise ::NotImplementedError, 'You must implement provider_class method for this gateway.' end |
#public_preferences ⇒ Object
393 394 395 |
# File 'app/models/spree/payment_method.rb', line 393 def public_preferences public_preference_keys.index_with { |key| get_preference(key) } end |
#resolved_capture_method ⇒ String
The method's own choice when it has one, otherwise the store's. A legacy auto_capture of true is still honored for installs that set it before capture_method existed and have not run the migration task.
auto_capture false is NOT read as manual: it only ever recorded "not at checkout" and could not distinguish dispatch from staff collection, so the store decides — the same rows the migration deliberately leaves empty. Reading it as manual would exclude those payments from dispatch capture while still letting the goods go out.
324 325 326 327 328 329 |
# File 'app/models/spree/payment_method.rb', line 324 def resolved_capture_method return capture_method if capture_method.present? return 'checkout' if auto_capture store_preference(:capture_method).presence || Spree::CaptureMethod::DEFAULT_CAPTURE_METHOD end |
#reusable_sources(_order) ⇒ Object
Custom gateways should redefine this method. See Gateway implementation as an example
309 310 311 |
# File 'app/models/spree/payment_method.rb', line 309 def reusable_sources(_order) [] end |
#risk_codes_for(_source) ⇒ Hash?
Risk codes the gateway can derive from a payment source — the address and
card-verification check results the provider recorded when the source was
created. Payment assigns them at creation when the gateway response path
did not supply any, so session-created payments still feed
Spree::Payment.risky and order risk analysis.
Override in gateway subclasses that store check results on their sources.
131 132 133 |
# File 'app/models/spree/payment_method.rb', line 131 def risk_codes_for(_source) nil end |
#session_required? ⇒ Boolean
299 300 301 |
# File 'app/models/spree/payment_method.rb', line 299 def session_required? false end |
#setup_session_supported? ⇒ Boolean
Whether this payment method supports setup sessions (saving payment methods for future use). Override in gateway subclasses that support tokenization without a payment.
249 250 251 |
# File 'app/models/spree/payment_method.rb', line 249 def setup_session_supported? false end |
#show_in_admin? ⇒ Boolean
303 304 305 |
# File 'app/models/spree/payment_method.rb', line 303 def show_in_admin? true end |
#source_required? ⇒ Boolean
295 296 297 |
# File 'app/models/spree/payment_method.rb', line 295 def source_required? true end |
#store_credit? ⇒ Boolean
377 378 379 |
# File 'app/models/spree/payment_method.rb', line 377 def store_credit? self.class == Spree::PaymentMethod::StoreCredit end |
#supports?(_source) ⇒ Boolean
337 338 339 |
# File 'app/models/spree/payment_method.rb', line 337 def supports?(_source) true end |
#update_payment_session(payment_session:, amount: nil, external_data: {}) ⇒ Object
Updates an existing payment session via the provider. Override in gateway subclasses to implement provider-specific session updates.
143 144 145 |
# File 'app/models/spree/payment_method.rb', line 143 def update_payment_session(payment_session:, amount: nil, external_data: {}) raise ::NotImplementedError, 'You must implement update_payment_session method for this gateway.' end |
#webhook_url ⇒ String?
Returns the webhook URL for this payment method.
239 240 241 242 243 |
# File 'app/models/spree/payment_method.rb', line 239 def webhook_url return nil unless store "#{store.formatted_url_or_custom_domain}/api/v3/webhooks/payments/#{prefixed_id}" end |