#735·open-saas

Payment webhooks process the same event more than once

Author: FranjoMindekCreated Oct 1, 2026Updated Oct 1, 2026
Labelsbug

Split out of #688, which covered several webhook problems at once. This one is the idempotency gap.

Problem

Stripe, Polar, and Lemon Squeezy all deliver webhook events at least once. Each of them retries when our endpoint returns a non-2xx response or times out. Stripe retries for up to three days, Polar up to 10 times, Lemon Squeezy up to three more times. On top of that, Stripe states that a webhook endpoint "might occasionally receive the same event more than once".

The template's webhook handlers in src/payment/<provider>/webhook.ts process every delivery as if it were new. The subscription handlers set absolute values, so running them twice is harmless. The credits purchase is not: updateUserCredits in src/payment/user.ts runs credits: { increment: ... }, so a redelivered invoice.paid (Stripe), order.paid (Polar), or order_created (Lemon Squeezy) grants the credits again.

What the providers recommend

  • Stripe: "guard against duplicated event receipts by logging the event IDs you've processed, and then not processing already-logged events" (docs).
  • Polar: use the webhook-id header as a durable deduplication key. Polar follows the Standard Webhooks spec, which says that id "remains the same no matter how many times a webhook that has failed is retried".
  • Lemon Squeezy documents no stable event id. Real payloads carry meta.webhook_id, but nothing documents whether it stays the same across retries.

Suggested fix

Store the id of every processed event in a table with a unique constraint and skip events you have already seen. Record the id before running the handler, so a retry that arrives while the first attempt is still running hits the unique constraint and gets skipped. If the handler throws, delete the record so the provider's next retry can process the event. For Lemon Squeezy, key the order_created event on the order id, since that is the only non-idempotent path.