Stripe webhooks and idempotency for small SaaS products

Published 2026-11-06

Do not trust the redirect

After a customer pays, the browser returns to your site. It is tempting to mark the order as paid at that moment. Do not: the user can close the tab before the redirect, can open the success URL by hand, and networks fail. The payment provider's server-to-server notification, a webhook, is the reliable record that money moved.

Verify every webhook

A webhook endpoint is a public URL, so anyone can post to it. Providers sign each event; check the signature against the raw request body with your signing secret before doing anything. Reject requests with a bad signature or a timestamp far in the past. Use the raw body, because re-encoded JSON will not match the signature.

Make handlers idempotent

Providers retry deliveries when they do not get a quick success response, and events can arrive out of order or more than once. Every handler must be safe to run repeatedly: store the event identifier and skip events already processed, and write state changes as set-to-value instead of increment. Granting a subscription twice should leave the user with one subscription.

Return a success status fast and do slow work, such as sending email, in a queued job, so timeouts do not trigger needless retries.

Keep your own state

Store the provider's customer and subscription identifiers next to your user, and the status you derive from events. Treat the provider as the source of truth for billing and your database as a cache of it, with a reconciliation command that can re-sync a customer after a missed event.

Log for the bad day

  • Event identifier, type and result for every webhook received.
  • The user or account an event was applied to.
  • Failures with enough context to replay them.
  • No card data or secrets in logs, ever.

Test in test mode, then rehearse

Use the provider's test mode and its tools to send events to your local or development endpoint, including failure and repeat scenarios. Rehearse the awkward cases: a payment that fails after a trial, a refund, a dispute, and a customer who changes plan twice in a minute.

A minimal event flow

A robust flow has four steps. The endpoint receives the event and verifies its signature. It records the event identifier, and if that identifier was already processed, it returns success immediately. Otherwise it updates the local order or subscription state inside a database transaction. Finally it responds quickly, leaving slow work such as sending emails to a queue.

Because the provider retries until it sees a success response, anything that can fail halfway must be safe to run again. That is the whole meaning of idempotency in this context.

Edge cases to plan for

  • Events arriving out of order: derive state from the object in the event, not from an assumed sequence.
  • Duplicate deliveries: the stored event identifier makes them harmless.
  • A secret that was rotated: support the new value before retiring the old one.
  • Refunds, disputes and failed renewals: handle them as deliberately as successful payments.
  • Missing configuration: fail closed, so an endpoint without a signing secret refuses events instead of accepting unsigned ones.

Keeping money logic reviewable

Put payment state changes in a small number of well-tested places, name the states plainly, and keep an audit trail of every transition. When something goes wrong with a payment, the question is always what happened and when; a clean event log answers it in minutes.

© 2026 The Good Fellas