All work
Production

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
~563K
payment orders / month
99.56%
order-creation success
99.8%
payment completion (~21K / month)
6
payment channels

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.
Callback vs webhook race
App callbackGateway webhook0s0.5s1s1.5s2s2.5s3sread: pendingread: pendingcompletecompleteorder #1order #2
Result: Two orders

t=2.1s · Gateway webhook: Webhook places order #2. Duplicate order. Result: Two orders.

  1. t=0.2s App callback: App callback reads the payment: not completed.
  2. t=0.5s Gateway webhook: Gateway webhook reads the payment: also not completed.
  3. t=1.0s App callback: App callback completes the payment.
  4. t=1.3s Gateway webhook: Webhook completes the same payment again.
  5. t=1.7s App callback: App callback places order #1.
  6. t=2.1s Gateway webhook: Webhook places order #2. Duplicate order.
The app and the webhook race to complete the same payment. The lock and the terminal-state guard make the outcome deterministic.
Payment gateway abstraction Checkout, credit and delivery-collections callers all use one gateway contract; a factory picks the matching strategy for PayU, Razorpay, Cashfree, BharatPe or in-app UPI, supported by per-tenant encrypted credentials and status normalization. callers strategies Checkout Credit Delivery collections Gateway contract Factory PayU Razorpay Cashfree BharatPe In-app UPI Status normalization Per-tenant encrypted credentials
Gateway strategy abstraction

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

Back to all work