7
← Back to Pattern Arsenal
structural

Adapter

must know

Stage 1: Problem

Problem

You are building a payment processing service. Your system has a clean internal interface:

typescript
1interface PaymentGateway {
2 charge(amount: number, currency: string, customerId: string): Promise<ChargeResult>;
3 refund(chargeId: string, amount: number): Promise<RefundResult>;
4 getCharge(chargeId: string): Promise<ChargeDetails>;
5}

Your entire codebase — checkout, subscriptions, invoicing — depends on this interface.

Now you need to integrate three payment providers:

Stripe has methods like paymentIntents.create({ amount, currency, customer }) and returns { id, status, amount }.

Razorpay uses orders.create({ amount, currency, receipt }), then payments.capture(paymentId, amount), and returns { id, status, amount } — but amount is in paise (1/100 of a rupee), and it requires a two-step flow (create order, then capture).

PayPal uses a completely different model: orders.create({ intent, purchase_units }), then orders.capture(orderId), with nested response objects and amount as a string "10.00".

Each provider has:

  • Different method names and signatures
  • Different data formats (amount in cents vs paise vs string)
  • Different response shapes
  • Different error formats
  • Different flows (single-step vs two-step)

Challenge: How do you integrate all three without polluting your codebase with provider-specific code? And how do you make it easy to swap providers or add a fourth one (say, Adyen) later?


Related Patterns

Confused With a Similar Pattern?

1 / 10 — Problem