Billing
Billing lets users pay for a tier through Stripe. This page covers how to connect Stripe, test it, and go live.
Billing is off by default. Until you save a provider in /admin/billing, every /api/v1/billing/* route returns 404 and users see no plan or upgrade options.
You do not need billing to use tiers. You can create tiers and assign them to users by hand. See Tiers.
Before you start
You need:
- The admin panel turned on.
- A public HTTPS address for the server, so Stripe can send webhooks to it. For local testing, use the Stripe CLI:
stripe listen --forward-to localhost:3848/api/v1/billing/webhook. - A Stripe account. Use test mode (
sk_test_...keys) until everything works.
1. Create your tiers
In /admin/billing, under Billing tiers, create an unpaid tier with the ID free, then your paid tiers. To start from the built-in Free and Pro tiers, click Load the built-in free/pro catalog.
See Tiers for the details.
2. Create the Stripe prices and webhook
civ api billing init creates what Stripe needs and prints the values for step 3:
- a product and a recurring price for the built-in
protier, - a product and a per-unit price for the built-in
blob-100add-on (100 GB of extra blob storage), - a webhook endpoint for your server.
export STRIPE_SECRET_KEY=sk_test_...
civ api billing init \
--webhook-url https://your-server/api/v1/billing/webhook \
--price pro=900
Provisioning Stripe billing (test mode)
Pro: price_1QabcdEXAMPLE (9.00 USD/month)
+100 GB storage: price_1QefghEXAMPLE (2.00 USD/month per unit)
Webhook endpoint: we_1QabcdEXAMPLE
Configure /admin/billing with:
Provider: stripe
Secret key: (the same sk_… key used here)
Webhook secret: whsec_EXAMPLE...
Tier prices:
pro = price_1QabcdEXAMPLE
Add-on prices:
blob-100 = price_1QefghEXAMPLE
Save the webhook secret. Stripe shows it only when the endpoint is created. If you run the command again for the same URL, it reports the existing endpoint and prints no secret. To get a new secret, delete the endpoint in the Stripe dashboard and run the command again.
Running the command again does not create duplicate products or prices.
The command only talks to Stripe. It does not read your server's tiers. For a tier other than pro, create a recurring price in the Stripe dashboard and use its price_... ID in step 3.
| Flag | Default | What it sets |
|---|---|---|
--stripe-key |
STRIPE_SECRET_KEY |
The Stripe secret key. The environment variable is safer: a flag is visible in the process list. |
--webhook-url |
none | Your server's webhook address, ending in /api/v1/billing/webhook. Required unless you pass --no-webhook. |
--price |
pro=900, blob-100=200 |
The amount for a tier or add-on, in the currency's smallest unit (cents). Can be repeated. |
--currency |
usd |
ISO 4217 currency code. |
--interval |
month |
month or year. |
--no-webhook |
off | Do not create the webhook endpoint. |
--json |
off | Print { provider, prices, addonPrices, webhookSecret, webhookExisted } as JSON. |
If you create the webhook yourself, subscribe it to these six events:
customer.subscription.created
customer.subscription.updated
customer.subscription.deleted
invoice.payment_succeeded
invoice.payment_failed
customer.deleted
3. Enter the settings in the admin panel
Open /admin/billing and fill in the form.
| Field | Value |
|---|---|
| Billing provider | Stripe. Disabled turns billing off. |
| Secret key | The sk_... key from step 2. |
| Webhook signing secret | The whsec_... value from step 2. |
| Tier prices | For each paid tier, its price_... ID. A paid tier with no price cannot be bought. |
| Add-on prices | For each add-on, its per-unit price_... ID. Leave blank to not offer the add-on. |
Click Save billing settings. The change applies right away, with no restart.
- The two secrets are encrypted with
CIVILITY_AUTH_SECRETand never shown again. Leave a secret field blank to keep the stored value. - Clearing a price field removes that price.
- This form and the Billing tiers forms below it save separately.
4. Test it
- Sign in as a normal user and open
/settings/billing. The Plan & Billing card shows the current plan and an Upgrade to Pro button with the price. - Click the button. You are sent to Stripe Checkout. Pay with a test card such as
4242 4242 4242 4242, any future expiry date and any CVC. - Go back to
/settings/billing. The plan is now Pro, and a Manage billing button opens the Stripe customer portal. - In the Storage card, add a 100 GB block. Stripe charges the prorated difference, and the user's blob quota grows by 100 GB per block.
- In the Stripe dashboard, cancel the subscription.
- Cancel at period end: the user stays on the paid tier until the period ends.
- Cancel now: the user moves to
freeright away.
In the Stripe dashboard, open Webhooks and check that each delivery got a 200.
5. Go live
- Run step 2 again with your live
sk_live_...key and your production webhook address. The command printsLIVE mode. - Enter the live secret key, webhook secret and price IDs in
/admin/billing. - Open
/admin/maintenanceand check that Storage usage snapshots and Scheduled subscription downgrades are available and enabled. See Maintenance.
How Stripe changes a user's tier
The server updates the user when Stripe sends a webhook.
| Stripe event | What the server does |
|---|---|
| Subscription created or updated | Sets the user's tier from the subscription's price, and records any add-on blocks. |
| Subscription set to cancel at period end | Records the end date. The Scheduled subscription downgrades job moves the user to free after that date. |
| Subscription deleted | Moves the user to free and removes their add-on blocks. |
| Customer deleted | Moves the user to free and removes the link to the Stripe customer. |
| Payment failed | Emails the user, if email is set up. |
If a subscription's price is not in Tier prices, the server keeps the user's current tier and writes an error to the server log. Add the price in /admin/billing.
The scheduled downgrade only runs if the server's scheduler is on. See Maintenance.
Add-on blocks are part of the subscription and end with it. Add-ons need a paid tier.
To set a tier without Stripe, use the Tier field on the user's page in the admin panel. See Tiers.
Webhook errors
Each webhook is handled once, even if Stripe sends it twice. The server answers 200 except in these cases, where Stripe will retry:
| Response | Cause |
|---|---|
400 |
The signature is wrong. Check the Webhook signing secret. |
404 |
Billing is disabled. |
503 |
The event is for a Stripe customer the server has not linked to a user yet. It succeeds on a retry, once the "subscription created" event has arrived. |
500 |
The server failed while handling the event, for example a database error. |