Floryne Pierson
← All posts

Fixing a silent Stripe webhook that was failing 100% of the time

Paying users weren't getting access. The webhook looked configured. Nothing was written to the database. Here's the exact sequence that caught it.

A user paid. They got the Stripe receipt. They came back to the site and had no premium access. I checked the database — their row hadn't been updated. I checked the Stripe dashboard — payment successful, subscription active. Somewhere between Stripe confirming the payment and my database knowing about it, everything had silently failed.

That gap is the webhook. And the webhook was failing on every single event, with no error visible anywhere.

Why it was silent

The first place I looked was the Stripe dashboard under Developers → Webhooks → Recent deliveries. Everything showed as delivered. Green checkmarks. HTTP 200. This was the most confusing part: Stripe thought it had succeeded, and my endpoint had returned 200, yet nothing had happened.

The issue was in the order of operations inside the handler. My endpoint was structured like this:

export default async function handler(req, res) {
  // 1. Parse body
  const body = await getRawBody(req);

  // 2. Return 200 immediately (avoid Stripe timeout)
  res.status(200).end();

  // 3. Verify signature
  const event = stripe.webhooks.constructEvent(
    body,
    req.headers['stripe-signature'],
    process.env.STRIPE_WEBHOOK_SECRET
  );

  // 4. Handle event
  await handleEvent(event);
}

The res.status(200).end() was placed before the signature verification. The intent was correct — Stripe requires a fast response to avoid retries — but the execution was wrong. When constructEvent threw (because I was in the wrong environment and using the wrong webhook secret), the exception was swallowed. The 200 had already been sent. Stripe saw success. The function exited. Nothing was written.

The trap: responding 200 before verifying means you're guaranteed to return success even when the actual work fails. Stripe stops retrying. The event is gone.

Finding the real error

The clue was in the Vercel function logs, not the Stripe dashboard. When I queried the logs for the webhook endpoint, there were no entries at all during the period when payments happened. The function hadn't run — or had run so briefly it left no trace.

I added a log at the very first line of the handler:

export default async function handler(req, res) {
  console.log('webhook received', req.method, new Date().toISOString());
  // ...
}

That line never appeared in production logs. Which meant the function was being called by Stripe, returning 200 (from somewhere), but my code wasn't executing at all.

The real issue: I had two webhook endpoints registered in Stripe. One was the production endpoint, correctly pointing at my Vercel deployment. The other was a leftover from local development — a forwarding URL from stripe listen --forward-to localhost:3000 that had somehow been saved as a live endpoint. Stripe was delivering to the development endpoint first (alphabetically, it came first in the dashboard list). That endpoint was unreachable, Stripe retried, eventually the delivery failed — but by then it had already marked the event as handled to avoid duplicate processing.

The fix

Three changes:

1. Remove the stale endpoint. Delete any webhook endpoint that isn't your current production URL. Having two endpoints for the same events means you're gambling on which one Stripe picks first.

2. Verify before responding. The correct pattern is to verify the signature, acknowledge with 200 if valid, and then do the work asynchronously — but critically, the 200 only goes out after verification succeeds:

export default async function handler(req, res) {
  const body = await getRawBody(req);

  let event;
  try {
    event = stripe.webhooks.constructEvent(
      body,
      req.headers['stripe-signature'],
      process.env.STRIPE_WEBHOOK_SECRET
    );
  } catch (err) {
    console.error('Webhook signature verification failed:', err.message);
    return res.status(400).send(`Webhook Error: ${err.message}`);
  }

  // Only 200 if signature is valid
  res.status(200).end();

  // Now handle the event (outside the response cycle)
  await handleEvent(event);
}

3. Use the correct webhook secret. Stripe generates a separate signing secret for each endpoint. The secret from stripe listen (local CLI) is different from the secret in the Stripe dashboard for your production endpoint. I was using the CLI secret in my production environment variable. They're not interchangeable.

Verifying the fix works

After deploying, I used the Stripe dashboard to resend a previous event: Developers → Webhooks → [your endpoint] → Recent deliveries → [event] → Resend. This time, the Vercel logs showed the handler running, the signature check passing, and the database row updating. I also confirmed directly in the database that the user's access flag had been written correctly.

The pattern worth keeping: for any webhook handler, before you ship it, trigger a test event from the Stripe dashboard and watch the destination logs in real time. Don't trust that a 200 response means the work happened — verify that the specific side effect (database write, email sent, access granted) actually occurred.

Silent failures are the worst category of production bug. The system looks healthy. Metrics look fine. Users are just quietly not getting what they paid for.