When a customer pays, it's tempting to mark the order paid as soon as they land on your success page. Don't. People close tabs, lose signal and refresh. The reliable signal is the webhook Stripe sends to your server.
Verify the signature
Anyone can POST to a public URL. Stripe signs each event, and you check that signature with the raw request body and your endpoint secret:
const event = stripe.webhooks.constructEvent(rawBody, signature, process.env.STRIPE_WEBHOOK_SECRET)
The body must be the exact bytes Stripe sent. If your framework parses JSON first, verification fails, so read the raw text.
Expect duplicates
Stripe retries events that don't get a 2xx response quickly, and occasionally delivers the same event twice. Your handler must be safe to run more than once:
- Store each processed
event.idand skip ones you've seen. - Or make fulfilment itself idempotent: "set order status to paid" is safe to repeat; "send another product" isn't.
Respond fast, work later
Return 200 as soon as the event is verified and recorded. Send emails, generate invoices and call other APIs from a background job, so a slow email provider never causes Stripe to retry.
Listen only for what you use
For one-off payments, checkout.session.completed is usually enough. For subscriptions, add invoice.paid and customer.subscription.deleted. Test them locally with stripe listen --forward-to localhost:3000/api/webhooks before going live.

Comments (0)
No comments yet. Start the conversation.
Join the conversation
Sign in or create a free account to comment.