Requirements
A payment gateway processes card transactions between merchants and issuing banks. Core flow: customer enters card details → merchant sends authorization request → gateway routes to card network (Visa/Mastercard) → issuing bank approves/declines → response returned in < 2 seconds. Additional responsibilities: settlement (actual money movement), refunds, idempotency (no double charges), fraud detection, and PCI-DSS compliance. Scale: Stripe processes hundreds of billions of dollars annually. PayPal handles ~40 million transactions per day. Visa's network peaks at 65,000 transactions per second globally. This is one of the most detail-rich system design questions — correctness and reliability matter more than raw scale.
Authorization Flow
Step-by-step authorization: (1) Customer submits payment on merchant checkout. JavaScript SDK tokenizes the card (card number → token) client-side — raw card data never reaches the merchant’s server. (2) Merchant server sends authorization request to the gateway API: {token, amount, currency, merchant_id, idempotency_key}. (3) Gateway decrypts the token, retrieves card data from its secure vault (PCI-DSS zone). (4) Gateway enriches the request with fraud signals (device fingerprint, IP geolocation, velocity checks). Fraud score computed by ML model in < 50ms. (5) Gateway constructs an ISO 8583 message and routes to the appropriate card network (Visa/Mastercard/Amex) via a dedicated leased line or Mastercard/Visa APIs. (6) Card network routes to the issuing bank. Bank checks: card validity, available credit, fraud rules. Approves or declines. Response back to gateway in < 1.5 seconds end-to-end. (7) Gateway records the authorization (auth_code, approved amount, expiry) and returns to merchant. Merchant fulfills the order. The authorization hold reserves funds but does not move money yet.
Idempotency and Double-Charge Prevention
The most critical reliability requirement: a payment must be processed exactly once even under retries, network failures, and timeouts. Idempotency keys: every payment request includes a client-generated idempotency_key (UUID). The gateway stores a mapping: idempotency_key → {result, created_at} in a distributed store (Redis or database). On each request: check if the key exists. If yes: return the stored result immediately without reprocessing. If no: process and store the result. TTL: idempotency keys expire after 24 hours (Stripe’s default). Database-level protection: unique constraint on (merchant_id, idempotency_key). If two concurrent requests race: the second gets a unique constraint violation and returns the stored result. Idempotency at every layer: the gateway must also be idempotent with the card network. Network timeouts require retrying with the same transaction ID to avoid double charges. The card network uses the transaction ID for deduplication.
Settlement and Reconciliation
Authorization ≠ settlement. Authorization holds funds; settlement moves money. Settlement timing: T+1 or T+2 (1-2 business days after transaction). Batch settlement: the gateway sends a batch of authorized transactions to the card network at end of day. The card network debits the issuing bank and credits the acquiring bank. Acquiring bank credits the merchant’s account (minus interchange fees). Reconciliation: every transaction has a corresponding settlement record. Daily reconciliation job: match authorization records against settlement records. Mismatches: uncaptured authorizations (not settled), settled without authorization (fraud), amount mismatches. Reconciliation runs nightly; alerts fire on discrepancies. Refunds: the gateway credits the customer’s account by reversing a previous settlement. Partial refunds are supported (refund_amount ≤ original_amount). Chargebacks: customer disputes a charge with their bank. Bank reverses the payment. Gateway notifies the merchant who can provide evidence to contest. Chargeback rate > 1%: Visa/Mastercard may terminate merchant account.
Fraud Detection Architecture
Fraud detection runs synchronously in the authorization path (< 50ms budget). Feature signals: transaction amount vs. merchant category typical amount, velocity (how many transactions on this card in the last hour/day), device fingerprint (new device for this cardholder?), IP geolocation (IP country ≠ billing address country?), time of day, merchant risk category (travel = high risk, grocery = low risk). ML model: gradient boosted trees (XGBoost/LightGBM) trained on historical fraud labels. Inference in < 20ms at the gateway. Score thresholds: score 0.7: decline. Rules engine alongside ML: hard rules for known fraud patterns (velocity > 5 transactions in 1 min: block). Rules are faster to update than retraining a model. Feedback loop: chargebacks and fraud reports are labeled and fed back into model retraining (weekly batch job). Feature store: precomputed features (rolling counts, averages) are stored in Redis with TTL matching the window (1-hour window: 1-hour TTL).
Interview Tips
- Idempotency is the most important reliability property — explain it in depth with the idempotency key pattern.
- Distinguish authorization (hold) from capture (settlement) — many candidates conflate them.
- Fraud detection must be synchronous and fast (< 50ms) — an async approach would allow fraud to complete before detection.
- PCI-DSS compliance shapes the architecture: card data only in the vault, tokenization on the client, no raw PANs in logs.
See also: Stripe Interview Prep
See also: Coinbase Interview Prep
See also: Shopify Interview Prep
See also: Uber Interview Prep