Your Stripe webhook fires twice, and the customer gets two emails, two credits, or two upgrades. This is an AI-built app security problem as much as a bug: a webhook endpoint is a public URL, and anything that can reach it can replay it. Here is why it happens and the fix, checked against Stripe's documented behavior.
Why Stripe sends the same event more than once
Stripe's docs say webhook endpoints might receive the same event more than once, and that you should guard against duplicates by logging the event IDs you have already processed. Stripe also retries when your endpoint does not return a 2xx quickly. So a slow handler that times out will get the event again, even though the first run succeeded.
The 6 causes
- Your handler does the work before it replies. Slow email or API calls push you past the timeout, Stripe retries, and the work runs twice. Return 200 first, then process.
- No record of processed event IDs. Every
event.idis unique. Store it in a table with a unique constraint and skip any repeat. - You subscribed to overlapping events. For example, handling both
checkout.session.completedandpayment_intent.succeededfor the same purchase grants access twice. Pick one source of truth. - Two endpoints point at the same URL. A test and a live endpoint, or an old endpoint you forgot to delete, both deliver to your app.
- Your handler throws after a partial write. Stripe retries, and the first half runs again. Wrap the write and the event ID insert in one database transaction.
- Events arrive out of order. Stripe does not guarantee delivery order. Read the current object state instead of assuming a sequence.
The fix
// Insert the event ID first. A unique violation means we already handled it. const { error } = await db.from("processed_events").insert({ id: event.id }); if (error?.code === "23505") return new Response("ok", { status: 200 }); // ...grant access, send email, etc. Do this in the same transaction as the work when you can.
The security part
Idempotency is not authentication. Always verify the signature with the raw request body and your signing secret before you look at the event, and reject anything that fails. Without it, anyone who finds your webhook URL can post a fake "payment succeeded" event. Do not skip verification to make local testing easier, and do not put the signing secret in client code or a public repo.
A quick test
- Use the Stripe CLI to resend one event and confirm nothing runs twice.
- Send a request with a wrong signature and confirm you get a 400.
- Confirm the endpoint does not return details in error responses.
What an outside scan can and cannot tell you
ProofForge looks at your live site from the outside, the way a stranger would. It can flag exposed files, secret-shaped strings in shipped code and open endpoints. It cannot see your webhook handler logic or your database, so it will not tell you whether your dedupe works. The checks above are yours to run.
Check your own app from the outside, free. Run the free ProofForge scan on your live URL. No account needed for the first scan. It looks at your public site the way a stranger would and says pass, fail or inconclusive, so you know what is confirmed and what it could not see. One-time reports start at $9: see pricing. Building with an AI agent? There is an MCP server so it can check before it deploys.