Planning a card payment processing system
paymentscredit-cardspci-dsspsppayfacledgeridempotencyfraudfintech
"Build a credit card processing system" can mean three very different businesses, and the choice sets the timeline, the regulatory load and the architecture. This is the shape of the answer worked out for a system serving 2 million users: the decision first, then the parts of the design that prevent the classic money-losing bugs. The figures are planning estimates, not quotes.
First decision: merchant, PayFac or processor
| Option | What you build | Time to live | Regulatory load |
|---|---|---|---|
| A. Merchant on a PSP (Stripe, Adyen, Braintree) | Checkout, orders, ledger, webhooks | 1 to 3 months | PCI SAQ A (minimal) |
| B. Payment facilitator on a sponsor processor | Merchant onboarding, sub-merchant ledger, payouts, risk | 9 to 18 months | PCI DSS Level 1, KYC/AML, sponsor bank agreement |
| C. Processor/gateway with direct network links | All of B plus ISO 8583 connectivity and card-brand certification | 18 to 36 months, $5M to $20M+ | Level 1, certification with each brand, bank sponsorship, money transmitter licenses |
Start with A and keep the internal design (orchestrator, ledger, idempotency) ready to move to B.
Scale is not the hard part
2M users at ~5 transactions a month is ~10M a month: about 4 TPS on average and ~200 TPS at a 50x Black Friday peak. Design for 1,000 TPS and you still fit a well-run Postgres cluster plus stateless services. Correctness, security and compliance are much harder than throughput; do not build planet-scale sharding before the ledger is right.
Architecture in one picture
- Edge: CDN, WAF, rate limits; an API gateway that authenticates and enforces an
Idempotency-Keyon every mutating call. - Core (out of PCI scope): payments API, an orchestrator that owns the state machine, risk engine, ledger, routing, payouts, disputes, merchant onboarding, signed and replayable webhooks.
- Cardholder data environment (CDE): a separate network or cloud account holding only the tokenization vault (card numbers encrypted under HSM-held keys) and the network connector. The rest of the system sees tokens only.
- Async backbone: an event log (Kafka) fed through the outbox pattern; batch jobs for clearing, settlement and reconciliation.
Rules that prevent money-losing bugs
- Idempotency everywhere. Store client key + request hash + result for 24 to 72 hours; a retry returns the original response and never charges twice.
- A timeout is not a decline. No answer from the network moves the payment to
UNKNOWN, which triggers an automatic reversal or inquiry so no cardholder is left with an orphaned hold. - Outbox, never dual writes. The state change and its event commit in one database transaction; a relay publishes the event.
- Money is integer minor units (cents) plus an ISO 4217 currency code. Never floats.
- Double-entry, append-only ledger. Each journal's debits equal its credits; entries are never updated or deleted (corrections are reversing entries); balances are derived from entries; a nightly job proves all accounts sum to zero and match the bank. Reconciliation is a three-way match of ledger, processor reports and bank statements.
- State machine: CREATED -> (3DS) -> AUTHORIZING -> AUTHORIZED -> CAPTURED -> SETTLED, with DECLINED, VOIDED, REFUNDED and DISPUTED branches. Multi-step flows (split payments, marketplace payouts) are sagas with a compensating action per step.
Shrinking PCI scope
- Hosted fields, iframes or mobile SDKs send the card number straight to the vault, so app servers never see it.
- Never store CVV after authorization, or track data, or PINs. Use network tokens (Visa VTS, Mastercard MDES) for stored cards: higher approval rates and less breach exposure.
- Envelope encryption: a data key per record or batch, the key-encryption key inside an HSM, rotated yearly.
- Scrub card numbers in the logging library itself, and add a CI test that fails if a 13 to 19 digit Luhn-valid number shows up in logs.
- Above 6M card transactions a year you are PCI Level 1: annual QSA audit and Report on Compliance, quarterly ASV scans, yearly penetration test. 3-D Secure 2 is mandatory under EU/UK PSD2 and shifts fraud liability to the issuer.
Fraud, reliability, money
- Real-time risk inside ~50 ms: velocity rules per card/device/IP/merchant, BIN-country mismatch, AVS/CVV results, device fingerprint, an ML score. Outcomes: approve, decline, step up to 3DS, or manual review. Buy (Sift, Sardine, Stripe Radar) until you have your own labeled chargebacks.
- Merchant risk: underwriting, rolling reserves, delayed payouts for new merchants; card-brand chargeback thresholds sit around 0.9 to 1%.
- Two or more acquirers with smart routing (cost, approval rate, health) typically lift approvals 1 to 3%, worth more at scale than any infrastructure saving. Circuit breakers per route.
- Alert on any ledger imbalance other than zero and on an approval-rate drop of more than 2% over 5 minutes.
- Interchange and scheme fees (~1.5 to 2.5% of volume) are the largest cost, priced into the merchant rate; compliance audits run $100k to $250k a year; a B-scale team peaks around 15 to 25 people.
Gotchas
- Engage a QSA and a compliance lead at the start, not after building.
- Money transmitter licenses are per US state; operating under a sponsor bank or as a PayFac is how most avoid them.