In my last post I fixed five bugs readers found in my booking webhook, and ended with an honest line: "I have not run the full flow against Stripe yet." A reader replied: "Run it in test mode before calling it proven. The replayed event is the one I'm most curious about. Post the numbers when they land."
Here they are.
The first thing I found: the app never went to Stripe
I opened my deployed demo, picked a paid service, confirmed the booking, and clicked the "Simulate payment success" button. It worked, so I almost moved on.
Then I realised that button belongs to my mock payment provider. My booking page never redirected to Stripe Checkout at all. The webhook code I had been reviewing for weeks was fine, but nothing in the UI could ever reach it. Unit tests cannot find this kind of bug, because every unit was correct.
The fix was small:
ts
if (res.checkoutKind === "redirect" && res.redirectUrl) {
window.location.assign(res.redirectUrl);
return; // keep the button disabled while the browser navigates
}
I also made the server refuse to "simulate" a payment that belongs to Stripe, so a real payment can only be confirmed by Stripe's webhook.
The setup
A Stripe Sandbox (test mode) with a webhook destination pointing at /api/stripe/webhook on the deployed demo.
Test card 4242 4242 4242 4242.
Supabase SQL editor to check the database after every step.
The check I ran each time:
sql
select b.status as booking_status,
b.confirmed_payment_id = p.id as same_payment,
p.status as payment_status
from public.payments p
join public.bookings b on b.id = p.booking_id
order by p.created_at desc
limit 3;
Test 1: the happy path
Confirm booking, get redirected to checkout.stripe.com, pay with the test card, land back on "My bookings".
Result: webhook checkout.session.completed returned 200. Booking confirmed, payment paid, and confirmed_payment_id equals the payment's id.
Test 2: the replayed event
This is the one the reader asked about. In the Stripe dashboard I opened the delivered event and clicked Resend.
Result: the same event ID was delivered a second time, 200 OK again. Stripe's payments list still showed one payment and no refund. The booking row was unchanged.
The reason it works: on a retry, the update WHERE status = 'pending' matches zero rows, so the handler reads the booking and sees that confirmed_payment_id is this same payment. Same id means "a retry, answer 200", not "a duplicate, refund".
Test 3: a duplicate payment
The UI now closes an open checkout before opening a new one, so a normal user cannot easily pay twice. To test the handler anyway, I created a second payment row for the already confirmed booking and a second Checkout Session with that payment's id in its metadata (from the Stripe shell), then paid it.
Result:
payment status
first paid, and it is the booking's confirmed_payment_id
second refunded
booking still confirmed
The handler saw a different payment id on an already confirmed booking, refunded the second payment, and left the booking alone.
What I did not test
Expired sessions and the retry path are covered by unit tests only. I did not run them against Stripe.
Live mode is untested. Everything above is test mode.
The duplicate in Test 3 was created by hand, so it proves the webhook logic, not a real customer opening two tabs.
What I would take from this
A passing unit test is not a working product. The most important bug here was not in the logic. It was a missing redirect.
Test the replay. Retried events are normal in production. Resend one on purpose.
Store which payment confirmed the booking. One nullable column (confirmed_payment_id) is what separates a retry from a duplicate.
Verify with the database, not the green message. I checked rows after every step.
Say what you did not test. It costs nothing and readers trust the rest more.
If you want to see the whole flow running, the booking engine with these fixes is a source-code starter kit: $99 one-time, with its limits listed on the page.
Details and live demo: https://grow-roots-landing.vercel.app/?ref=devto3
Previous post: https://dev.to/yassine_khattari_3cb9d27d/how-i-prevent-double-bookings-in-postgres-advisory-locks-supabase-11dl
Which flow in your own app have you only ever tested with a mock?
Top comments (4)
Resending a delivered event tests the dedup logic. The retry path you left to unit tests is a different beast, those are events that never got processed the first time. Stripe retries non-2xx deliveries for up to 3 days with backoff, and that only saves you if your endpoint recovers inside the window. I built Webhook Relay for this exact case, it holds failed deliveries and replays them once your endpoint answers again.
Fair point, and I should have been clearer. Resend only exercises the dedup path: an event that was already processed. The path you describe, where the first delivery fails and a retry has to do the work, I only covered with unit tests (the handler returns 500 on unknown failures so Stripe retries). I haven't run it against Stripe, and I listed it as untested for that reason.
The 3-day window is the real limit. If my endpoint is down longer than that, the client has paid and I have no booking. Before leaning on a retry queue, my plan is a small reconciliation job: list recent paid Checkout Sessions and check each one has a confirmed booking. I'll test the failing-first-delivery case in test mode and post what happens.
The missing redirect is the best finding here. A mock provider reachable from the real UI is a trap: anything that can mark a booking paid without Stripe will get hit eventually, so refusing it on the server was the right call.
The expired path is easy to run for real too: create a session and expire it through the API instead of waiting for it to time out, then check the booking goes back the way you expect.
One more case worth a test: the customer usually lands back on "My bookings" before the webhook has arrived. If that page only reads your DB, it shows the booking as unpaid for a few seconds, and some people will try to pay again. A short "confirming payment" state, or checking the session status on return, avoids that.
Agreed on the mock provider: the server now refuses to "simulate" any Stripe payment. On the return page, it currently shows a "confirming" message but still lets the booking look pending and payable until a refresh, so it's only half done. I'll add polling plus a disabled pay button until the webhook lands. I'll also run the expiry for real with stripe checkout sessions expire and post what the booking and payment rows do.