# Idempotent payment webhook pipeline for a paid membership

> A payment webhook pipeline for a paid Telegram membership: it answers the provider at once, verifies every signature and grants access once, even when the same webhook arrives again.

- Service: Payments and webhooks
- Stack: Cloudflare Workers, Queues, WebCrypto, PostgreSQL
- Canonical: https://jamm-automation.com/cases/payment-webhooks/
- Updated: 2026-11-29

## Manual

A paid membership has to grant access once, while providers resend webhooks, rotate signing keys and deliver events late or twice.

- **Retries:** a webhook is sent again when the answer is slow.
- **Keys:** the provider's signing key can change.
- **Order:** events arrive late or in duplicate.

## System

We split receiving from processing: the Worker only stores the event, a queue consumer handles it.

- **Ingress:** requires a signature header, caps the body at 64 KB, stores the raw body in a queue and answers 200 at once.
- **Signature:** ECDSA P-256 over the raw body with WebCrypto; on a mismatch the consumer refetches the provider key in case it rotated.
- **Transaction:** one PostgreSQL function locks the order row, drops stale events and ignores a repeated success.
- **Grant:** access is unique per (rail, source reference), so one payment cannot open it twice.

## Autopilot

Every delivery is journaled step by step, and failures are retried and reported.

- **Resume:** a redelivered webhook continues from the first unfinished step.
- **Retries:** backoff doubles from 60 s up to one hour; a message that keeps failing goes to a dead-letter queue.
- **Alert:** the admin gets the raw payload, the signature and the attempt count.
- **Fallback:** if the queue is down the Worker processes inline; if that fails too it answers 500 so the provider retries.
