# stripe

Payments, billing and marketplaces with Stripe, over its HTTP API: no
package, and no Stripe.js unless the program takes the one optional file
that draws Stripe's card form in its page. The whole of it (the calls, the
kinds of failure, idempotency, the plans kept from the code, standing and
`stripeCan`, usage, the events, Connect, a restricted key's rights, the API
version, checking it in test mode) is [docs/stripe.md](../../docs/stripe.md).

```toml
# mote.toml
[packages]
stripe = "../mote/extensions/stripe"
```

```text
use "pkg:stripe/webhooks.mote"      // the API, billing, payments, POST /stripe/webhook
use "pkg:stripe/connect.mote"       // sellers (optional)

fn stripePlans(): StripePlanSpec[]  // the plans, once, in the code
  return [{key: "pro", name: "Pro", features: ["export"], prices: [{key: "pro_month", amount: 1200, interval: "month"}]}]

StripeButton price="pro_month" label="Go Pro" mode="subscription"

server fn export(): bytes requires signedIn
  if !stripeCan(auth.me()?.id ?? "", "export")? fail("That is on Pro.", {kind: "refused"})?
  …
```

Settings: `STRIPE_KEY`, `STRIPE_WEBHOOK_SECRET` (secrets; with Connect,
`STRIPE_CONNECT_WEBHOOK_SECRET`). Optional: `STRIPE_SYNC=start`,
`STRIPE_CATCH_UP=start`, `STRIPE_TAX=auto`, `STRIPE_GRACE_DAYS` (7),
`STRIPE_PRICES`, `STRIPE_FETCH_FRESH`, `STRIPE_METER_BATCH`,
`STRIPE_WEBHOOK_TOLERANCE`, `STRIPE_VERSION` (pinned: 2024-06-20, in
`stripePinned`), `STRIPE_API`, `STRIPE_FILES_API`, `STRIPE_PUBLISHABLE_KEY`
(elements.mote).

| File | |
|---|---|
| `stripe.mote` | The core: one place that talks to Stripe (`stripeCallOn`: a form with bracket keys, the key as a bearer, `Stripe-Version`, `Stripe-Account` when asked, an idempotency key on every write, tried again on 429 and 5xx), Checkout in every mode, the portal, customers, the catalog, subscriptions, payment intents, refunds, the plans' shapes, `stripeBuy`, `StripeButton`. |
| `billing.mote` | `stripeSync`; subscriptions made, changed (now, charged now, at the period's end), previewed, seats, paused, cancelled with why; coupons, promotion codes; invoices, credit notes, tax ids, the balance; usage on meters; test clocks. |
| `payments.mote` | Saved ways of paying, charges off-session and the page that confirms one, payment links, disputes and files, the balance, payouts. |
| `webhooks.mote` | The route; each event kept once, answered at once, its object read again by a job; `StripePlan`, `stripeStanding`, `stripeCan`, `stripeCatchUp`; the hooks. |
| `connect.mote` | Sellers' accounts, onboarding, `StripeAccount`, destination and direct charges, transfers and reversals, fees, Connect's own endpoint. |
| `admin.mote` | `StripeAdmin`, the billing panel (`pkg:admin/stripe.mote` puts it in the admin panel). |
| `testing.mote` | For a program's own `mote test`: events built and signed as Stripe signs them. |
| `elements.mote`, `elements.js` | **Optional**, and the only script of Stripe's: the Payment Element, through `extern script`. |
| `tests.mote` | Its tests. |

**Covered**, against how products use Stripe: subscriptions sold by
Checkout and managed in the portal; plans in the repository; trials (with
and without a card), coupons and codes, upgrades charged now and shown
first, downgrades at the period's end, seats, pausing, cancelling at the
end and taking it back, several subscriptions per customer, several
currencies; usage-based prices on meters; invoices of the program's own,
credit notes, automatic tax, tax ids, the customer's credit; dunning
mapped to what the person may do, with a grace; saved cards and charges
when the person is not there, with the bank's confirmation on a page of
Stripe's; payment links; disputes with evidence; the balance and payouts;
Connect with Express and Standard accounts, destination, direct and
separate charges, fees, reversals; every event a product handles, read
fresh and caught up after downtime; test clocks; a billing panel.

**Left out on purpose**: embedded Checkout and Connect's embedded
components (Stripe's script in the page), Custom connected accounts'
onboarding, quotes, Issuing, Terminal, Treasury, Identity, Radar rules,
reporting, the Tax calculation API, the v2 APIs (the meter event stream,
v2 accounts), and one billing facade over Stripe and the others
([docs/paypal.md](../../docs/paypal.md) says why).

**Not yet run against the real Stripe**: only against a stand-in of its API
(`tests/svc_standin.mjs`, `tests/all.sh real_svc_stripe`) and the
extension's own tests. The endpoints and fields are written from Stripe's
API reference for version 2024-06-20, with the readers taking the
2025-03-31 shapes too; docs/stripe.md has the ten-minute check in test mode
and says which fields I was unsure of.
