After a Stripe Checkout payment, two things try to give the buyer their purchase: Stripe's webhook, and the browser, which Stripe redirects back to your site. They race. Either can arrive first, either can arrive twice, and either can fail halfway. I didn't want the buyer to wait for the webhook alone.
This is how our shop handles it: one function, called by both, with three separate "only once" guards. It's an exam practice site, so "the purchase" is access to practice exams, but the pattern holds for anything you deliver after payment.
The two callers
The webhook verifies the signature, then hands the event to a handler. If anything throws, it answers 500 and Stripe retries:
try { const outcome = await handleStripeEvent ( db , event , { email : getEmailSender ( ) , baseUrl : appUrl ( ) } ) ; return Response . json ( { received : true , outcome } ) ; } catch ( err ) { console .…