Stripe checkout and cart
Stripe Checkout + cart
For when the answer to “It takes payments” is yes.
Single-merchant checkout where the SERVER decides the price, a signature-verified webhook, and a cart drawer.
Built by hand again in 10 of 20 audited projects before this existed. Each rebuild was another chance to make the mistake below.
Skip the rebuild
You do not write this one. It arrives in your project as ordinary source you can read, change and keep, with its audit record and its caveats beside it.
- Download the Playground. It is free and runs on your own machine.
- Start a new app and tick “It takes payments”.
- This block is written in, along with anything else you ticked.
The mistake it removes
High-value and easy to get wrong: a checkout that trusts a client-supplied price is a free-money bug. Rebuilt in ~10/20 projects.
What is already handled
Every item here is checked by running the block’s own tests every time its checks run. Tested means the block’s own test suite runs that code. It does not mean a live run against an outside service such as a payment provider.
- unit_amount is only ever p.price_cents from the server catalog, mechanically checked, not eyeballed Tested
- no client price/amount field is read anywhere in the handler Tested
- unknown item id rejected (including __proto__/constructor, which a plain-object index let through); qty clamped to 1–99; server-side price sanity-checked; at most 20 line items per cart Tested
- the webhook fulfils from Stripe's own line items for the paid session (each line's sku and charged quantity), so what ships cannot disagree with what was paid for; metadata.cart is only a readable reference, cut at 480 characters and marked with "~" when a big cart will not fit, and nothing ships from it Tested
- SITE_URL is required and the redirect is never derived from request headers, so a caller cannot aim the page a paying customer lands on Tested
- webhook verifies its Stripe signature against the raw body Tested
What the audit found
Named rather than summarized. The reasoning behind each one ships inside the block, so it travels with the code instead of living on a page you have to trust. A finding marked fixed was fixed after the audit ran; the verdict above is still the one the audit gave.
- Webhook fulfils on checkout.session.completed without checking payment_status major · Correctness Fixed after the audit in
f7e59a75f - No live Stripe test-mode purchase has ever been run; the real SDK is installed nowhere major · Correctness
- metadata.cart truncation silently drops line items from the fulfilment record minor · Correctness Fixed after the audit in
188328b6e - Stripe client has no timeout; shopper can wait 80 seconds on 'Redirecting…' minor · Error handling & resilience Fixed after the audit in
ddd4ddfdf - Malformed JSON string body answers 500 instead of 400 minor · Correctness Fixed after the audit in
1e9924b06 - Documented cross-origin endpoint config cannot work with the shipped handler (no CORS) minor · Claims match behavior Fixed after the audit in
8c6c27d87 - Catalog fetch failure is swallowed; storefront renders empty with no message minor · Error handling & resilience Fixed after the audit in
830a1ce15 - Display currency is hard-coded to '$' regardless of catalog.currency minor · Correctness Fixed after the audit in
a1fc95c5f - Cart drawer does not move or trap focus on open (WCAG 2.1 SC 2.4.3, 1.3.1) minor · Accessibility Fixed after the audit in
9a5e591ad - Two 'adapted from' credits point at sources outside the repo minor · Licensing & attribution Fixed after the audit in
a3338947e - Assertion floor counts passes, not executed assertions minor · Verification integrity Fixed after the audit in
70f0caf8c
One more finding is written in the auditors' internal shorthand, so it is not listed here. The full text is in the report inside the block. It was fixed after the audit, in a3fabd5e3.
What you still have to do
A copied file cannot own your secrets, your host or your legal obligations. This is the part the block deliberately does not claim.
- Keep STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET in your host's secret store, serve the webhook over HTTPS at the endpoint registered in Stripe, and replace the in-process fulfillment log with a unique index on session_id in your own store before you take real money. The price-trust rule and the signature check are shipped; secret custody and durable dedupe are not things a copied file can own.
- Your platform's rate limiting in front of the checkout endpoint (a Vercel firewall rule, Cloudflare, or an API gateway limit) before launch. The handler bounds a cart at 20 line items and caps metadata.cart under Stripe's 500-character limit, but it has no rate limit and no caller identity: anyone who can reach it can loop it and mint Checkout Sessions against your Stripe account.
- The rewritten code was audited in this registry on 2026-09-29, but it has never been run against Stripe, because that needs your own Stripe test keys. Everything short of Stripe's servers is tested against the real stripe package with no network (test/stripe-kit-real-sdk.test.mjs: signatures generated locally, the outgoing request read before it would leave). Before launch, exercise the money path once with a test card: run the one purchase in LIVE-TEST.md, including its price_cents:1 tamper check.
- Single merchant only: Connect fields (transfer_data, application_fee_amount) were deliberately removed.
- Order dedupe is a SEAM, not a finished feature. Stripe delivers a webhook at least once, so a retry is a real duplicate with a valid signature; `_fulfillment-log.js` ships an in-process default that stops a duplicate hitting the SAME running instance and nothing more: it does not survive a cold start or a second instance. Replace it with a unique index on session_id in your own store before you take real money (the file shows the four-line version).
What lands in your project
node
- node/create-checkout-session.js
- node/stripe-webhook.js
- node/_checkout-core.js
- node/_fulfillment-log.js
- LIVE-TEST.md
Serverless handlers (Vercel-style). Reads prices from YOUR catalog.json; the browser only sends item ids and quantities. Requires the `stripe` npm package (npm i stripe): declared in stacks.node.dependencies. The two `_`-prefixed files are helpers, not endpoints: every decision the checkout makes lives in `_checkout-core.js` (no SDK, so it is testable), and `_fulfillment-log.js` is the dedupe seam the webhook claims each session through. Vercel skips `_`-prefixed files under /api rather than publishing them as functions.
vanilla
- vanilla/cart.js
- vanilla/catalog.example.json
Cart drawer + the catalog shape. Rename catalog.example.json to catalog.json and put it where the handler reads it (../assets/catalog.json).
Needs stripe ^17 from npm.
What you supply
- STRIPE_SECRET_KEY: sk_… (test key first)
- STRIPE_WEBHOOK_SECRET: whsec_… from the dashboard endpoint
- SITE_URL: REQUIRED. Your own origin, e.g. https://your-domain.com, where a paying customer is sent back to. There is no fallback on purpose: deriving it from request headers let an attacker choose that page, and checkout fails closed with a message if it is unset.
- catalog.json: your products: id, name, price_cents (integer, >= 50), kind
Licensed MIT. It is a starting point, not a finished product.
Get it
Download the Playground See the other blocks
Nothing here is locked. The files are yours, in your folder, under a permissive license.
Questions
- Is Stripe checkout and cart audited?
- It was audited on 2026-09-29, and the verdict was not a clean pass. 11 findings have been fixed since then, and the verdict stands until the block is audited again.
- What do I still have to do myself?
- Keep STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET in your host's secret store, serve the webhook over HTTPS at the endpoint registered in Stripe, and replace the in-process fulfillment log with a unique index on session_id in your own store before you take real money. The price-trust rule and the signature check are shipped; secret custody and durable dedupe are not things a copied file can own.
- How do I get this code?
- Download the Playground, start a new app, and tick “It takes payments”. The block is written into your project as ordinary source you can read and edit.
All pre-built code blocks · Learn to build from zero · The coding guide