AUTHORISE, CAPTURE, REFUND
A card payment is a state machine, not a row of flags
Ngozi's ₦50,000 Ojaline order is authorised. One item is out of stock, so the warehouse captures ₦32,000 and the rest of the hold is released. She then returns everything. The refund screen offers the order total minus refunds so far. What does your refund screen send?
A card payment happens in steps. Authorise asks the issuer to approve an amount and hold it. Capture takes it; for most payments there is one capture, and a partial one releases the rest. Void cancels a hold before capture. Refund sends captured money back, whole or in parts. A hold expires: one processor’s table gives 7 days for most online card payments and 2 for some in-person ones. Windows vary by scheme and change, so the sim makes yours a parameter.
Loose columns (status, captured_amount, a voided flag added later) let each code path check the one it knows. One state, a list of legal moves and two running totals refuse every illegal move in one place.
-- each feature added a column; each code path checks its own
-- card path: status = 'authorised'
-- warehouse: if status = 'authorised': capture; captured_amount = x
-- cancel button: voided = true
-- nightly sweep: capture shipped orders WHERE status = 'authorised'
-- refund screen: offers amount - refunded_amount
-- nothing knows when the authorisation expires// one state, one table of legal moves, running totals
authorised --capture(x ≤ authorised, day < expires)--> captured | partially_captured
authorised --void--> voided
authorised --day ≥ expires--> expired
captured | partially_captured | partially_refunded
--refund(x ≤ captured - refunded)--> partially_refunded | refunded
anything else --> refused, record unchanged
UPDATE payments SET state = 'captured', captured = captured + :x
WHERE id = :id AND state = 'authorised' AND :day < expires_day
AND captured + :x <= authorised; -- 0 rows: the move is illegal Watch the three invariants and the “sent to the gateway” line under each payment. Many gateways would reject some of these calls; the sim shows what your records ask for, which is where the bug lives. LOOSE COLUMNS turns captured ≤ authorised red when the sweep captures Emeka’s P2 a second time, no capture on a voided or expired authorisation red when the warehouse captures the voided P3, and refunded ≤ captured red when Bisi refunds ₦50,000 against ₦32,000. STATES, NO TOTALS stops the first two and still over-refunds, because a state alone does not know how much was taken. FIX: STATE MACHINE + TOTALS holds all three. Move P1’s ship day past the authorisation window and the fix marks P1 expired instead of capturing it.
The second capture here comes from a different job, not a retry, so an idempotency key would not catch it. Retries of one request are the idempotency lesson; this one is about requests that are each new and still illegal. The processor reports each move back to you by webhook, late and sometimes out of order, which is webhook delivery.
The loose-columns design has no idea when an authorisation ends. Move P1’s ship day and the authorisation window until the warehouse captures P1 on an authorisation that has already expired.
GOAL: P1 is captured on an expired authorisation
Rewrite handle as a state machine. Return true if the event is accepted and update the record; return false and leave the amounts unchanged if it is not. Rules: one capture per authorisation, only while authorised and ev.day < p.expiresDay, for 0 < amount ≤ authorised (a partial capture moves to partially_captured and releases the rest). A void only while authorised and unexpired. Refunds only after a capture, for 0 < amount ≤ captured − refunded. Any event on an authorised payment at or after expiresDay moves it to expired first.
Checkout and order systems that ship later (marketplaces, hotels, car hire, pre-orders), partial shipments, returns desks, nightly capture sweeps, and the payment tables behind them. Processors expose it as manual capture, an amount to capture, cancel and refund calls; your own table has to agree with them. The usual symptom is a reconciliation break: captured more than authorised, refunds larger than captures, captures on cancelled orders.
“Model a card payment for an order that ships in parts.” Draw the states (authorised, partially captured, captured, partially refunded, refunded, voided, expired) and the legal transitions. Put the guards in the write itself (UPDATE … WHERE state = 'authorised' AND captured + :x ≤ authorised), keep the expiry day on the row, re-authorise rather than capture late, and say that idempotency keys handle retries while the state machine handles illegal requests.
USED IN
Chapters that build on this lesson.