When you pay online, you usually just click a button and wait for confirmation. Behind this simple action, a system works quietly to move money from your bank to the service provider. This system is called a Payment Gateway.
What Is a Payment Gateway
A payment gateway is a bridge between your application and payment providers like Paytm or Razorpay.
-
Your app only needs to know it wants to process a payment.
-
The gateway handles how the request goes to the bank and how the response comes back.
-
This keeps your app code clean and focused on your main features.
Core Requirements
Functional Needs
-
Support multiple payment providers (Paytm, Razorpay, etc.).
-
Make it easy to add new providers in the future.
-
Follow a standard payment flow with proper checks and validations.
-
Handle errors and retries correctly.
Non-Functional Needs
-
Scale to handle millions of transactions.
-
Use a plug-and-play design so components can be added or removed easily.
-
Follow SOLID principles to keep the code clean, organized, and easy to maintain.
Patterns That Power the Design
Strategy Pattern
This pattern helps switch between different payment providers. For example, at checkout, you can pick UPI, card, or wallet. The system adapts to the choice without changing the rest of the code.
Template Method Pattern
This pattern defines the fixed steps of a payment: validate, process, and confirm. Each provider can customize parts of the steps, but the overall flow stays the same.
Proxy Pattern
This pattern helps handle failures quietly. For example, if there is a network timeout, the system can retry automatically without asking the user to click again.
Factory + Singleton Pattern
-
Factory creates the specific payment gateway instance you need, like Paytm or Razorpay.
-
Singleton ensures there is only one shared instance of the gateway handling all payments, keeping the system consistent.
Handling Retries
Retries are important in payment systems. If a transaction fails partway, the system should try again safely:
-
Linear Retry: Try again after a fixed interval, like every 3 seconds.
-
Exponential Backoff: Try again with increasing delays, like 1 second, then 2 seconds, then 4 seconds, and so on.
This ensures the system gives the transaction multiple chances to succeed without overwhelming the payment provider.
Recurring Payments
For subscriptions like Netflix, the system automatically charges payments at regular intervals. It keeps logs for auditing and uses retry logic to make sure payments go through even if the first attempt fails.
UML Diagram

Example
Imagine you are ordering food on Swiggy and choose Paytm to pay.
-
Swiggy first checks and validates your order.
-
Then it sends the payment request to Paytm.
-
Finally, it confirms the payment back to you.
Behind this simple click, all the design patterns we discussed Strategy, Template Method, Proxy, Factory, and Singleton are working together quietly to make the process smooth and reliable.
Summary
A payment gateway is a system that handles online payments by connecting your app to providers like Paytm or Razorpay. It manages the full process of validating, processing, and confirming payments so your app does not have to deal with the details. Using patterns like Strategy, Template Method, Proxy, Factory, and Singleton makes it simple to switch providers, retry failed payments, and support recurring payments while keeping the system reliable and easy to maintain.