Card Payments With Tips and Nightly Batch Capture for a Café Chain
System DesignModel answerLast updated
Design a card-present payment flow for a multi-location café chain: hold at swipe, tip added later, and a scalable nightly capture per location with safe retries and reconciliation.
You are designing the payment backend for a café chain's point-of-sale terminals. The payment flow works like this:
- At checkout, the customer's card is authorized for the purchase price. This places a hold but moves no money.
- After the fact, often on a separate screen while the transaction is still open, the customer may enter a tip. The tip is recorded as an adjustment to the original authorization.
- Money actually moves only when the location's nightly settlement runs. All finalized transactions for that location are captured together, using the purchase amount plus the tip.
Treat the card network and the payment processor as external black boxes that you call. You are not designing them.
Points to clarify first
State your assumptions about the following (or ask):
- Scale: number of locations, terminals, and transactions per day, and the length of the nightly window.
- Whether all locations share one cutoff time or each closes on its own local clock.
- Until when a tip may change, and what happens to a tip that arrives after the batch has gone out.
- Whether the processor takes a settlement file, per-transaction API calls, or both, and whether it supports idempotency keys or batch references.
- Whether paying out to each location is in scope.
Part 1: Authorization and tip adjustment
Design the data model and API for authorizing a swipe and later attaching a tip. Terminal connections are unreliable, so every call may be retried. Cover:
- The lifecycle states of a transaction (authorized, tip-adjusted, captured, and so on) and what each state stores.
- Idempotency for both the authorize call and the tip-adjust call.
- How to handle a processor timeout on authorization without placing two holds on the same card.
Part 2: Making nightly settlement scale
When a location closes for the day, its completed transactions must be captured. Design this so it keeps fitting in the nightly window as the chain adds locations and volume, and so a crash mid-run can neither lose a transaction nor charge it twice. Cover:
- How you partition the work so it parallelizes as the chain grows.
- Exactly-once submission to the processor, including what happens when a worker dies mid-batch.
- Reconciliation: how you detect a transaction where the processor's report disagrees with your records, and what a location operator sees.
Your answer should be a structured design discussion with the trade-offs you are making, not just a list of components.
Hints
Hint 1
Generate identifiers on the client for each physical action (one per swipe, one per tip-entry attempt) and reuse them on every retry, then enforce uniqueness on them server-side and pass them to the processor.
Hint 2
Avoid modeling nightly settlement as one big job per location. Think about the smallest independently retryable unit, and how a lease or visibility timeout lets another worker take over from a crashed one.
Hint 3
Money discrepancies should not be auto-corrected. Think about what the processor's next-day report is compared against, where mismatches go, and what a tip arriving after the cutoff should do.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Assumptions
I would state these up front. Say ~500 locations, ~3,000 terminals, ~300k transactions per day with an evening peak, a per-location local-time cutoff (e.g. 11 pm local), and a nightly window of a few hours. The processor supports idempotency keys on capture and also accepts a settlement file. Payouts to locations are out of scope. These numbers matter mostly because they show that the nightly run is bursty: the whole day's volume is pushed into a short window, so throughput must scale horizontally.
Part 1: Authorization and tips
States
AUTHORIZED -> TIP_ADJUSTED -> SUBMITTED -> SETTLED or FAILED
(A tip-free transaction can go straight from AUTHORIZED to SUBMITTED.) Each record carries: transaction id, location id, terminal id, purchase amount, current tip amount (default 0), processor authorization reference, state, timestamps, and the idempotency data described below. Transitions are enforced with conditional updates (UPDATE ... WHERE state = 'AUTHORIZED'), so a stale or duplicated request cannot move a transaction backwards.
Idempotent authorization
The terminal generates a transaction id once per physical swipe and sends it on every retry. The payment service:
- Inserts a row keyed by that id with a unique constraint. If the row exists, it returns the stored result instead of calling the processor again.
- Passes the same id to the processor as its idempotency key.
This prevents two holds from a flaky connection.
Handling a processor timeout
A timeout means the outcome is unknown, not failed. Mark the row AUTH_PENDING, then retry with the same key (the processor returns the original result if it already placed the hold) or query the processor by that reference. Never generate a fresh key for a retry. If the outcome cannot be determined within a bounded time, a background job resolves it, and as a last resort voids the authorization. The terminal shows "processing" and not "declined" meanwhile.
Idempotent tip adjustment
set_tip(transaction_id, tip_cents, adjustment_id), where adjustment_id is created once per tip-entry attempt. The service stores the latest applied (adjustment_id, tip_cents):
- Same
adjustment_idagain: no-op, return the recorded tip. - New
adjustment_id: the customer changed their mind; overwrite.
Because the tip is an overwrite, a replay is harmless here, but keying by attempt generalizes to incremental adjustments, and it prevents an old delayed retry from overwriting a newer tip if you also store a monotonic attempt sequence from the terminal.
Part 2: Scaling nightly settlement
Partitioning
Do not run one job per location. Settlement parallelizes across locations and across transactions, so use a work queue of per-transaction capture units:
- At each location's cutoff, a scheduler locks tips for that location and enqueues one unit per finalized transaction.
- A chain-wide pool of workers drains the queue. Growth is handled by adding workers, so the window stays roughly constant if worker count tracks volume.
- Use a rate limiter per processor account so scaling workers does not trip processor throttling. This, not worker count, is often the real ceiling.
- Stagger cutoffs by time zone, which naturally flattens load.
Exactly-once effect
True exactly-once delivery does not exist; the goal is at-least-once delivery plus idempotent capture:
- A worker claims a unit with a lease and visibility timeout.
- It calls capture with the transaction's idempotency key.
- It marks the unit
SUBMITTEDonly after the processor confirms.
If the worker crashes after the call but before the mark, the lease expires, another worker retries, and the key makes the second call a no-op at the processor. Nothing vanishes because the unit stays in the queue until confirmed; nothing is double-charged because of the key. Units that fail repeatedly go to a dead-letter queue and surface to operators.
Grouping for the processor
If the processor needs a settlement file, assemble one per location per night incrementally as units complete, so file building overlaps with the tail of capture work. Give the file a deterministic batch reference (location + business date) so resubmission is safe.
Reconciliation
The next day, compare the processor's settlement report with our records, transaction by transaction. Flag:
- amount mismatches,
- transactions we marked settled that the processor has no record of,
- transactions the processor has that we do not,
- late declines or chargebacks.
Mismatches go to an exceptions queue, not an automatic fix, because money discrepancies need a human decision. The operator view for a location shows the business date, transaction id, our amount versus the processor's, the likely cause, and actions (retry, adjust, write off). Track aggregate metrics too: totals per location per night, unmatched count, and age of open exceptions.
Late tips
Tips lock at a fixed time before the location's run (e.g. 30 minutes), and the terminal UI reflects it. A tip after the lock is either rejected or recorded as a separate adjustment settled the next night. It is never silently dropped or applied to the wrong batch.
Trade-offs
- Per-transaction units cost more coordination than one job per location, but a lost worker means redoing a few leased units instead of a whole batch. This is worth it once batches are large enough that a restart risks the window.
- Strict tip cutoff simplifies correctness but slightly annoys late tippers; the next-night adjustment path softens that.
- Manual reconciliation is slower than auto-correct, but safer for money.
Common mistakes
- Generating a new idempotency key on each retry.
- Treating a timeout as a failure and re-authorizing.
- Marking work done before the processor confirms.
- Using one monolithic job per location, so a single failure restarts everything.
- Ignoring processor rate limits when scaling workers.
- Silently overwriting reconciliation mismatches.