🔧 Unbreak My AppAll fixesError codesFix KitsSponsor

Fixes › Payments

Stripe webhook runs twice: duplicate orders, credits or double charges

CursorBoltLovableReplitv0

Published

The error

checkout.session.completed handled twice / user credited twice / customer charged twice for one purchase

Why it happens

Stripe delivers webhook events at least once, so the same event can arrive more than once, and Stripe retries any delivery that doesn't get a fast 2xx response. AI-generated handlers usually do the work (grant credits, create the order, send email) every time an event arrives, with no record of which event IDs were already processed. Other common causes: two webhook endpoints (or the Stripe CLI listener plus a Dashboard endpoint) pointing at the same app, handling both checkout.session.completed and payment_intent.succeeded for the same purchase, slow handlers that time out and get retried, and a pay button that can be clicked twice and creates two PaymentIntents or Checkout Sessions.

How to fix it

  1. Store every processed event.id in a table with a unique constraint (e.g. processed_stripe_events(id text primary key)); insert first, and if the insert conflicts, return 200 and skip the work.
  2. Make the business action itself idempotent too: upsert the order keyed by the Checkout Session or PaymentIntent id instead of inserting a new row each time.
  3. Fulfil from one event type per purchase (usually checkout.session.completed for Checkout), not from several overlapping ones.
  4. Verify the signature, record the event, return 2xx quickly, and do slow work (emails, AI calls) afterwards or in a background job so Stripe doesn't retry on timeout.
  5. In Stripe Dashboard → Developers → Webhooks, check for duplicate endpoints subscribed to the same events, and stop stripe listen once a real endpoint exists.
  6. Prevent real double charges at the source: disable the pay button after the first click and send an Idempotency-Key header (idempotencyKey option in stripe-node) on server-side create calls.

Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)

Our Stripe webhook sometimes processes the same purchase twice (duplicate orders/credits). Make the handler idempotent: after verifying the signature, insert event.id into a processed_stripe_events table with a primary key and skip processing if it already exists; upsert the order keyed by the Checkout Session or PaymentIntent id; fulfil from checkout.session.completed only; return 200 quickly and move slow work after the response or into a background job. Also disable the checkout button after the first click and add an idempotencyKey to the server-side Stripe create call. Give me the SQL for the new table and list any duplicate webhook endpoints I should check in the Dashboard.

FAQ

Is it a Stripe bug that events arrive twice?

No. Stripe documents that endpoints may occasionally receive the same event more than once, so handlers must ignore events they've already processed.

Webhook duplicates vs real double charges?

Duplicate events just re-run your code for one payment. A real double charge means two payments were created, usually from a double-clicked button or a retried create call without an idempotency key.

How do I test it?

Use stripe events resend <event_id> (or Resend in the Dashboard) and confirm the second delivery is skipped.

Sources

Based on the official documentation:

Related fixes

Want the full checklist for this area? Get a Fix Kit ($29)

Get one new fix a week

Free newsletter: the newest breakages in Lovable, Bolt, Cursor, Replit and v0 apps, with paste-in fixes.

We only use your email to send the newsletter. Unsubscribe any time. See privacy.