Backend live coding · 40 minutes
Idempotent webhook receiver
In 40 minutes, write an endpoint that receives payment webhooks from a provider. Each webhook is a JSON body with an event id, a type such as payment.succeeded or payment.refunded, an order id and an amount, plus a signature header: an HMAC-SHA256 of the raw body with a shared secret. The provider retries when it does not get a 2xx in time, so the same event can arrive several times, and events can arrive out of order. The order's status in the database must end up correct. Use the language you know best and a database or an in-memory store. Talk while you work.
Requirements
- POST /webhooks/payments reads the raw body and verifies the signature before parsing anything
- Compares signatures in constant time; a bad or missing signature returns 401 and changes nothing
- Stores each event id once; a repeated event returns 200 and is not processed again
- Records the event and updates the order in one transaction
- Handles order: a refund that arrives before its payment does not leave the order in a wrong state
- Answers quickly; slow work, such as sending an email, goes to a queue or a background job
What reviewers look for
What reviewers look for, the checklist and the stretch goals opens with a year in the app. What a year includes