Class: Spree::PaymentSplit

Inherits:
Object
  • Object
show all
Extended by:
DisplayMoney
Defined in:
app/models/spree/payment_split.rb

Overview

One child order's share of one payment made against an order group.

A grouped checkout takes a single charge — the customer authorised one amount, and creating N gateway charges would change what they agreed to. The per-seller bookkeeping is these rows: each child order's authorised, captured and refunded share of that one payment.

Durable rather than computed on read, because the shares stop being proportional the moment anything partial happens: one seller ships and is captured while another has not, one child is refunded and its siblings are not. A proportion recomputed from current totals cannot express that history; a row can.

Every payment on the group gets its own set of rows — the gateway charge, the store credit, the gift card — so a child's shares across all of them sum to that child's total, whatever the customer paid with.

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Instance Method Details

#capture_in_flight? ⇒ Boolean

Returns whether a charge for this share is still in flight — reserved by a parcel, and not yet confirmed by the gateway.

Returns:

  • (Boolean) —

    whether a charge for this share is still in flight — reserved by a parcel, and not yet confirmed by the gateway



104
105
106
# File 'app/models/spree/payment_split.rb', line 104

def capture_in_flight?
  claimed_amount.positive?
end

#net_captured_amount ⇒ BigDecimal

This child's reported position: what it has captured, less the refunds a subscriber has already written back to #refunded_amount.

The reported figure, which is what makes it right for reading — a seller's earnings, an order's payment total — and wrong for bounding a refund, since the column trails the rows. Reach for #refundable_amount before moving money.

Returns:

  • (BigDecimal)


61
62
63
# File 'app/models/spree/payment_split.rb', line 61

def net_captured_amount
  captured_amount - refunded_amount
end

#refundable_amount ⇒ BigDecimal

The most this child order can still be given back from this payment.

The same quantity as #net_captured_amount counted from the refund rows instead of the denormalized column, because that column is written by a subscriber after the fact: two refunds landing together would each read a share that still looked unrefunded. Refunds::Create bounds every refund by this under the payment's lock. A refund still waiting on its provider counts, so its amount cannot be refunded twice.

Returns:

  • (BigDecimal)


75
76
77
78
# File 'app/models/spree/payment_split.rb', line 75

def refundable_amount
  already_refunded = Spree::Refund.not_failed.where(payment_id: payment_id, order_id: order_id).sum(:amount)
  captured_amount - already_refunded
end

#settle_claim!(amount, captured:) ⇒ Object

Turns a claim into what it turned out to be: money the gateway took, or nothing at all.

Parameters:

  • amount (BigDecimal) —

    how much of the claim settles

  • captured (Boolean) —

    whether the gateway took it



96
97
98
99
100
# File 'app/models/spree/payment_split.rb', line 96

def settle_claim!(amount, captured:)
  attributes = { claimed_amount: claimed_amount - amount }
  attributes[:captured_amount] = captured_amount + amount if captured
  update!(attributes)
end

#undrawn_amount ⇒ BigDecimal

What is left of this share for a parcel to draw on.

Counts money already taken and money another parcel has reserved but not yet drawn, so two dispatching at once cannot both reach for the same amount.

Returns:

  • (BigDecimal)


87
88
89
# File 'app/models/spree/payment_split.rb', line 87

def undrawn_amount
  authorized_amount - captured_amount - claimed_amount
end