Building a Central Payment Gateway with Razorpay
How we unified all payment flows across our platform using Razorpay as the central payment orchestration layer — reducing failures by 40% and cutting integration time in half.

Project Overview
Our platform handled payments across subscriptions, one-time purchases, and marketplace payouts — each using different libraries and ad-hoc integrations. This created a maintenance nightmare. We consolidated everything into a Central Payment Gateway (CPG) backed by Razorpay, giving us a single source of truth for all transaction flows, webhooks, and reconciliation.
System Architecture
The CPG sits between all internal services and Razorpay's API. It abstracts payment creation, status polling, webhook verification, and refund handling into a unified internal SDK. Services never call Razorpay directly — they go through CPG, which enforces retry logic, idempotency, and audit logging.

Payment Orchestrator
Routes payment requests to the correct Razorpay method (orders, subscriptions, payment links) based on context.
Webhook Processor
Verifies HMAC signatures, deduplicates events, and fans out to internal services via an event bus.
Idempotency Layer
Uses Redis to track in-flight and completed requests, preventing duplicate charges on retries.
Reconciliation Engine
Periodically syncs transaction states between our DB and Razorpay's settlement reports.
Implementation Details
Code Example
// Central Payment Gateway - Create Order
async function createOrder(payload: CreateOrderDto): Promise<RazorpayOrder> {
const idempotencyKey = `order_${payload.referenceId}`;
const cached = await redis.get(idempotencyKey);
if (cached) return JSON.parse(cached);
const order = await razorpay.orders.create({
amount: payload.amountInPaise,
currency: payload.currency ?? 'INR',
receipt: payload.referenceId,
notes: {
userId: payload.userId,
productId: payload.productId,
},
});
await redis.setex(idempotencyKey, 3600, JSON.stringify(order));
await auditLog.record({ event: 'ORDER_CREATED', orderId: order.id, ...payload });
return order;
}Agent Memory
Network failures can cause duplicate order creation if not handled. Pair Razorpay's `receipt` field with a Redis-backed idempotency check on your side to guarantee exactly-once semantics — especially critical for subscription renewals.
Workflow
A payment request flows from any internal service → CPG → Razorpay API. On completion, Razorpay fires a webhook → CPG verifies and processes it → publishes an internal event → downstream services (fulfillment, notifications, analytics) react independently.

Results & Impact
"Before the CPG, every team had their own Razorpay integration. Debugging a failed payment meant checking 4 different codebases. Now it's one place, one log, one truth."
Unified Observability
All payment events flow through a single service, giving us centralized logging, alerting, and dashboards for the first time.
Faster Incident Response
MTTR for payment incidents dropped from 45 minutes to under 10 minutes due to structured logs and webhook replay capability.
Easier Compliance
PCI-DSS scope was dramatically reduced — only CPG touches payment data, isolating risk from other services.
Team Velocity
New product teams integrate payments in hours using our internal SDK instead of reading Razorpay docs from scratch.
About the Author
Praveen Jogi
AI Context Engineer
Apex Neural
Building real-world AI with LLMs, RAG pipelines, and clean backends. Works with vector databases and intelligent agents for context-aware applications. Designs modular, scalable systems with secure implementation and reliable cloud deployment.
Contributors
Ready to Build Your AI Solution?
Get a free consultation and see how we can help transform your business.

