Design a Payment System

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-04-12

Problem Statement

Design a Payment System that allows users to pay merchants securely and reliably. The system should support multiple payment methods (UPI, cards, net banking, wallets) and ensure idempotency, consistency, and fault tolerance. It must scale to millions of daily transactions while maintaining low latency and compliance with financial regulations.


✅ Functional Requirements

1.<span></span>Users can initiate a payment to a merchant.
2.<span></span>Support multiple payment methods:
3.<span></span>Credit/Debit Cards
4.<span></span>Net Banking
5.<span></span>UPI / Wallets
6.<span></span>Handle transaction states: Initiated → Pending → Success/Failure.
7.<span></span>Ensure idempotency (no double charges if a retry happens).
8.<span></span>Refunds and reversals must be supported.
9.<span></span>Merchants should be able to check transaction status via APIs.


⚙️ Non-Functional Requirements

1.<span></span>High Availability → System must be up 24x7.
2.<span></span>Scalability → Handle peak loads (e.g., festive sales with millions of transactions/min).
3.<span></span>Reliability → No payment should be “lost” in transit.
4.<span></span>Security → PCI DSS compliance, tokenization, encryption.
5.<span></span>Low Latency → End-to-end payment completion < 2-3 seconds.
6.<span></span>Observability → Logs, metrics, fraud detection, monitoring dashboards.


🔐 Constraints

1.<span></span>Payments involve external systems (banks, UPI servers, card networks).
2.<span></span>System should gracefully handle timeouts, retries, and duplicate requests.
3.<span></span>Must comply with regulatory requirements (e.g., RBI in India, PCI DSS globally).


🧱 Key Components to Design

1.<span></span>API Gateway – Expose payment APIs securely to merchants.
2.<span></span>Payment Orchestrator – Core service that manages payment lifecycle and state transitions.
3.<span></span>Transaction Database – Store transaction metadata (SQL/NoSQL).
4.<span></span>Idempotency Layer – Ensure safe retries.
5.<span></span>Queue/Retry Mechanism – For pending transactions, retries, reconciliation.
6.<span></span>Bank/PSP Integrator – Connect with banks, card networks, UPI switches.
7.<span></span>Notification Service – Send updates to merchants and users.
8.<span></span>Reconciliation Service – Verify settlement with banks at the end of the day.
9.<span></span>Monitoring + Fraud Detection – Alerting, anomaly detection, fraud checks.


🧪 Example Flow

<div><div><br /></div><div>1. User initiates payment via merchant checkout page. </div><div>2. Merchant calls Payment API → creates a transaction (status: INITIATED). </div><div>3. Payment Orchestrator routes request to appropriate PSP/bank (via integrator). </div><div>4. Waits for success/failure callback (status: SUCCESS/FAILURE). </div><div>5. Updates merchant via webhook/notification. </div><div>6. If timeout → retry or mark as PENDING for reconciliation. </div><div>7. End-of-day reconciliation ensures consistency with bank settlements. </div></div>


Solution

Comments