AUTHORISE, CAPTURE, REFUND

A card payment is a state machine, not a row of flags

L216 MINMONEY
PREDICT

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.

▶ SIMULATION
design · dayloose columns · day 0
P1 · Ngozi · ₦50,000
your record: status '—' · captured_amount ₦0 · refunded_amount ₦0 · voided false
sent to the gateway: captured ₦0 in 0 capture(s) · refunded ₦0
P2 · Emeka · ₦40,000
your record: status '—' · captured_amount ₦0 · refunded_amount ₦0 · voided false
sent to the gateway: captured ₦0 in 0 capture(s) · refunded ₦0
P3 · Ngozi · ₦18,000
your record: status '—' · captured_amount ₦0 · refunded_amount ₦0 · voided false
sent to the gateway: captured ₦0 in 0 capture(s) · refunded ₦0
last events—
✓ captured ≤ authorised, per payment✓ refunded ≤ captured, per payment✓ no capture on a voided or expired authorisation
SCHEDULE
01[CARD]CARD: payments stored as loose columns: status, captured_amount, refunded_amount, voided
02[CARD]CARD: day 0, authorise P1: Ngozi, ₦50,000
03[CARD]CARD: day 0, authorise P2: Emeka, ₦40,000
04[CARD]CARD: day 0, authorise P3: Ngozi, ₦18,000
05[SUPPORT]SUPPORT: day 1, Ngozi cancels P3; Bisi voids it
06[WAREHOUSE]WAREHOUSE: day 1, P2 ships in full; capture ₦40,000
07[JOB]JOB: day 1 night, sweep: capture any shipped order whose payment is still authorised
08[WAREHOUSE]WAREHOUSE: day 2, P3 was picked before the cancel arrived; capture ₦18,000 before dispatch
09[WAREHOUSE]WAREHOUSE: SHIP DAY, P1 ships 3 of 4 items (one out of stock); capture ₦32,000
10[SUPPORT]SUPPORT: three days later, Ngozi returns P1; Bisi refunds what the refund screen offers
11[SUPPORT]SUPPORT: report
Start of loose columns scenario. 11 steps.
-- 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

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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

A payment is one state with a short list of legal moves: authorised to captured, voided or expired; captured to partially or fully refunded; nothing else. Each move checks the state and a running total (captured ≤ authorised, refunded ≤ captured) in the same write, and an authorisation carries its expiry as data. A refused move changes nothing.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: SETTLEMENT FLOAT →