Skip to content
SD-32 · Back to the write-up

Design a Card Payment Processing

Save each payment step behind an idempotency key, authorize through a token in a card-data zone, reverse every unknown, and book every cent twice.

Follow a request

Merchant
Edge
Payment services
Card zone, data and networks
Checkout scriptShopper's browser
Merchant serverRetries with a key
API gatewayKeys + idempotency
Payment APIState machine
Risk scoring50 ms budget
Payments DB16 shards
Card vaultTokens, HSM keys
Network adapter16 acquirer links
Card networksAcquirers + banks
LedgerDouble entry
SettlementNightly + recon
Webhook workersFrom the outbox
1merchant → gw · POST /v1/paymentsThe merchant asks for $49.99 with the token and Idempotency-Key: ord-881-a1.1 / 11

The numbers, at a glance

Where an authorization's time goesTypical, in milliseconds
The card-data zone is tinyMachines that ever see a card number
So PCI audits cover a few machines, not the whole system.
Merchant calls that are retriesAbout 1 in 100 at busy times
1 retry: must return the saved answer99 first tries

An authorization, in order

Happy path. On a timeout, the adapter sends a reversal before step 9 and the result is declined.

Merchant
Gateway
Payment API
Payments DB
Adapter
Vault
Network
1 POST /payments + Idem-Key
2 new key, payment id
3 created + key, then authorizing
4 authorize(token)
5 detokenize
6 card number
7 0100 auth, trace no
8 0110 approved
9 approved, auth code
10 authorized + outbox
11 201 authorized
12 201 authorized
RequestResponseAsync, does not block

When the bank does not answer

The decision this question is really about.

Retry the auth
Unsafe
auth → timeout
auth again
charged twice
+ Usually works on the second try.− Two holds if the first was approved.
Call it declined
Loses money
auth → timeout
status = failed
charged, nothing shipped
+ Fast, clear answer.− The bank may have approved it.
Save, reverse, then answer
Our pick
save authorizing
auth → timeout
reversal until acked
declined, same answer on retry
+ Every outcome is known; retries are safe.− More states and a recovery job.

How it grows

Add a box only when a number forces you to. Highlighted boxes are new at each stage.

Stage 1 · ≈ 100 payments/sMake it work
Merchant
App server
DatabaseCard network
One server, one database, one link. Card numbers in the database.
Stage 2 · ≈ 1,000 payments/sSafe to retry
Checkout scriptMerchant
Gateway
Payment API × 2
Payments DBLedgerVault + adapter
Idempotency keys, a vault and adapter in a card-data zone, a ledger.
Stage 3 · ≈ 5,000 payments/sScale the money path
Checkout scriptMerchant
Gateway
Payment API × 4Risk scoring
Payments DB × 16Ledger16 acquirer links
Webhook workers
16 shards, 16 links over 2 acquirers, outbox webhooks, fraud scoring.
Stage 4 · ≈ 10,000 payments/sClose the books
Checkout scriptMerchant
Gateway
Payment API × 8Risk scoring
Payments DB × 16Ledger + sub-accounts16 acquirer links
Webhook workersSettlement + recon
Split hot merchant accounts, nightly settlement and reconciliation.
Was this walkthrough useful?Your vote tells us what to improve next.