Class: Spree::PaymentSplitSubscriber
- Inherits:
-
Subscriber
- Object
- Subscriber
- Spree::PaymentSplitSubscriber
- Defined in:
- app/subscribers/spree/payment_split_subscriber.rb
Overview
Keeps a split checkout's per-order shares in step with the money.
Two things move a share after it is written. A refund puts one seller's order right, and must touch only that order's share: the customer's money comes back out of the one charge either way, but what each seller owes and is owed does not move because a sibling refunded. And a payment completing captures the whole basket at once — an authorisation settled after placement — which every child shares in proportionally.
Synchronous, like the commission subscriber, because a share is what an order's payment status is derived from: an order that still read as unpaid until a job ran would be showing the wrong thing to the merchant and to the seller.
A share's refunded amount moves when a refund completes (payment.refunded, whether the provider answered at once or by webhook) or fails after it completed (refund.failed), never when the row is created: a provider may still refuse a requested refund.
Instance Method Summary collapse
-
#handle_capture(event) ⇒ Object
The whole payment settled, so every share it covers is captured in full.
-
#handle_refund(event) ⇒ Object
The payload names the payment, not the refund, so every share of it is recomputed; each is derived from the rows, so the others do not move.
- #handle_refund_failed(event) ⇒ Object
Instance Method Details
#handle_capture(event) ⇒ Object
The whole payment settled, so every share it covers is captured in full. A share already carrying a larger captured figure is left alone — a dispatch captured it first, and that is the more specific record.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/subscribers/spree/payment_split_subscriber.rb', line 50 def handle_capture(event) payment = Spree::Payment.find_by_prefix_id(event.payload['id']) return if payment.nil? || !payment.grouped? payment.payment_splits.includes(:order).each do |split| # Read and written under the row's lock, so a dispatch capturing the # same share at the same moment cannot be overwritten by the figure # this one read before it. settled = split.with_lock do next false if split.captured_amount >= split. split.update!(captured_amount: split.) true end Spree::Orders::UpdateStatuses.call(order: split.order) if settled && split.order end end |
#handle_refund(event) ⇒ Object
The payload names the payment, not the refund, so every share of it is recomputed; each is derived from the rows, so the others do not move.
31 32 33 34 35 36 |
# File 'app/subscribers/spree/payment_split_subscriber.rb', line 31 def handle_refund(event) payment = Spree::Payment.find_by_prefix_id(event.payload['id']) return if payment.nil? || !payment.grouped? payment.payment_splits.includes(:order).find_each { |split| refresh_refunded_amount(split) } end |
#handle_refund_failed(event) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'app/subscribers/spree/payment_split_subscriber.rb', line 38 def handle_refund_failed(event) refund = Spree::Refund.find_by_prefix_id(event.payload['id']) order = refund&.order return if order.nil? || !order.grouped? split = order.payment_split_for(refund.payment) refresh_refunded_amount(split) if split end |