Lesson 10 of 16
45. Design a Payment System (Stripe / UPI)

45.9 How do callbacks and webhooks work?

3 min read+10 XP

Two directions, same rules (Chapter 24):

  • Incoming callbacks from banks: verify the signature and timestamp, de-duplicate by event ID, and apply idempotently. In the chapter's code, the same success callback delivered twice captures the payment once, and a forged signature gets 400.
  • Outgoing webhooks to merchants: sign them (HMAC), retry with backoff for days, let merchants see delivery logs and replay events, and include an event ID so they can de-duplicate.
  • Never trust the client's redirect ("payment successful" in the browser URL) as proof of payment: only the server-to-server callback or a status check counts.
  • Order isn't guaranteed: "refund succeeded" might arrive before "payment succeeded"; the state machine and ledger IDs handle any order.

Memory code: "Check the signature, apply once, and believe the server, not the browser."