Payment Gateways — PayU & Razorpay
One gateway abstraction, six payment channels, and a new gateway is one strategy class away.
- Role
- SDE 1 – Backend (Java)
- Company
- Standard Chartered Research and Technology (SOLV)
- Period
- Jan 2024 – Present
- Stack
- Java · Dropwizard · MySQL · Redis · REST · Datadog
Production metrics · 30-day window · Sep 2026
Context
All money movement goes through a single payment-gateway service. Checkout, credit repayments and delivery-time collections all call it to create payment orders, and it handles completion, status checks, refunds and callbacks. It integrates PayU, Razorpay, Cashfree and BharatPe, plus in-app UPI.
The problem
Each gateway has its own API, statuses, hashing or signing, callback format and quirks. Callers such as checkout and credit should not care which gateway is in use. Payments must also complete correctly when the app and the gateway’s webhook both try to finish the same payment, when callbacks arrive late, or when they arrive twice.
Constraints
- Adding or swapping a gateway must not require changes in calling services.
- Merchant secrets must stay server-side.
- Completion must be safe under concurrency and at-least-once callbacks.
- Different tenants use different gateway accounts.
Design
- Strategy + Factory. Every gateway implements one transaction-strategy interface: create order, complete order, check status, refund, refund status, payment link, QR. A factory picks the strategy for each transaction. Callers speak one internal contract and statuses are normalized, so a new gateway ships as one strategy class.
- Per-tenant credentials. Stored encrypted (AES) and resolved per client at call time.
- PayU. SHA-512 request hashing (payment, mobile-SDK and VAS hashes) and a server-side dynamic-hash API, so the merchant salt never reaches the client. The app receives only the hashes it needs. Amounts are converted from paisa to PayU’s rupee format with fixed scale and rounding.
- Razorpay. Six channels in one integration: orders, UPI intent, single-use dynamic UPI QR, 24-hour payment links, eNACH recurring mandates, and refunds. Orders are created in paisa with our transaction id as the receipt.
- Safe completion. Completing a payment takes a Redis distributed lock keyed on the payment, polls the gateway with backoff (up to 9 attempts) until a terminal status, then captures the payment if it is only authorized. This is deferred capture.
- Audit-first S2S webhooks. Every raw callback is persisted in its own isolated transaction before any validation, so there is always a record of exactly what the gateway sent. Then mandatory fields are validated, the status is mapped, and a terminal-state guard stops a late or duplicate callback from downgrading a payment that already succeeded.
- Late-callback and anomaly alerts. A P0 alert fires if an authorization webhook arrives more than 15 minutes late, and if an order ever shows both a success and a refund.
Key decisions & trade-offs
Results
- ~563K payment orders/month created at 99.56% success.
- 99.8% success across ~21K payment completions/month.
- ~325K Razorpay webhook events/month and ~18K PayU callbacks/month processed.
- ~130K PayU hash-signing requests/month served at 100% availability.
Tech used
- Java
- Dropwizard
- MySQL
- Redis
- PayU
- Razorpay
- Cashfree
- Datadog