Class: Spree::PaymentSession
- Inherits:
-
Object
- Object
- Spree::PaymentSession
- Includes:
- HasCustomFields, PaymentSessionTransitions
- Defined in:
- app/models/spree/payment_session.rb
Direct Known Subclasses
Constant Summary collapse
- RETIRED_STATUSES =
Statuses after which a session's intent can no longer take money: placement cancels the provider intent, and an expired session's intent is gone. A failed session is not one of them — a declined confirm can leave its intent payable (see
unused). %w[canceled expired].freeze
Instance Method Summary collapse
- #amount_in_cents ⇒ Object
-
#apply_settlement_metadata(payment, metadata) ⇒ void
Records gateway-specific detail on the new payment.
-
#capture_reference_for_settlement(_payment) ⇒ String?
The provider's id for the capture when the payment settles as captured, written on its capture event so a late capture failure can find it.
- #expired? ⇒ Boolean
- #find_or_create_payment!(metadata = {}) ⇒ Object
- #money ⇒ Object
-
#order=(record) ⇒ Object
Bridge for legacy callers assigning
current_order(now a Spree::Cart) to the order association — routes carts to the cart FK instead. -
#own_payment ⇒ Spree::Payment?
Creates or finds the Spree::Payment record for this session.
- #owner ⇒ Spree::Cart, ...
-
#owner=(record) ⇒ Object
Assigns the owning record to the matching association (cart or order), clearing the other one.
-
#payment_source_for_settlement ⇒ Spree::PaymentSource?
The payment source to attach when the payment row is first created.
-
#prepare_for_settlement! ⇒ Spree::PaymentSession
Fetches whatever settlement will need from the provider, so a caller that settles inside a lock (the webhook path) holds it across no provider I/O.
-
#settle_payment!(captured:, metadata: {}) ⇒ Spree::Payment?
The one place a session settles its payment — used by both routes a settled session is noticed on (the storefront confirm call and the gateway webhook), so the two can never drift apart.
Methods included from PaymentSessionTransitions
#can_cancel?, #can_process?, #cancel, #complete, #expire, #fail, #process
Instance Method Details
#amount_in_cents ⇒ Object
46 47 48 |
# File 'app/models/spree/payment_session.rb', line 46 def amount_in_cents money.cents end |
#apply_settlement_metadata(payment, metadata) ⇒ void
This method returns an undefined value.
Records gateway-specific detail on the new payment. Overridden by gateways that carry references worth keeping (a charge id, a mandate).
134 135 136 |
# File 'app/models/spree/payment_session.rb', line 134 def (payment, ) payment..merge!(.stringify_keys) if .present? end |
#capture_reference_for_settlement(_payment) ⇒ String?
The provider's id for the capture when the payment settles as captured, written on its capture event so a late capture failure can find it. Called inside the settlement lock, so it must not perform provider I/O.
144 145 146 |
# File 'app/models/spree/payment_session.rb', line 144 def capture_reference_for_settlement(_payment) nil end |
#expired? ⇒ Boolean
54 55 56 |
# File 'app/models/spree/payment_session.rb', line 54 def expired? expires_at.present? && expires_at <= Time.current end |
#find_or_create_payment!(metadata = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/models/spree/payment_session.rb', line 77 def find_or_create_payment!( = {}) # An unsaved session has no identity to key a payment to, and nothing # has settled against it. return unless persisted? # Session payments carry no Spree-side source unless the gateway builds # one, so the requirement is skipped on every load, not just at creation # — a later capture webhook transitions the same row and would otherwise # fail source validation. existing = own_payment return skip_source_requirement(existing) if existing # Keyed on the gateway's own identifier alone. Never on the amount: a # session whose amount moved is still the same intent, and a second row # for it would both double the order's money and collide with the unique # index on (owner, payment_method, response_code). created = owner.payments.find_or_create_by!( payment_method: payment_method, response_code: external_id ) do |new_payment| new_payment.amount = amount new_payment.skip_source_requirement = true new_payment.source = payment_source_for_settlement (new_payment, ) end skip_source_requirement(created) rescue ActiveRecord::RecordNotUnique # The webhook and the customer's synchronous return race by design; # whoever loses the insert finds the row the winner created. skip_source_requirement( owner.payments.find_by!( payment_method: payment_method, response_code: external_id ) ) end |
#money ⇒ Object
50 51 52 |
# File 'app/models/spree/payment_session.rb', line 50 def money @money ||= Spree::Money.new(amount, currency: currency) end |
#order=(record) ⇒ Object
Bridge for legacy callers assigning current_order (now a Spree::Cart)
to the order association — routes carts to the cart FK instead.
201 202 203 204 205 206 207 208 |
# File 'app/models/spree/payment_session.rb', line 201 def order=(record) if record.is_a?(Spree::Cart) self.cart = record super(nil) else super end end |
#own_payment ⇒ Spree::Payment?
Creates or finds the Spree::Payment record for this session. Gateway subclasses can override this in their PaymentSession subclass to handle gateway-specific source creation (credit cards, wallets, etc).
The payment settled from this session: its owner's payment under the
session's payment method carrying the gateway's identifier, the row
find_or_create_payment! creates. The payment association matches the
identifier alone, which gateways only keep unique per payment method,
so it can return another method's or another owner's payment.
71 72 73 74 75 |
# File 'app/models/spree/payment_session.rb', line 71 def own_payment return unless persisted? owner&.payments&.find_by(payment_method_id: payment_method_id, response_code: external_id) end |
#owner ⇒ Spree::Cart, ...
195 196 197 |
# File 'app/models/spree/payment_session.rb', line 195 def owner order || cart end |
#owner=(record) ⇒ Object
Assigns the owning record to the matching association (cart or order), clearing the other one. Lets gateways stay owner-agnostic.
214 215 216 217 218 219 220 221 222 |
# File 'app/models/spree/payment_session.rb', line 214 def owner=(record) if record.is_a?(Spree::Cart) self.cart = record self.order = nil else self.order = record self.cart = nil end end |
#payment_source_for_settlement ⇒ Spree::PaymentSource?
The payment source to attach when the payment row is first created. Gateways holding the instrument (a card, a mandate) build one here so the payment shows a brand and last four digits; those with nothing to represent return nil and the payment stands on its own.
Called inside the settlement lock, so it must not perform provider I/O
— warm anything it needs in prepare_for_settlement!.
124 125 126 |
# File 'app/models/spree/payment_session.rb', line 124 def payment_source_for_settlement nil end |
#prepare_for_settlement! ⇒ Spree::PaymentSession
Fetches whatever settlement will need from the provider, so a caller that settles inside a lock (the webhook path) holds it across no provider I/O. Gateway session subclasses override; no-op by default.
153 154 155 |
# File 'app/models/spree/payment_session.rb', line 153 def prepare_for_settlement! self end |
#settle_payment!(captured:, metadata: {}) ⇒ Spree::Payment?
The one place a session settles its payment — used by both routes a settled session is noticed on (the storefront confirm call and the gateway webhook), so the two can never drift apart.
Serialized on the owner's row: the two routes can race, and the completed? guard alone is check-then-act — two concurrent settlements would each record a capture event, doubling captured_amount. The lock wraps only the local settlement, never gateway I/O — callers fetch provider data first (the confirm path as it verifies the session, the webhook path via prepare_for_settlement!) — and nests as a no-op inside HandleWebhook's own owner lock.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'app/models/spree/payment_session.rb', line 173 def settle_payment!(captured:, metadata: {}) owner.with_lock do settled_payment = find_or_create_payment!() if settled_payment.present? # DB-state recheck, deliberately not a reload — the payment may have # been cached on this instance before the lock (a concurrent # settlement could have completed it), but reload would discard # in-memory state like skip_source_requirement. # A requested payment waits on its provider's answer to a capture or # void, so a redelivered authorization must not move it. already_settled = settled_payment.status.in?(Spree::Payment::SETTLED_OR_WAITING_STATUSES) || Spree::Payment.exists?(id: settled_payment.id, status: Spree::Payment::SETTLED_OR_WAITING_STATUSES) settled_payment.confirm!(captured: captured, capture_reference: capture_reference_for_settlement(settled_payment)) unless already_settled end settled_payment end end |