Class: Spree::OrderStatusSubscriber

Inherits:
Subscriber
  • Object
show all
Defined in:
app/subscribers/spree/order_status_subscriber.rb

Overview

The trigger side of derive-then-persist statuses: any payment, refund, store credit, fulfillment or return change recomputes the owning order's payment_status / fulfillment_status through the single writer (Spree::Orders::UpdateStatuses). Synchronous so API responses right after a capture/void/fulfill already carry the fresh status. Cart-owned records are skipped — carts have no status columns.

Money-moving payment events additionally re-sum payment_total through the totals workflow, which is the only writer of that column. A payment save used to do both itself in an after_save callback; the callback and these events overlapped, so one capture recomputed the order four times.

Constant Summary collapse

MONEY_EVENTS =

Events whose payload changes what the customer has actually paid, so payment_total has to be re-summed before statuses derive from it. Store credit is not one of them: the money stayed with the store, and payment_total is what came in through payments. A refund counts once it completes (payment.refunded) and stops counting if it fails.

%w[
  payment.created payment.updated payment.deleted payment.completed
  payment.voided refund.created refund.updated payment.refunded refund.failed
  return.refunded
].freeze
SETTLEMENT_COMPANIONS =

payment.captured always accompanies payment.completed for the same settlement — the order changed once, so it is answered once. The remaining overlap (payment.updated is the settling save's own after_commit) is left alone deliberately: the payload cannot tell an accompanying update from a genuine later edit of a completed payment, and skipping the wrong one would leave the order stale. Two cheap recomputations beat a missed one.

Keyed on the event, never on the order: two payments settling in one request (mixed tender) are two real settlements and must both count.

%w[payment.captured].freeze

Instance Method Summary collapse

Instance Method Details

#handle(event) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/subscribers/spree/order_status_subscriber.rb', line 46

def handle(event)
  return if event.name.in?(SETTLEMENT_COMPANIONS)

  owner = owner_for(event)
  return if owner.nil?

  # Only the payment side is re-summed, before statuses derive from it
  # (payment_status reads payment_total). Deliberately not the full
  # totals workflow: a payment settling must not re-derive item and
  # delivery money, which is the totals workflow's business and would
  # overwrite figures the caller set.
  owner.refresh_payment_total! if event.name.in?(MONEY_EVENTS) && owner.respond_to?(:refresh_payment_total!)

  # Carts carry payment_total but no status columns — money still has to
  # be right while checkout is in flight, statuses only exist on orders.
  owner.update_statuses! if owner.is_a?(Spree::Order)
end