Payment lifecycle
The state machine, its invariants, and why polling can never report success.
The happy path
1. Your server POST /payments → { paymentId, paymentUrl }
2. Your frontend redirect user to paymentUrl
3. User completes payment on the gateway page
4. Gateway fires webhook to /webhooks/:gateway
5. checkoutOs verifies signature, updates state, relays to WEBHOOK_RELAY_URL
6. Your server receives { paymentId: "chk_…", status: "SUCCESS" }Invariants
These hold across every gateway, and every new plugin must preserve them.
Polling never transitions to SUCCESS
GET /payments/:chkId can move a payment forward through intermediate states after the
10-second staleness window, but the path to SUCCESS is webhook-only. A gateway that
settles synchronously must post its confirmation into the webhook ingestion path rather than
being special-cased in the service — that way the state machine, the ID backfill and the relay
behave identically everywhere.
gatewayOrderIdis set at creation.gatewayPaymentIdstarts as an empty string and is backfilled only when a webhook arrives.- Refunds require a non-empty
gatewayPaymentId, so a webhook must precede any refund. Attempting one earlier returnsREFUND_NOT_ALLOWED. - Partial refunds stack: a
PARTIALLY_REFUNDEDpayment can receive further refunds up to the original amount. - Invalid status transitions are ignored with a warning, not errors. A late
payment.failedwebhook for an already-succeeded payment is a no-op.
Valid transitions
| From | Allowed next |
|---|---|
PENDING | PROCESSING, SUCCESS, FAILED, CANCELLED, EXPIRED |
PROCESSING | SUCCESS, FAILED, CANCELLED, EXPIRED |
SUCCESS, FAILED, CANCELLED, EXPIRED, REFUNDED, PARTIALLY_REFUNDED | terminal — no webhook transitions |
REFUNDED and PARTIALLY_REFUNDED are set by the refund service, not by a payment webhook.
Webhook relay
Once a webhook is verified and applied, checkoutOs relays a normalized event to
WEBHOOK_RELAY_URL.
{
"paymentId": "chk_a1b2c3d4e5f6a7b8",
"gateway": "razorpay",
"gatewayPaymentId": "pay_ABC123",
"gatewayOrderId": "order_ABC123",
"event": "payment.captured",
"status": "SUCCESS",
"amount": 50000,
"currency": "INR",
"raw": {}
}The relay is fire-and-forget with a 5-second timeout. A relay failure logs a warning and
does not fail the webhook request — the gateway still gets its 200, because a gateway
retrying forever because your own server is down is worse than a dropped relay you can
reconcile by polling GET /payments/:chkId.
The payload carries more than paymentId
The relay is { paymentId: chkId, ...event }, so gatewayPaymentId, gatewayOrderId and
the raw gateway payload are forwarded alongside it. Key your own records on paymentId; the
gateway IDs are informational.
The checkout page
GET /checkout/:chkId serves HTML, not JSON — a page carrying the Razorpay JS SDK for
Razorpay, or a 302 to Paytm's hosted payment page. The plugin decides which action to take
through getCheckoutAction().