Skip to content

Lld Payment Gateway

Low Level Design (LLD) is concerned with class-level structure — the objects, data structures, algorithms, and design patterns that make a subsystem work correctly. It is distinct from High Level Design (HLD), which covers infrastructure topology, service boundaries, and data flow between systems.

Where HLD answers “what services exist and how do they communicate?”, LLD answers:

  • What classes exist inside a service?
  • What are their responsibilities and relationships?
  • How does data flow through a method call chain?
  • Which algorithms and data structures handle the core logic?
  • Which design patterns enforce correctness constraints?

Why Design Patterns Matter in Payment Systems

Section titled “Why Design Patterns Matter in Payment Systems”

Payment systems operate under constraints that most software does not:

  • Never double-charge. A customer must be billed exactly once per transaction, regardless of retries, network failures, or system restarts.
  • Never lose a transaction. Every operation that touches money must be durable, auditable, and recoverable.
  • Every state transition must be valid. A transaction cannot jump from INITIATED to SETTLED without passing through AUTHORIZED and CAPTURED.

Design patterns are not decorative — they are the mechanism by which these constraints are enforced in code. A State pattern makes illegal state transitions unrepresentable. A Command pattern makes every operation replayable and auditable. A Chain of Responsibility pattern ensures fraud filters run in the correct order and can short-circuit safely. Choosing the right pattern is a correctness decision, not a style decision.


The table below lists every pattern used across the LLD documents, the subsystem it appears in, and the reason it was chosen.

PatternCategoryUsed inWhy
StateBehavioralTransaction EngineEnforce valid state transitions; prevent invalid operations
CommandBehavioralTransaction Engine, SettlementEncapsulate operations for retry, undo, audit
Template MethodBehavioralTransaction Engine (card/ACH/wallet)Fixed processing skeleton, variable steps per payment type
Chain of ResponsibilityBehavioralFraud EngineOrdered fraud filter pipeline with short-circuit
StrategyBehavioralFraud Engine (scoring), Processor IntegrationSwap algorithms/processors without changing calling code
ObserverBehavioralFraud EngineDecouple fraud event notification from detection
AdapterStructuralProcessor IntegrationWrap legacy/incompatible processor APIs
FacadeStructuralProcessor Integration, API layerSingle entry point hiding subsystem complexity
ProxyStructuralStored CredentialsControl access + lazy decryption of encrypted payment profiles
DecoratorStructuralAPI layerStack cross-cutting concerns (logging, rate-limit, idempotency)
SingletonCreationalHSM ClientSingle shared connection pool to hardware security module
IteratorStructuralSettlement PipelineStream large transaction batches without loading all into memory

FilePatternsWhat it covers
01-transaction-engine-lld.mdState, Command, Template MethodTransaction lifecycle state machine, operation encapsulation, multi-payment-type processing pipeline
02-fraud-engine-lld.mdChain of Responsibility, Strategy, ObserverFraud filter chain, scoring algorithm swap, event notification decoupling
03-processor-integration-lld.mdStrategy, Adapter, FacadeProcessor routing, legacy API wrapping, unified payment facade
04-settlement-pipeline-lld.mdIterator, CommandMemory-safe batch traversal, retry-aware settlement step encapsulation
05-stored-credentials-lld.mdProxy, Singleton, DecoratorEncrypted profile access control, HSM singleton, API request pipeline

Start with 01-transaction-engine-lld.md. The transaction engine is the heart of the system — every payment flows through it, and it introduces the three most fundamental patterns (State, Command, Template Method) that recur throughout the rest of the series.

After that, each document is self-contained. If you are designing a specific subsystem, you can jump directly to the relevant file without reading the others first.

Every pattern section in each document follows the same structure:

  • Intent — the problem the pattern solves in one sentence
  • Class diagram — the key classes and their relationships
  • When to use — the specific conditions that justify applying this pattern
  • Java implementation — a worked example drawn from the payment gateway domain
  • Tradeoffs — what you gain and what you give up

Several patterns were considered during design and deliberately set aside.

PatternConsidered forReason not used
BuilderPaymentRequest constructionThe request object is simple enough to construct directly; Builder would add indirection without meaningful benefit
CompositeTransaction line itemsLine items are flat — they do not form a hierarchy, so Composite would misrepresent the data model
VisitorReporting engineDeferred; would apply if a reporting layer needed to traverse a heterogeneous transaction hierarchy, but that requirement does not exist yet
MediatorService coordinationFacade was sufficient for request-response flows; Mediator would introduce unnecessary indirection and complexity