checkoutOs
Core concepts

Idempotency

Why POST /payments requires an Idempotency-Key, and how duplicate orders are rejected.

Creating a payment costs money and creates upstream state. A retried request that creates a second order is a double charge, so POST /payments requires an Idempotency-Key header.

The header

curl -X POST https://api.checkoutos.in/payments \
  -H 'Content-Type: application/json' \
  -H 'Idempotency-Key: 3f7d2a1e-9c4b-4f6a-8d2e-1b5c7a9e3f01' \
  -d '{"amount":50000,"currency":"INR","orderId":"order_abc123"}'

Generate a UUID v4 once per payment attempt and reuse it for every retry of that attempt.

SituationResult
Header omitted400 MISSING_IDEMPOTENCY_KEY
Not a valid UUID400 INVALID_IDEMPOTENCY_KEY
Same key, same payloadThe original response is replayed
Same key, different payload400 IDEMPOTENCY_KEY_REUSED
Same key, first request still running409 REQUEST_IN_PROGRESS

Records are retained for 24 hours (chk:idem:{key}, TTL 86400s).

Two layers of protection

Idempotency keys stop the same attempt from being charged twice. They do not stop two different attempts from colliding on one order, so orderId is guarded separately.

A duplicate orderId with a different amount or currency is rejected regardless of the idempotency key:

CodeStatusWhen
ORDER_ID_AMOUNT_MISMATCH409orderId exists with a different amount
ORDER_ID_CURRENCY_MISMATCH409orderId exists with a different currency
ORDER_CREATION_IN_PROGRESS409Another request holds the reservation for this orderId

Reservations and crash recovery

Before calling the gateway, the service reserves chk:pay:by-order:{orderId} with a 30-second TTL. The TTL is the crash-recovery bound: a process that dies between reserving and saving blocks that orderId for at most 30 seconds.

ORDER_RESERVATION_TTL_SECONDS  = 30
IDEMPOTENCY_STALE_TIMEOUT_MS   = 30000
IDEMPOTENCY_TTL_SECONDS        = 86400
IDEMPOTENCY_MAX_RETRIES        = 3

Why 30 seconds, twice

The reservation TTL must exceed the 10-second gateway HTTP timeout, so a slow-but-successful create cannot have its own reservation expire underneath it. It is deliberately the same 30s as the idempotency stale timeout — one recovery window to reason about, not two.

Record states

StatusMeaning
IN_PROGRESSClaimed; the original request has not finished. response is absent.
COMPLETEDFinished; response holds the body to replay.

A check against the store returns one of three results:

type IdempotencyCheckResult =
  | { type: 'MISS' }
  | { type: 'HIT'; response: unknown }
  | { type: 'IN_PROGRESS' };

A caller may only release a claim it created — release requires the requestHash, so one request can never release another's record.

Refunds are not idempotent in V1

POST /payments/:chkId/refund has no Idempotency-Key yet. A retried refund request can create a second refund. Refund idempotency is planned for V1.1; see the roadmap.

On this page