# Payments, Webhooks and Idempotency

> Integrate a payment provider safely and handle incoming webhooks exactly once, even when they arrive twice. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/backend/payments-webhooks
Last updated: 2026-11-28

Integrate a payment provider safely and handle incoming webhooks exactly once, even when they arrive twice.

Intermediate · about 6 hours · difficulty 3/5

Learn first: [Resource-Oriented REST APIs](https://www.internhack.in/roadmaps/tracks/backend/rest-api-design), [Transactions, ACID and Isolation](https://www.internhack.in/roadmaps/tracks/backend/transactions-acid)

Money is where backend bugs become very expensive. You should almost never touch raw card numbers: providers like **Stripe**, Adyen, PayPal or Razorpay host the card form, and your backend works with their objects (customers, payment intents, subscriptions).

### The typical flow

1. Your API creates a payment or checkout session with the provider for a specific order and amount (computed on the server, never trusted from the client).
2. The customer pays on the provider's hosted page or embedded component.
3. The provider notifies you through a **webhook**: an HTTP POST to your endpoint saying the payment succeeded, failed or was refunded.
4. Your webhook handler marks the order paid and triggers fulfilment.

### Webhooks done right

- **Verify the signature** on every webhook using the shared secret; otherwise anyone can POST "payment succeeded".
- **Respond fast** (2xx within a few seconds) and do heavy work in a background job.
- **Expect duplicates and reordering.** Providers retry on failure, so store the event id and skip events you already processed. Fetch the latest object state instead of trusting event order.

### Idempotency for outgoing calls

Network calls can time out after the charge actually happened. Send an **idempotency key** (for example the order id) with create requests so a retry does not charge twice. Design your own POST endpoints the same way for clients that retry.

### Money in code

Store amounts as integers in the smallest unit (cents, paise) plus a currency code; never use floating point. Keep an append-only ledger of payment events for audits and reconciliation.

You are ready to move on when you can take a test-mode payment end to end, replay the same webhook twice without double fulfilment, and explain why the client must not send the price.

## Key concepts

- **Hosted checkout and payment intents**
- **Webhook signature verification**
- **Handling duplicate and out-of-order events**
- **Idempotency keys**
- **Storing money as integers**
- **Subscriptions and billing cycles**
- **Refunds, disputes and reconciliation**
- **PCI DSS scope basics**

## Mini project

Integrate a payment provider in test mode for a small shop: server-side order totals, a signed webhook handler that records processed event ids, and a script that replays webhooks to prove idempotency.

## Check yourself

Your server times out while creating a charge and the client retries. How do you guarantee the customer is charged only once?

## Free resources

- [Stripe: Receive events with webhooks](https://docs.stripe.com/webhooks) (Stripe)
- [Stripe: Idempotent requests](https://docs.stripe.com/api/idempotent_requests) (Stripe)
- [Standard Webhooks specification](https://www.standardwebhooks.com/) (Standard Webhooks)

Previous: [Transactional Email and Notifications](https://www.internhack.in/roadmaps/tracks/backend/email-notifications) · Next: [NoSQL Families and When to Use Them](https://www.internhack.in/roadmaps/tracks/backend/nosql-fundamentals)

## More in Files, Email and Payments

- [File Storage and Uploads with Object Storage](https://www.internhack.in/roadmaps/tracks/backend/file-storage-uploads)
- [Transactional Email and Notifications](https://www.internhack.in/roadmaps/tracks/backend/email-notifications)
- [Payments, Webhooks and Idempotency](https://www.internhack.in/roadmaps/tracks/backend/payments-webhooks)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
