---
title: Take payments with Stripe
description: Checkout with your own Stripe key, verified webhooks under /webhooks/, paid orders in D1.
---

**Verdict: bring your own Stripe key; Outer never touches the money.** The app creates Checkout Sessions with its own secret key, Stripe hosts the card form and the customer pays there, and Stripe POSTs a signed event to the app's webhook endpoint. The app verifies the signature and records the paid order in D1.

The fastest path is the example: `add_example stripe-checkout` adds a one-product Checkout page plus a verified `/webhooks/stripe` endpoint that records paid orders. The rest of this page explains what it wires and why.

## Store two secrets

Never commit keys. Set them outside the code and read them as Worker env vars on the next deploy ([Secrets](/agents/security/secrets)):

- `set_secret {app, key: "STRIPE_SECRET_KEY", value: "sk_test_…"}` (MCP), or Console → Secrets. Test-mode keys start `sk_test_`; live keys start `sk_live_`.
- `set_secret {app, key: "STRIPE_WEBHOOK_SECRET", value: "whsec_…"}` — from the Stripe Dashboard, Developers → Webhooks, after adding the endpoint below.

Declare both in the server modules that read them so `env` typechecks (`env` from `cloudflare:workers` is `Cloudflare.Env`, which projects extend by redeclaration):

```ts
declare global {
  namespace Cloudflare {
    interface Env {
      STRIPE_SECRET_KEY?: string;
      STRIPE_WEBHOOK_SECRET?: string;
    }
  }
}
```

## Send buyers to Checkout

The buy action runs server-side with the secret key and redirects to Stripe — no publishable key, no client SDK, no card data through the app:

```ts
// pages/checkout.server.ts
import { env } from "cloudflare:workers";
import { defineHandler } from "void";

export const action = defineHandler(async (c) => {
  const secret = env.STRIPE_SECRET_KEY;
  if (!secret) {
    return c.json({ errors: { buy: "Payments are not configured yet." } }, 503);
  }
  const origin = new URL(c.req.url).origin;
  const res = await fetch("https://api.stripe.com/v1/checkout/sessions", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${secret}`,
      "Content-Type": "application/x-www-form-urlencoded",
    },
    body: new URLSearchParams({
      "payment_method_types[]": "card",
      mode: "payment",
      "line_items[0][price_data][currency]": "usd",
      "line_items[0][price_data][product_data][name]": "Example print",
      "line_items[0][price_data][unit_amount]": "2500",
      "line_items[0][quantity]": "1",
      success_url: `${origin}/checkout?paid=1`,
      cancel_url: `${origin}/checkout?canceled=1`,
    }),
  });
  if (!res.ok) {
    return c.json({ errors: { buy: "Couldn't start checkout — try again." } }, 502);
  }
  const session = (await res.json()) as { url?: string };
  if (typeof session.url !== "string") {
    return c.json({ errors: { buy: "Couldn't start checkout — try again." } }, 502);
  }
  return c.redirect(session.url);
});
```

Amounts are integer cents (`2500` is $25.00). The success page state (`?paid=1`) is a courtesy — the D1 order row written by the webhook is the record.

## Verify every webhook

Register the endpoint in the Stripe Dashboard as `https://<slug>.outer.now/webhooks/stripe`, listening to `checkout.session.completed`. Every POST carries a `Stripe-Signature` header: `t=<unix seconds>,v1=<hex HMAC-SHA256 of "t.payload" keyed by the webhook secret>`, possibly with several `v1` values during a rotation. Verification is mandatory — an unverified endpoint lets anyone forge a paid order. Copy the shape from the example (`pages/_stripe.ts` plus `routes/webhooks/stripe.ts` in `add_example stripe-checkout`): the HMAC runs on the raw body with WebCrypto, no SDK.

Rules: read the raw body with `c.req.text()` (parsed JSON re-serialises differently and breaks the HMAC); reject stale timestamps past 5 minutes; accept any live `v1` during a rotation; compare in constant time; answer 400 on a bad signature or malformed JSON so Stripe shows the failure instead of a dropped event; make the insert idempotent (session id UNIQUE, `INSERT OR IGNORE`) because Stripe retries unanswered events.

## The webhook path rule

A PIN/password gate (the visitor gate) would otherwise answer Stripe's POST with the unlock page, so payments could never be confirmed. Machine requests to webhook paths bypass the visitor gate — and nothing else:

- Only paths under `/webhooks/` (plus the bare `/webhooks`), on every host that serves the app. The path must be canonical — no `.`/`..` segments, no `//`, no backslashes, no percent-encoded dots, slashes or backslashes — or the request stays gated.
- Only POST, PUT, PATCH and DELETE. A GET on a webhook path still gets the lock page, so browsers and crawlers never see app content there; webhooks are server to server, so there is no preflight to allow.
- The bypass only forwards: no unlock cookie is granted or minted. A forged POST reaches the app exactly like any other forwarded request, which is why signature verification is mandatory.

The adults-only gate has no bypass: an 18+ app still asks Stripe's POST for age confirmation, so webhooks only work on apps without it. The D1 insert is the record, and the bypass wakes a sleeping fleet first: the first delivery pays that wake cost, and Stripe retries a failed delivery — but keep the checkout page light so the wake usually starts from the buyer's visit, not the webhook.
