← Writing5 min read

SYMPTOM

Customers receive duplicate credits or pay but cannot access their account

How Stripe webhooks silently break in AI-generated apps (and how to fix them)

Why AI-coded payment handlers often fail in production: raw body signature parsing in Next.js, missing idempotency guards, and duplicate subscription provisioning on network retries.

You tested your Stripe Checkout integration in test mode using 4242.... You completed the checkout, your webhook received checkout.session.completed, your database updated, and your test user received credits.

It looks 100% complete.

A successful test checkout does not cover every fulfillment path. Examples to check include duplicate credits, a paid account that remains locked, and webhook signature failures. These are illustrative failure cases, not outcomes from a client engagement.

What went wrong?

AI coding tools (Cursor, Lovable, v0) write payment handlers that work under perfect local test conditions. But payment infrastructure is an asynchronous distributed system where network timeouts, retries, and out-of-order events are the norm.

Payment Launch Gate

A payment integration is only as reliable as its webhook failure handling.

  1. 01Stripe can deliver an event more than once. Return promptly after durably accepting the work, and make fulfillment safe to retry.
  2. 02If fulfillment is not safe to repeat, a retried or duplicated webhook can grant credits twice, upgrade an account again, or send duplicate emails.
  3. 03Signature verification fails when the handler does not keep the raw request body exactly as received, for example after reading it with req.json().

1. The Duplicate Grant Problem: Why Webhook Idempotency is Mandatory

When a customer pays on Stripe Checkout, Stripe sends an HTTP POST request to your webhook endpoint (/api/webhooks/stripe).

Slow or failed delivery can cause retries. Do not rely on a fixed three-second threshold; check the endpoint's delivery history and make repeated processing safe.

Here is the typical AI-generated webhook handler:

// VULNERABLE: Naive webhook handler
export async function POST(req: Request) {
  const event = await req.json();

  if (event.type === 'checkout.session.completed') {
    const session = event.data.object;
    // DANGEROUS: Executes unconditionally every time Stripe sends or retries this event!
    await addCreditsToUser(session.customer, 100);
  }

  return NextResponse.json({ received: true });
}

For example, if this event reached the handler three times (say, after earlier deliveries timed out or failed), this handler would grant 300 credits for a 100-credit purchase.

Sources (Stripe docs): Handle duplicate events, Verify webhook signatures and Checkout fulfillment.

2. Recording an event is not the same as granting access

A unique event ID can help detect repeated delivery. It does not, by itself, make the whole fulfillment process safe to retry.

If a handler records an event and then fails before granting access, a later delivery can see that event ID and skip the unfinished work. The customer paid, but access was never created. If recording the event fails for another reason and the handler continues anyway, duplicate side effects can still occur.

Ask your developer how the system recovers from each partial failure. When the event record and entitlement live in the same database, a transaction can keep the related database changes together. When work crosses services, use a durable processing state and a retryable workflow with idempotent side effects. Recording receipt or queuing work is not the same as recording completed fulfillment.

Do not acknowledge work that can still be lost. A fast webhook response should follow durable acceptance of the work; a worker can then complete it and record the outcome. This article describes requirements, not a tested implementation for your app.

For Stripe Checkout, also use the Checkout Session and confirmed payment state when deciding whether to fulfill. Fulfillment must remain safe if called repeatedly or concurrently for the same purchase, including through both webhooks and a landing-page request. An event-ID table alone does not define that business-level rule.

3. The Next.js Raw Body Signature Trap

Another classic failure mode in AI-generated Next.js 14/15 App Router apps is req.json() vs req.text().

Stripe signs the raw request body exactly as it was sent, and verification needs those same bytes. Calling await req.json() consumes the body stream and leaves you with a parsed object, not the original payload. If the handler then rebuilds a string from that object (for example with JSON.stringify) and verifies that, the bytes no longer match what Stripe signed, so verification fails and a typical handler returns a 400 error.

Always read const body = await req.text(); first, verify the signature, and only then extract data.

4. Pre-Launch Webhook Test Checklist

Before taking real money, simulate real-world failure scenarios using the Stripe CLI:

  • Verify repeat and concurrent delivery using the same test event or Checkout Session. Check the resulting entitlement and database state; do not assume two trigger commands create the same event ID.
  • Simulate failure after accepting an event but before granting access. Retry the work and verify that access is eventually granted once, without duplicate credits or lost fulfillment.
  • Verify signature rejection: Send a POST request with an invalid signature header and verify the server returns 400 Bad Request.
  • Verify server-side price validation: Ensure the checkout session validates price IDs from server configuration, not from client-supplied amounts.
  • Verify cancellation and subscription updates against your product's access policy, including cancellation at period end. Do not revoke prepaid access early unless that is the intended rule.

More writing

Written by Jaeil Lee — independent reviewer of AI-built apps. About