Sub topic 13
Taking payments
This chapter assumes you have a working app with sign-in and a data model that records which user owns which rows. You should also know what a webhook is. Payments build on all of that.
Why you should not handle card data yourself
Use a payment provider such as Stripe, Paddle or Lemon Squeezy. The provider collects card numbers on its own hosted page, so card data never reaches your app. This keeps you out of most card-security compliance work. Your app only stores references to what the provider created.
For an MVP, pick the simplest option that tests your hypothesis:
| Option | Best for | Trade-off |
|---|---|---|
| Payment link | A first test of whether anyone will pay | No control over the flow |
| Hosted checkout session | Subscriptions or one-time purchases tied to a user | Needs a small amount of backend logic |
| Merchant of record (Paddle, Lemon Squeezy) | Selling internationally without handling sales tax | Higher fees |
The payment flow
The key point is that the browser returning from checkout is not proof of payment. A user can reach your "success" page without paying, or close the tab before reaching it. The provider's webhook is the source of truth.
Steps to implement it
- Create a product and price in the provider's dashboard, using test mode.
- Add a plan field to your data model, such as
planwith valuesfreeandpaid. - When the user clicks upgrade, create a checkout session and attach your user ID as a reference.
- Add a webhook endpoint that receives events from the provider.
- Verify the signature, then update the matching user's plan.
- Make your access rules read the
planfield, not anything the browser sends.
Verify webhooks
Anyone who finds your webhook URL can post a fake "payment completed" event. Providers sign each request with a shared secret. Check the signature against the raw request body before you act on it.
import { createHmac, timingSafeEqual } from "node:crypto";
// Verifies an HMAC-SHA256 signature (hex) sent by a service with a shared secret.
export function isValidSignature(rawBody, signatureHex, secret) {
const expected = createHmac("sha256", secret).update(rawBody).digest();
const received = Buffer.from(signatureHex, "hex");
return received.length === expected.length && timingSafeEqual(received, expected);
}Some providers use their own signature format. Stripe, for example, signs a timestamp together with the body and offers a library function for verification. Use the provider's documented method when there is one.
Common errors
- Trusting the success page. Grant access only after the webhook arrives.
- Processing the same event twice. Providers retry failed deliveries. Store each event ID and skip ones you have seen.
- Keeping secret keys in the front end. Secret keys and webhook secrets belong in server-side environment variables.
- Skipping cancellations and failures. Handle events for failed renewals and cancellations, or users keep paid access forever.
- Going live without a test. Run a full purchase, a cancellation and a failed card in test mode first.
Exercise
A user pays, but closes the browser before the success page loads. Your app only upgrades users when the success page loads. What goes wrong, and how do you fix it?
Show answer
The user paid but stays on the free plan, because the success page never ran. Fix it by upgrading the user from the provider's signed webhook, after verifying the signature. The webhook arrives whether or not the browser returns.
Exercise
Why do you store each webhook event ID?
Show answer
Providers retry deliveries, so the same event can arrive more than once. Storing the ID lets you skip repeats and avoid applying the same change twice.