Skip to content

After the webhook arrives

A. Lee11/21/20263 min read
Cover for the article “After the webhook arrives”

Most payment tutorials end in the same place: the provider posts an event, the application returns 200, and a success message appears. That is a good first afternoon. It is not the part that keeps a store running.

The webhook is the easy part. Everything after it is the product.

The happy path is one branch

A payment integration has more events than the one in the tutorial. There are pending payments, failed payments, expired sessions, refunds, partial refunds, disputes, and events that arrive out of order.

Write down every event the provider can send before you write the handler. Then decide what your application should do with each one. For events you do not care about, log them and return 200 deliberately. Do not let an unknown event crash the endpoint, because the provider will retry it.

Duplicate events are normal

Providers retry. Networks drop responses. A process can write the order and die before it acknowledges the event. The same payment can therefore arrive two or three times, and that is normal operation.

Store the provider's event ID with a unique constraint. Process the event once. Make the side effects idempotent as well: sending a receipt twice is still a bad customer experience, even if the database row is correct.

Return success only after the work is durable. A 200 before the write is a promise you have not kept.

Refunds move backwards

A refund is not the absence of a payment. It is a new event with its own rules.

Decide what happens to access after a refund. Does the buyer keep the files? Does a course enrollment close? What about a partial refund, or a dispute that is later resolved in your favour? The answers are business decisions, but the code must make them explicit.

Record the reason and the provider reference. Future you will need both.

Delivery can fail after payment

A paid order can still fail to deliver. Email bounces. A storage link expires. A background worker runs out of retries.

Keep payment state and delivery state separate. "Paid" and "delivered" are different facts, and an order can be one without the other. Give delivery its own retry policy, its own failure reason, and a view that shows paid orders that have not reached the buyer.

The worst outcome is not a failed payment. It is a successful payment that nobody notices failed to deliver.

Replay the last month

Build a way to replay events before you need it. Start with a dry run that reports what would change. Add an audit log that records who replayed what, and when.

Then test with real old events, not only fresh ones from the sandbox. Production traffic contains the odd ordering, the duplicate delivery, and the customer who paid twice that no test fixture predicted.

Monitor the customer outcome

Dashboards full of CPU graphs will not tell you whether buyers are getting their files. Track the things a customer would notice: unfulfilled paid orders, failed deliveries, signature failures, and the age of the oldest unprocessed event.

Alert on those. The infrastructure can be healthy while the store is quietly broken.

Related posts

Start from a billing seam that is already wired.

SubSaver ships Supabase auth, the database migration, and optional Stripe billing in a Next.js project.

Get SubSaver source — USD 49