Payment System Design, Step by Step
Charges a card exactly once despite retries and timeouts, using idempotency keys, and records every rupee in a double-entry ledger.
Loading the interactive diagram…
Payment System Design, Step by Step
Charges a card exactly once despite retries and timeouts, using idempotency keys, and records every rupee in a double-entry ledger.
Customer
- Checkout page
- Client · Hypothetical. The customer pressing Pay.
Front door
- Payments API
- Gateway · Hypothetical. The public door for pay requests, and for the processor's webhooks.
Moving money
- Payment service
- Service · Hypothetical. Runs each payment through its states: created, pending, captured or failed. Never charges twice.
- Ledger service
- Service · Hypothetical. Records every movement of money as balanced debits and credits.
- Card processor
- External · Hypothetical. The payment processor that talks to card networks and banks, and actually moves the money.
Data and events
- Ledger database
- Database · Hypothetical. Append-only entries. Nothing is ever edited or deleted.
- Payments database
- Database · Hypothetical. Each payment's state, and each idempotency key with the response it got.
- Payment events
- Queue · Hypothetical. "Payment captured", "payment failed": for any service that needs to know.
- Order service
- Service · Hypothetical. Ships the order once the payment is captured.
Back office
- Reconciliation job
- Service · Hypothetical. Every night, checks the ledger against the processor's settlement report.
Flow: Pay for an order
The happy path: save first, charge once, record the money in the ledger, then tell everyone else.
- Checkout page → Payments API (Pay ₹1,499 (k-5f1)). The customer presses Pay. The request carries the idempotency key k-5f1.
- Payments API → Payment service (Pay ₹1,499 (k-5f1)). The API passes it to the payment service.
- Payment service → Payments database (Save k-5f1, p-77 created). Before calling anyone, it saves the key and a new payment p-77 in one transaction.
- Payments database → Payment service (Saved). If anything crashes from here on, p-77 is on record.
- Payment service → Card processor (Charge ₹1,499 (p-77)). It asks the card processor to charge the card, passing p-77 as the processor's own idempotency key.
- Card processor → Payment service (Captured c-31). The bank approved it and the money is captured.
- Payment service → Ledger service (Record p-77 ₹1,499). The payment service tells the ledger money moved. The ledger accepts one entry per payment id, so a repeat after a crash can't record the money twice.
- Ledger service → Ledger database (Debit + credit ₹1,499). Two entries: owed by the processor +₹1,499, owed to the merchant +₹1,499. They balance, so they're accepted.
- Ledger database → Ledger service (Committed). The entries are permanent.
- Ledger service → Payment service (Recorded). The ledger confirms.
- Payment service → Payments database (p-77 captured + outbox). It saves the new state and, in the same transaction, an outbox row that becomes a "payment captured" event, along with the response for key k-5f1.
- Payments database → Payment service (Saved). State, saved response and event are all stored together.
- Payment service → Payment events (payment.captured p-77). The outbox relay publishes the event. The order service will pick it up and ship.
- Payment service → Payments API (Paid). The payment service answers.
- Payments API → Checkout page (Payment successful). The customer sees the confirmation.
Flow: The customer's phone retries
The answer to Pay got lost and the app sent it again. The same key returns the saved answer, and the card isn't charged twice.
- Checkout page → Payments API (Pay ₹1,499 (k-5f1)). The connection dropped before the answer arrived, so the app sends Pay again, with the same key k-5f1.
- Payments API → Payment service (Pay ₹1,499 (k-5f1)). The API passes it on, exactly as before.
- Payment service → Payments database (Seen k-5f1?). The payment service always looks the key up first.
- Payments database → Payment service (Yes: p-77, captured). This key already belongs to p-77, which is captured, and the response sent last time was saved with it.
- Payment service → Payments API (Paid (saved answer)). It returns the saved response unchanged. The processor and the ledger are never touched.
- Payments API → Checkout page (Payment successful). The customer sees success, and was charged once. Had the first try still been running, the service would answer "still processing" rather than start a second one.
Flow: The processor times out
The answer from the processor never comes. The payment waits as pending, and the processor's webhook settles it later, without a second charge. Shipping then follows in the background, from the event.
- Checkout page → Payments API (Pay ₹2,999 (k-8a2)). Another customer presses Pay.
- Payments API → Payment service (Pay ₹2,999 (k-8a2)). The API passes it on.
- Payment service → Payments database (Save k-8a2, p-78 created). Key and payment saved first, as always.
- Payments database → Payment service (Saved). On record.
- Payment service → Card processor (Charge ₹2,999 (p-78)). It asks the processor to charge.
- Card processor → Payment service ((timed out)). No answer within 10 seconds. The charge may or may not have happened: the dangerous case.
- Payment service → Payments database (p-78 pending). It records the payment as pending. It does not charge again; that's how customers get charged twice.
- Payments database → Payment service (Saved). Pending is a real, visible state.
- Payment service → Payments API (Processing). The payment service answers honestly that it doesn't know yet.
- Payments API → Checkout page (We'll confirm shortly). The customer sees "processing" instead of an error that would make them try another card.
- Card processor → Payments API (Webhook: p-78 captured). Seconds later the processor's webhook arrives: the charge did go through. The API checks the webhook's signature.
- Payments API → Payment service (p-78 captured). The API passes the news to the payment service. Had no webhook come, a background job would ask the processor about p-78.
- Payment service → Ledger service (Record p-78 ₹2,999). Now the money is real, so it goes in the ledger.
- Ledger service → Ledger database (Debit + credit ₹2,999). Balanced entries, appended.
- Ledger database → Ledger service (Committed). The entries are permanent.
- Ledger service → Payment service (Recorded). The ledger confirms.
- Payment service → Payments database (p-78 captured + outbox). Pending becomes captured, saved together with the outbox row for the event and the final answer for key k-8a2, exactly as in the normal path.
- Payments database → Payment service (Saved). If the app asks again with k-8a2, it now gets "paid".
- Payment service → Payment events (payment.captured p-78). The outbox relay publishes the event.
- Payment service → Payments API (Done). The payment service has handled the webhook.
- Payments API → Card processor (200 received). The API acknowledges the webhook. Processors resend webhooks until they get this, so a duplicate must be harmless: p-78 is already captured, and the ledger won't take it twice.
- Payment events → Order service (Ship order). The order service hears about it and ships; the customer gets their confirmation email.
Flow: Nightly reconciliation
Every night the ledger's totals are checked against what the processor says it paid. Background work: nobody is waiting on it.
- Reconciliation job → Ledger database (Captured totals, 10 Nov). The job has downloaded the processor's settlement report for 10 November. Now it asks the ledger for the same day.
- Ledger database → Reconciliation job (₹48,20,315.50, 3,214 txns). It matches every payment one by one. Any payment in one list but not the other is flagged for the finance team to look at.
HowItScales is not affiliated with the companies shown. Components marked Assumption or Hypothetical are not confirmed by the company.