Designing a Payment System (Stripe / Razorpay)
Difficulty: Advanced Topics: Payment Orchestration, Idempotency, Double-Entry Ledger, Settlement, Reconciliation, PCI Compliance Asked at: Google, Amazon, PhonePe, Razorpay, Stripe, Flipkart, Goldman Sachs · Prerequisites: Message Queues, Database Transactions, and Scalability
1. Understanding the Problem
A payment system orchestrates the movement of money between buyers, merchants, and banks. It processes a payment request (card charge, Unified Payments Interface (UPI) transfer, wallet debit), routes it to the appropriate payment network, handles success and failure, settles funds to the merchant, and maintains a bulletproof ledger for auditing. The hard parts: exactly-once processing (never charge a customer twice), handling partial failures across multiple external systems (bank timeouts, network drops), and reconciling millions of transactions daily across banks that disagree with your records.
💡 Exactly-once means a charge lands one time no matter how many times the request is sent or retried. It is the hardest guarantee in distributed systems because the network can always lose the *answer to a request that already succeeded. Learn more →*
Real examples: Stripe, Razorpay, PayPal, Square, Adyen.
2. Naive First Cut
flowchart LR
MERCHANT["Merchant App"]
API["Payment API"]
BANK[("Bank API")]
MERCHANT -->|"charge card"| API
API -->|"call bank"| BANK
Merchant calls API, API calls bank, returns success/failure.
Discussion
Newest first