Skip to Content
StagePayDocs v2

Retries and Idempotency

How callback failures are determined, the retry schedule (1 minute to 24 hours, up to 8 times), manual resends, and preventing duplicate grants with X-StagePay-Delivery.

Failure determination

ResultOutcome
HTTP 2xxSuccess (delivered)
Any non-2xx status code (3xx, 4xx, and 5xx alike)Failure → retry
Connection failure, TLS error, 10-second timeoutFailure → retry
Callback URL not set / URL that is not http(s)Immediately failed (no retries)

Redirects (3xx) are not followed, so register the final address directly.

Retry schedule

If the initial attempt fails, StagePay retries up to 8 times at the intervals below (9 attempts in total).

RetryWait after previous attemptCumulative from initial attempt (approx.)
11 minute1 minute
25 minutes6 minutes
315 minutes21 minutes
41 hour1 hour 21 minutes
53 hours4 hours 21 minutes
66 hours10 hours 21 minutes
712 hours22 hours 21 minutes
824 hoursAbout 46 hours

If all 8 retries fail, the callback status becomes failed and it is no longer sent automatically. Because of the worker cycle, actual delivery times can be delayed by tens of seconds.

Manual resend

You can resend a failed callback from the console under Transaction details → Callbacks.

  • It is sent again with the same X-StagePay-Delivery value (the idempotency key is preserved).
  • Resending resets the attempt count, so the retry schedule above starts over from the beginning.
  • X-StagePay-Timestamp and the signature are recalculated on every send. Even an old callback that is resent passes the timestamp check.

Idempotency

A callback for the same payment may arrive more than once (retries, manual resends, lost merchant responses). Make sure your merchant server grants only once.

Choose an idempotency key

  • Primary key: X-StagePay-Delivery — always the same value for retries of the same event
  • Secondary key: tid + event — payment completion occurs only once per transaction

Record it atomically

Before granting, first INSERT the idempotency key into a table with a unique constraint. If a duplicate key error occurs, treat the callback as already processed and respond with 200.

Grant and record in the same transaction

Where possible, wrap the idempotency key INSERT and the item grant in a single DB transaction. If the grant fails partway, it is rolled back and processed again on the next retry.

Example idempotency table (PostgreSQL)
CREATE TABLE stagepay_deliveries (
  delivery_id  uuid PRIMARY KEY,
  tid          text NOT NULL,
  event        text NOT NULL,
  received_at  timestamptz NOT NULL DEFAULT now(),
  UNIQUE (tid, event)
);
Node.js (pg)
async function handleCallback(db, deliveryId, event) {
  const client = await db.connect();
  try {
    await client.query('BEGIN');
    const r = await client.query(
      `INSERT INTO stagepay_deliveries (delivery_id, tid, event) VALUES ($1, $2, $3)
       ON CONFLICT DO NOTHING`,
      [deliveryId, event.tid, event.event],
    );
    if (r.rowCount === 0) {
      await client.query('ROLLBACK');
      return 'duplicate'; // Already processed → respond 200
    }
    if (event.event === 'payment.succeeded') {
      await grantItem(client, event); // Verify the order (productId, amountMinor, currency), then grant
    } else if (event.event === 'payment.refunded') {
      await revokeItem(client, event); // Handle according to your clawback policy
    }
    await client.query('COMMIT');
    return 'processed';
  } catch (e) {
    await client.query('ROLLBACK');
    throw e; // Respond 5xx → StagePay retries
  } finally {
    client.release();
  }
}

Ordering

Callback order is not guaranteed. For example, payment.refunded may arrive before a payment.succeeded that is still being retried. The body's transactionStatus and refundedAmountMinor reflect the transaction state at send time, so if you need the latest state, query the transaction with the REST API.

On this page