Stripe webhook fires twice and the user is credited again

The same Stripe event is delivered more than once, and your handler grants access or adds credits again

What you see

Checkout works, the payment shows as paid in Stripe, and your webhook returns 200. Then the user has twice the credits, two “welcome” emails, or the fulfilment job ran again. In Stripe Workbench → Webhooks → your endpoint → Event deliveries, the same event appears more than once: an automatic retry, or a Resend you clicked after debugging.

This is not a broken signature. If verification is failing, start with the signature verification guide and come back here once deliveries show 200.

Why it happens

Stripe’s webhook docs say webhook endpoints might occasionally receive the same event more than once. Two mechanisms make that routine, not exceptional:

  1. Automatic retries. In live mode Stripe retries deliveries for up to three days with an exponential back off. In a sandbox it retries three times over the course of a few hours. Any response that isn’t a 2xx counts as a failure and is retried. A timeout after your handler already did the work is retried too.
  2. Manual resends. In the Dashboard you can click Resend for up to 15 days after the event was created (the Stripe CLI can resend for up to 30 days). A manual resend does not cancel the automatic retry schedule.

The identifier to use is event.id (an evt_… string), which the Event object calls the unique identifier for that event. Stripe also tells you not to use the created timestamp to decide whether you have already processed an event: snapshot events record created in seconds, so distinct events can share a timestamp, and Stripe does not guarantee delivery order.

A common bug in AI-generated handlers is “check if this event id is in the table, and if not, grant credits, then insert the row.” Two deliveries can both see “not seen,” both grant credits, and only then collide on the insert. The fix is to let a unique constraint decide the winner, in one transaction with the side effect.

Fix 1: insert the event id first

Create a table whose primary key is the Stripe event id:

create table public.stripe_events (
  id text primary key,
  type text not null,
  object_id text,
  processed_at timestamptz not null default now()
);

Then, after you have verified the signature on the raw body, claim the event with an insert. PostgreSQL’s ON CONFLICT DO NOTHING skips the row instead of erroring when the primary key already exists, and RETURNING only comes back for a row that was actually inserted:

insert into public.stripe_events (id, type, object_id)
values ($1, $2, $3)
on conflict (id) do nothing
returning id;
  • If a row is returned, this is a new event: run your fulfilment in the same transaction, then commit and return 200.
  • If no row is returned, this delivery is a duplicate: do not grant anything again, and still return 200.

Stripe’s recovery docs say the same thing for retries that arrive after you have already processed the event: skip it and return a 200 so Stripe stops retrying. Verify the signature before you look at the event id.

Putting the insert and the side effect in one transaction matters. If you commit the event id first and then crash while granting access, the retry will see a duplicate and never finish the work. If they commit together, a crash rolls both back and the next delivery can try again.

In a Postgres function, FOUND is false when ON CONFLICT DO NOTHING skips the row, so this is a complete handler:

create function public.fulfil_checkout(
  event_id text,
  event_type text,
  object_id text,
  target_user uuid
)
returns boolean
language plpgsql
as $$
begin
  insert into public.stripe_events (id, type, object_id)
  values (event_id, event_type, object_id)
  on conflict (id) do nothing;

  if not found then
    return false; -- already processed
  end if;

  update public.profiles
     set plan = 'pro'
   where user_id = target_user;

  return true;
end;
$$;

Call it from your webhook route after constructEvent (or constructEventAsync in a Supabase Edge Function). Return 200 whether the function returns true or false. Return a 5xx only when the function errors, so Stripe retries a real failure.

Fix 2: make the side effect idempotent too

Deduping on event.id stops retries of that event. Stripe also warns that in some cases two separate Event objects are generated and sent. Those have different event.id values, so the table above will admit both. To identify them, Stripe says to use the id of the object in data.object together with event.type.

Don’t put a unique constraint on (object_id, type) for every event you listen to: types such as customer.updated legitimately fire many times for the same object. Instead, make the business write safe:

  • Set a status, don’t increment. update profiles set plan = 'pro' is safe to run twice. set credits = credits + 100 is not: a second Event object for the same Checkout Session will add 100 again.
  • Key fulfilment on the Stripe object id. For example, a unique checkout_session_id (or PaymentIntent id) on an orders or entitlements table, and insert ... on conflict (checkout_session_id) do nothing before you grant anything.

Do slow work that isn’t in the database (emails, third-party APIs) after you have committed the durable write, or pass your own idempotency key derived from event.id so a retry doesn’t send the email twice. Stripe also asks you to return a 2xx quickly, before logic that could time out: a timeout is retried even if the work already happened.

Don’t do this

  • Don’t skip signature verification because the event already has an evt_ id. Anyone can POST a JSON body with a made-up id. Verify first; the signature guide covers the raw body and the whsec_ secret.
  • Don’t use event.created as a dedupe key. Stripe tells you not to: distinct events can share that timestamp.
  • Don’t return 500 for a duplicate. That asks Stripe to retry an event you have already handled.
  • Don’t rely on “check if seen, then process.” Two concurrent deliveries can both pass the check. The unique constraint plus insert-first is the part that stays correct under retries.

Check it worked

  1. In a sandbox, run stripe trigger checkout.session.completed (or the event type your endpoint listens for), then Resend that same event from Workbench → Event deliveries.
  2. Both deliveries show Delivered with status 200, and your app grants the plan or credits once.
  3. Resend it a third time: the handler still returns 200, and the totals do not change.