Design a Payment Gateway System

Difficulty: EASYCategory: LLDFrequency: 1Asked On 2025-06-13

Problem Statement

Design the low-level system for a Payment Gateway that allows customers to make online payments to merchants using different payment methods like credit/debit cards, UPI, or wallets.

You are expected to design the class structure, interfaces, and object interactions needed to support the following use cases:


🔷 Functional Requirements:

1.Initiate a Payment:
•A customer can make a payment to a merchant for a given amount using a selected payment method.
•The system should support Card, UPI, and Wallet as payment methods.
2.Process a Payment:
•Route the request to the appropriate payment processor.
•Simulate communication with external bank/payment processors.
3.Check Payment Status:
•Track and return the current status: PENDING, SUCCESS, FAILED.
4.Refund Payment:
•Allow a merchant to initiate a full or partial refund for a previously successful transaction.
5.Idempotency Handling:
•Ensure duplicate payment requests with the same reference are not processed multiple times.


🔷 Constraints & Assumptions:

•You can assume there are external services for banks or wallets which return random success/failure.
•No need to handle concurrency/threading, but design should be extensible and testable.
•No database or persistence layer is required — you can simulate in-memory data storage using collections.
•Focus on object-oriented principles, design patterns, and solid abstractions.


🔶 Expected Design Elements:

•Use interfaces or abstract classes to define the behavior of different payment methods.
•Use appropriate design patterns (like Strategy for payment processing).
•Ensure Open/Closed Principle is followed for adding new payment types.
•Include classes like:
•PaymentGateway
•PaymentProcessor (interface or abstract class)
•CardProcessor, UPIProcessor, WalletProcessor
•Payment, Transaction, Refund, etc.


🔷 Bonus (Optional):

•Add Webhook Notification Service to simulate sending payment success/failure updates to merchants.
•Add Fraud Check Service for validating high-value transactions.


Solution

Comments