Charging Customers Safely
Taking a payment is more than calling an API. Idempotency, webhooks, subscription lifecycles and never trusting the client are the patterns that keep real charges correct even when the network misbehaves.
Actually taking money introduces problems that pure calculation doesn't: networks time out, users double-click, webhooks arrive twice or out of order, and subscriptions renew and lapse over time. Charging customers safely is about the patterns that keep every real charge correct despite all that. This session covers the essentials: idempotency, webhooks as the source of truth, subscription lifecycles, and the golden rule of never trusting the client.
Idempotency: charge once, even on retries
The classic bug: a payment request times out, the client retries, and the customer is charged twice - even though the first charge actually succeeded.
client sends payment with a unique idempotency key:
key = 'order-8471'
first request -> process, record key, charge once
retry (same key) -> already seen? return the SAME result, DON'T charge againWebhooks and never trusting the client
Two rules save you from whole categories of fraud and bugs: the server decides the amount, and the payment processor - not the browser - confirms the payment.
| Naive / unsafe | Safe |
|---|---|
| Client sends the price to charge | Server computes the price from its own data |
| Trust a browser redirect as 'paid' | Wait for the processor's signed webhook |
| Grant access on the success page | Grant access when the webhook confirms |
| Accept unverified webhook calls | Verify the webhook signature (HMAC) |
<b>Never trust the client for the amount</b> - a manipulated request could pay $0.01 for a $100 product; the server must compute the price from its own records. And <b>the success page is not proof of payment</b> - a user can reach it without paying, or close the tab before it loads; the real confirmation is the processor's <b>webhook</b>, a server-to-server message you verify by its signature (HMAC) and then act on. This is how the stack's settlement adapters work: a checkout creates a payment, and access/credit is granted only when the signed webhook confirms it - deduped by transaction id.
Subscriptions are a state machine
Recurring billing isn't a boolean 'subscribed'; it's a lifecycle with states and transitions, and modelling it explicitly prevents bugs.
trial -> active -> (renews) -> active ...
active -> past_due (payment failed) -> active (recovered)
-> canceled (dunning gave up)
active -> canceled (user cancels; often access until period end)๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- Make charging idempotent - a unique key so retries (timeouts, double-clicks) charge only once.
- Never trust the client for the amount; the server computes the price from its own data.
- The success page isn't proof of payment - grant access when the signed webhook confirms it.
- Model subscriptions as an explicit state machine (trial/active/past-due/canceled), not a boolean.
Frequently asked
What exactly is idempotency and why does payment code need it so badly?
Idempotency means that performing the same operation multiple times has the same effect as performing it once - and payment code needs it because, in the real world of unreliable networks, the same payment request will sometimes be sent more than once, and without idempotency that means charging the customer multiple times for a single purchase, which is a serious, trust-destroying bug. Here's the scenario that makes it essential: a client sends a request to charge a customer, the request reaches your server and the charge succeeds, but the response back to the client is lost (network timeout, dropped connection, the user's phone loses signal). The client, having received no confirmation, reasonably assumes the charge didn't go through and retries - sending the same charge request again. Without protection, your server processes it as a brand-new charge, and the customer is billed twice for one purchase. This isn't rare: timeouts, double-clicks on a 'Pay' button, mobile network flakiness, and automatic client retries all cause duplicate requests routinely. Idempotency solves it with a simple, powerful idea: each payment operation carries a unique idempotency key (generated by the client for that specific purchase), and the server records which keys it has already processed. When a request arrives, the server checks: have I seen this key before? If no, process the charge and record the key with its result. If yes, don't charge again - just return the same result as the first time. So retries with the same key are safe: the customer is charged exactly once, and the retrying client gets a successful response, no matter how many times the request is delivered. The same principle protects webhook handling (payment processors may send the same event multiple times, so you dedupe by the event id) and any operation that must not be double-applied. Payment code needs this so badly specifically because the cost of getting it wrong is direct financial harm to customers and a breach of their trust, and because duplicate requests are not an edge case but a routine occurrence in networked systems. Reputable payment APIs provide idempotency-key support for exactly this reason, and any money-moving code you write should build it in: assign a unique key per intended charge, check-and-record before acting, and return the prior result on repeats. It's a foundational safety pattern for correct payments, turning the unavoidable reality of duplicate requests from a double-charging disaster into a non-event.
Why can't I just trust that the customer paid when they land on my success page?
You can't trust the success page as proof of payment because reaching that page and actually completing a payment are two separate things that can come apart in both directions - a user can arrive at the success URL without having paid, and a user can pay successfully but never reach the success page - so granting access or fulfilling an order based on the success page leads to giving away product for free and to paying customers not getting what they paid for. Consider the ways it breaks. A user could bookmark or guess the success URL and visit it directly without paying. A malicious user could deliberately navigate to it to try to get the product for free. On the other side, a legitimate paying customer might complete payment on the processor's page but then close their browser, lose connection, or have the redirect back to your site fail before the success page loads - so they paid but your system, relying on the success page, never records it, and they don't get access, generating a support complaint and a refund risk. The success page is just a browser navigation; it is not a trustworthy, tamper-proof signal that money actually changed hands. The reliable source of truth is the payment processor's webhook: a server-to-server message the processor sends directly to your backend, confirming the payment succeeded (or failed), independent of what the user's browser does. Because it comes from the processor to your server, it can't be forged by the user (and you verify its authenticity by checking a cryptographic signature, typically an HMAC, so you know it genuinely came from the processor), and it arrives regardless of whether the user's redirect worked - so you learn about the payment even if the customer's browser never made it back to your success page. The correct pattern is therefore: treat the success page purely as a user-facing 'thanks, we're processing' screen (nice UX, not authoritative), and grant access, fulfil the order, or credit the account only when you receive and verify the processor's webhook confirming payment, deduping by the transaction id so you act once. This ensures you fulfil exactly the payments that really happened - not the ones where someone merely reached a URL, and not missing the ones where the redirect failed. Trusting the client's browser for something as consequential as 'did they pay' is a classic, costly mistake; the server-verified webhook is the source of truth. Build your fulfilment around the webhook, verify its signature, and let the success page be just a friendly message.