Module: Order::AuthorizationSplittable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Order
- Defined in:
- app/models/order/authorization_splittable.rb
Overview
Lets a split-off child order pull its share of the parent order's
authorization once it has a committed delivery to attach payment to.
Splitting moves line items into the child immediately, but its delivery
(and therefore the unit payments attach to) is committed later. The original
splitter only allocated payment when that delivery happened to exist at split
time; this concern + AuthorizationSplitWorker close the gap by
reacting when the delivery is finally committed.
Belongs to collapse
Has many collapse
Instance Method Summary collapse
-
#needs_authorization_split_from_parent? ⇒ Boolean
True when this is a split-off child that is funded by a parent authorization that hasn't been allocated to it yet, and whose deliveries are now committed (so the amount is final and there's something to attach payment to).
-
#needs_shared_pi_authorization_topup? ⇒ Boolean
True when this order (parent OR child of a split) carries a shared-PI multicapture authorization that no longer covers its total — the split's coupon reallocation and shipping re-quote can raise an order's total after the authorization was taken against an interim total (SO727687: authorized $2,260.60 against a $2,626.23 order).
Instance Method Details
#children ⇒ ActiveRecord::Relation<Order>
21 |
# File 'app/models/order/authorization_splittable.rb', line 21 has_many :children, class_name: 'Order', foreign_key: :parent_id, inverse_of: :parent, dependent: :nullify |
#needs_authorization_split_from_parent? ⇒ Boolean
True when this is a split-off child that is funded by a parent
authorization that hasn't been allocated to it yet, and whose deliveries
are now committed (so the amount is final and there's something to attach
payment to).
The authoritative idempotency guard — re-checked under the advisory lock in
Order::AuthorizationSplitWorker.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/models/order/authorization_splittable.rb', line 33 def return false if parent_id.blank? own_deliveries = deliveries.to_a return false if own_deliveries.empty? return false if own_deliveries.any?(&:quoting?) # wait until shipping is finalized return false unless balance.positive? # nothing left to fund return false if payments..exists? # already has its own authorization par = parent return false unless par # A parent authorization is still "unsplit" while its original CC auth is # intact (the shared-PI split voids it), or there's an authorized PO/PayPal. par.payments. .where(category: [Payment::CREDIT_CARD, Payment::PO, Payment::VPO, Payment::CHECK, Payment::CASH, Payment::PAYPAL]) .exists? end |
#needs_shared_pi_authorization_topup? ⇒ Boolean
True when this order (parent OR child of a split) carries a shared-PI
multicapture authorization that no longer covers its total — the split's
coupon reallocation and shipping re-quote can raise an order's total
after the authorization was taken against an interim total (SO727687:
authorized $2,260.60 against a $2,626.23 order).
The parent counterpart to #needs_authorization_split_from_parent?: the
deferred child-only worker never re-funds the parent, so a parent left
short by post-split re-pricing has no other safety net. Guarded like the
child split — only once deliveries are out of quoting, so the total is
final before we authorize against it.
65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/order/authorization_splittable.rb', line 65 def own_deliveries = deliveries.to_a return false if own_deliveries.empty? return false if own_deliveries.any?(&:quoting?) # wait until totals are final return false unless balance.positive? # already fully authorized payments.credit_cards. .where.not(stripe_payment_intent_id: nil) .any?(&:shared_pi?) end |
#parent ⇒ Order?
19 |
# File 'app/models/order/authorization_splittable.rb', line 19 belongs_to :parent, class_name: 'Order', optional: true, inverse_of: :children |