<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/i-rewrote-my-payment-webhooks-before-taking-a-single-dollar-here-s-what-i-found--zm3oung5e" -->

---
title: I rewrote my payment webhooks before taking a single...
description: A developer building a client payment portal describes a race-condition bug found in their own Stripe webhook handler before ever taking a real payment....
canonical: https://daily.dev/posts/i-rewrote-my-payment-webhooks-before-taking-a-single-dollar-here-s-what-i-found--zm3oung5e
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: I rewrote my payment webhooks before taking a single dollar. Here's what I found. | daily.dev
og:description: A developer building a client payment portal describes a race-condition bug found in their own Stripe webhook handler before ever taking a real payment....
og:url: https://daily.dev/posts/i-rewrote-my-payment-webhooks-before-taking-a-single-dollar-here-s-what-i-found--zm3oung5e
og:image: https://api.daily.dev/og/posts/Zm3OunG5e.png
og:image:alt: I rewrote my payment webhooks before taking a single dollar. Here's what I found.
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# I rewrote my payment webhooks before taking a single dollar. Here's what I found.

**[Developer's Journey](https://daily.dev/sources/devjourney)** · [@drakeze](https://daily.dev/drakeze) · 3 min read · 1 upvotes · 0 comments

## Summary

A developer building a client payment portal describes a race-condition bug found in their own Stripe webhook handler before ever taking a real payment. Marking a webhook event as processed before executing the payment update logic meant that if the update failed and Stripe retried, the handler would see the dedup record, return success, and silently never write the payment status, even though the card had been charged. The fix is a three-state claim record (pending, completed, failed) with a claim-execute-complete pattern, using atomic $set updates instead of increments, returning 400 rather than 500 on signature verification failures, and using constructEventAsync on edge runtimes where signature verification is async.

## Content

My studio's site has been live for weeks with the contact form open and the payment pipeline wired end-to-end. I haven't signed a client or taken a payment, and people keep asking why.

This is why.

**The stake**

I run a client portal where clients pay invoices and manage subscriptions. The money path is the one part of the system where being wrong isn't a bug report; it's a customer relationship.

I had a choice: ship it, take clients, and fix problems as real usage surfaces them. Or hold the launch open and try to break the payment path myself first, with test invoices, while nobody's money was involved.

I held it. Then I found this.

**The receipts**

My webhook handler deduplicated incoming Stripe events. Stripe guarantees at-least-once delivery, so duplicates arrive, and you have to handle them; this is the documented thing you're supposed to think about.

typescript

```
const existingEvent = await WebhookEvent.findOne({ eventId: event.id });
if (existingEvent) {
  return NextResponse.json({ received: true }); // skip duplicate
}

await WebhookEvent.create({ eventId: event.id });  // mark processed BEFORE the work

await updatePaymentStatus(event);
```

The order is wrong, and it costs you money.

`updatePaymentStatus` throws connection spike, validation error, cold start. Route returns 500. Stripe retries with backoff, correctly.

The retry arrives. `findOne` Finds the event, because the ID was written _before_ the work that failed. Handler returns 200 and exits.

The payment status is never written. Not delayed or dropped permanently, while Stripe's retry mechanism actively cooperates, because your handler now reports success every time it's asked.

Card charged. Stripe has the money. Your database says unpaid. No error anywhere.

**The one thing to take away**

Your dedup record is a **claim**, not a receipt. Give it three states instead of one:

typescript

```
status: 'pending' | 'completed' | 'failed'
```

Then: **claim → execute → complete.**

- Record exists and is `completed` → genuinely done, return early.
- Record exists as `pending` or `failed` → a previous attempt didn't finish, fall through and re-execute.
- No record → create it as `pending`.
- Work throws → set`failed`, store the error, return 500 so Stripe retries.
- Work succeeds → set `completed` with a timestamp.

Three things that make re-execution actually safe:

**Use** `$set`**, never** `$inc` **or** `$push`**.** Setting a status five times is idempotent. Incrementing five times is not. Idempotency is a property of every individual write, not of the handler; one `$inc` breaks the guarantee while everything around it looks correct.

**Return 400 on signature failure, not 500.** A bad signature can never succeed on retry. A 500 generates retry traffic forever for a request that's permanently invalid.

**On edge runtimes, use** `constructEventAsync`**.** Signature verification is sync on Node and async under WebCrypto on V8 isolates. Same family as the Stripe SDK defaulting to Node's `https` module and hanging on Workers libraries carry Node assumptions that quietly don't hold at the edge.

**Where that leaves things**

Still not signing clients. The legal pages are under attorney review, and the admin side isn't closed yet.

But the honest reason is this one. That handler was merged and deployed and would have worked perfectly under every condition except a database hiccup at the wrong moment. It might have run correctly for months. The first time it didn't, a client would have paid me and been told they hadn't.

I found it with test invoices and fake cards. That's the whole case for a launch that stays open longer than it looks like it needs to.

[**My Personal Blog**](https://blog.drakeze.com/)

## Similar posts on daily.dev

- [How to Handle Stripe Webhooks Reliably with Background Jobs](https://daily.dev/posts/how-to-handle-stripe-webhooks-reliably-with-background-jobs-hc1vqu7fi) · freeCodeCamp · 2 upvotes · 0 comments
- [Stripe Webhooks in Production: A 2026 Developer Guide](https://daily.dev/posts/stripe-webhooks-in-production-a-2026-developer-guide-k2uxwifgt) · Alex CloudStar · 0 upvotes · 0 comments
- [System Design Drill Day 9: Idempotency Keys for Payments and Retries](https://daily.dev/posts/system-design-drill-day-9-idempotency-keys-for-payments-and-retries-eywkkueyl) · C\# Corner · 11 upvotes · 1 comments
- [Beyond Mocked Payloads: End-to-End Stripe Webhook Testing](https://daily.dev/posts/beyond-mocked-payloads-end-to-end-stripe-webhook-testing-y0xejk2jm) · RUBYLAND · 0 upvotes · 0 comments
- [The valley of webhooks](https://daily.dev/posts/the-valley-of-webhooks-6ekxclvor) · Hacker News · 0 upvotes · 0 comments

---

Tags: [#nextjs](https://daily.dev/tags/nextjs), [#stripe](https://daily.dev/tags/stripe), [#payment-processing](https://daily.dev/tags/payment-processing)

[View this post on daily.dev](https://daily.dev/posts/i-rewrote-my-payment-webhooks-before-taking-a-single-dollar-here-s-what-i-found--zm3oung5e)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/i-rewrote-my-payment-webhooks-before-taking-a-single-dollar-here-s-what-i-found--zm3oung5e","headline":"I rewrote my payment webhooks before taking a single dollar. Here's what I found.","text":"A developer building a client payment portal describes a race-condition bug found in their own Stripe webhook handler before ever taking a real payment. Marking a webhook event as processed before executing the payment update logic meant that if the update failed and Stripe retried, the handler would see the dedup record, return success, and silently never write the payment status, even though the card had been charged. The fix is a three-state claim record (pending, completed, failed) with a claim-execute-complete pattern, using atomic $set updates instead of increments, returning 400 rather than 500 on signature verification failures, and using constructEventAsync on edge runtimes where signature verification is async.","url":"https://daily.dev/posts/i-rewrote-my-payment-webhooks-before-taking-a-single-dollar-here-s-what-i-found--zm3oung5e","datePublished":"2026-09-24T19:30:52.939Z","dateModified":"2026-09-24T19:31:14.871Z","author":{"@type":"Person","name":"Anthony","url":"https://daily.dev/drakeze","image":"https://media.daily.dev/image/upload/s--11gwAjuC--/f_auto/v1765561095/avatars/avatar_m24lqqCHX4cycLADBIHJ7?_a=BAMAK+ZW0","description":"Full-stack dev | Founder of SorenLab | Building in public | Future polyglot engineer","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":4180}},"image":"https://media.daily.dev/image/upload/s--KICV9jI0--/f_auto/v1790278253/posts/Zm3OunG5e?_a=BAMAMicg0","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/devjourney","name":"Developer's Journey"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Developer's Journey","item":"https://daily.dev/squads/devjourney"},{"@type":"ListItem","position":3,"name":"I rewrote my payment webhooks before taking a single dollar. Here's what I found."}]}
```

