Billing, captcha and sign-up
Turn on billing with Stripe and a captcha with Turnstile, and choose who may sign up.
A new server has billing and the captcha turned off, and lets anyone sign up. Each section below says what to set to change that.
Billing
Billing is off until you set STRIPE_SECRET_KEY. Until then there are no plans, nothing is limited, and the workspaces’ billing page says so.
With a key, every workspace starts on the free tier, with 2 connected accounts across its billing account. A monthly Stripe subscription to per-account pricing gives it any number of accounts, billed by the account and by use. The plans priced by channels are no longer sold. Plans and billing explains both, with what each one limits, and how ordering, cancelling and withdrawing work.
Billing takes these settings, on the server and the web app:
| Setting | Where | What it does |
|---|---|---|
STRIPE_SECRET_KEY |
Server | Turns billing on |
STRIPE_WEBHOOK_SECRET |
Server | The signing secret of the webhook endpoint |
STRIPE_LOOKUP_KEY_PREFIX |
Server | The prefix of the catalogue’s lookup keys |
PEAL_BILLING_GRACE_UNTIL |
Server | The day until which workspaces over the free tier keep publishing on the accounts they had |
PEAL_TERMS_URL |
Web app | Your terms of service, which the billing page links to before an order |
PEAL_CONTACT_URL |
Web app | Where people write for more than the largest plan |
Environment variables has the server’s settings with their defaults, and also the web app’s and the site’s.
What counts toward the limits
Each limit counts in its own way:
- Posts count in the calendar month (UTC) they go out in, once they’re scheduled. Drafts and cancelled posts don’t count.
- A post taken back to draft gives its place back, and a post sent again on a channel counts once.
- Members count together with the invitations still pending when you invite someone.
- Pages or Instagram accounts ticked together need room for all of them, or none is connected.
- Media count by the bytes stored, uploads in progress included.
A limit applies when something is added. A workspace on a smaller plan than it needs keeps what it has, but schedules nothing while it has more channels than its plan allows.
Set up Stripe
Give Stripe Peal's catalogue
With STRIPE_SECRET_KEY set, one command makes the per-account pricing (The catalogue). Run it from the repository, or with the server’s image and settings as in With Docker Compose and Caddy, so the key stays on the server:
bun nx run peal-server:stripe-cataloguedocker compose --project-name peal --file compose.production.yaml \
--file compose.self-host.yaml --env-file /srv/peal/peal.env \
run --rm --no-deps server packages/peal-server/scripts/stripe-catalogue.tsKeep the old plans' prices, if any
A new Stripe account needs none, because the plans priced by channels are no longer sold. If a subscription to one still runs on your server, keep their prices, as The plans’ prices says.
Add the webhook endpoint
Add a webhook endpoint for PUBLIC_URL/api/auth/stripe/webhook with the customer.subscription.created, customer.subscription.updated, customer.subscription.deleted, checkout.session.completed and refund.failed events. The last one lists a refund that failed for staff. Set STRIPE_WEBHOOK_SECRET to the endpoint’s signing secret. If something guards the rest of /api/auth, leave this path open to Stripe.
Add the meter errors destination
Add an event destination for PUBLIC_URL/api/stripe/meter-errors, with thin events as its payload and the v1.billing.meter.error_report_triggered and v1.billing.meter.no_meter_found events. Set STRIPE_METER_WEBHOOK_SECRET to its signing secret. Stripe sends these events when it took meter events it couldn’t count, and that usage then isn’t billed. Without the destination, nobody hears of it. Leave this path open to Stripe too.
Turn on Stripe Tax and the customer portal
Checkout always has Stripe Tax add the VAT, because the prices are tax-exclusive and Peal shows each one excluding VAT and with France’s VAT.
Turn on the customer portal, where members update their payment method and download invoices. Leave out retention offers, and turn plan switching (subscription updates) and cancellation off. Plans change and end on the billing page only, where each change takes an order.
Then set the web app’s PEAL_TERMS_URL and PEAL_CONTACT_URL.
Match your tax and invoicing duties
Peal leaves these to Stripe:
- In Stripe Tax: your origin address, a registration for each place you collect tax in (for an EU seller, the home country and the One-Stop Shop), and the product tax code of each plan’s product.
- In the invoice settings: your legal mentions (company identifiers, VAT number, footer), a numbering scheme, and emailing customers their invoices and receipts.
- In the customer portal: the invoice history, so members can download their invoices from Billing.
The tax code is software as a service: txcd_10103001 for business use, or txcd_10103000 for personal use. The command gives the catalogue’s products txcd_10103001 when it makes them. The split between business and personal use matters for US sales tax, which is a question for your accountant.
Checkout then asks every customer for a billing address. It also offers a field for a business’s tax id, which Stripe prints on the invoice and uses for the reverse charge.
The catalogue
A server that bills checks Peal’s catalogue when it starts:
- A meter or a price that’s missing or bills differently, or a price on an archived product, stops the server. It lists what differs, and the command that gives the catalogue.
- The prices of the plans priced by tiers of channels, which are no longer sold, are checked only while a subscription to one still runs. A new Stripe account needs none.
- If Stripe doesn’t answer, the server starts and logs a warning.
- The server reads the catalogue only as it starts. After you run the command, restart any server that’s already running.
Per-account pricing is a meter, a product and a metered price for each line of the rate card, in US dollars, tax-exclusive and billed monthly. Their lookup keys are also the meters’ event names. They’re these, or they start with the prefix set in STRIPE_LOOKUP_KEY_PREFIX:
| Lookup key | Line of the rate card |
|---|---|
peal_accounts |
Connected accounts, on graduated tiers by account-day |
peal_assistant_requests |
Writing assistant requests |
peal_assistant_images |
Writing assistant images |
peal_outbound_messages |
Inbox messages sent |
peal_managed_ads |
Managed ads |
peal_x_api |
X API usage |
The command’s key needs write access to meters, products and prices. The command prints what it makes, by name and id, and changes nothing when you run it again. With --check, it only compares, and fails when something would change.
When the rate card's amounts change, and rolling back
The command makes a new price that takes the lookup key, and leaves the old one active for the subscriptions on it. The release before then no longer finds its own price under that key, and doesn’t start. To roll back, run the previous release’s stripe-catalogue, which moves the key back, then start that release.
Product names and tax codes
The command gives each product it makes its name and the tax code txcd_10103001, and never changes them afterwards. If you rename a product or give it another tax code at Stripe, the command says so and leaves them as they are.
When the command stops
It stops, changing nothing, on a price whose product is archived. Restore the product. It also stops on a meter that counts differently from what Peal sends, because Stripe can’t change a meter. Deactivate the meter at Stripe and run the command again. From then on, subscriptions on a price that reads the old meter stop counting until they move to the new price.
If Stripe refuses the new meter the old one’s event name, set STRIPE_LOOKUP_KEY_PREFIX to a new prefix. Then give Stripe the catalogue and the plans’ prices under it.
The plans’ prices
The plans priced by channels are no longer sold, and a new Stripe account needs none of their prices. The server checks them only while a subscription to one still runs.
In that case, keep a product for each paid plan, with a monthly and a yearly EUR price, both tax-exclusive. Their lookup keys are peal_<plan>_monthly and peal_<plan>_yearly (peal_starter_monthly, peal_pro_yearly…), with the same prefix.
Their amounts are those of Plans and billing, and they must stay equal. The server checks that each price exists, is active, costs what Peal publishes at the right interval, and is tax-exclusive.
How subscriptions behave
Subscriptions follow these rules:
- Owners and admins order, cancel and withdraw from the workspace’s billing page, in their own session. Staff impersonating them, API keys and OAuth clients can’t.
- A checkout starts only for an order placed there in the last 30 minutes, which records the request to start at once.
- A subscription’s state comes only from its webhook events, checked against the signing secret.
- An event sent twice is recorded once, and an older event never replaces a newer one.
- A subscription stays paid for while Stripe retries a failed payment. Unpaid, paused or cancelled, the workspace is back on the free tier.
Peal offers no new trials. A trial that already started is set to end, cancelled with its trial, even if a payment method is added, so it never turns into a charge. Its workspace is then on the free tier. A paid plan ordered during the trial ends the trial once it’s paid for.
Every hour, the billing.reminders job sends reminders for the trials that end within three days, and for the yearly plans priced by channels that renew within 45 days. A renewal is always reminded more than a month ahead.
Cancelling and withdrawing
Members who manage billing cancel a contract or withdraw from one on the workspace’s Billing page, with Cancel your contract and Withdraw from contract here: contracts are made in the web app, so the web app is where they end. The web app also serves both as forms that need no signing in, at /cancel and /withdraw. Peal links to them from nowhere: link to them from your own pages if you want them public.
POST /api/contract-requests records each request with the moment it came in, which is the moment that counts whenever the request is applied. It acknowledges the request by email. It takes 5 requests an hour from an address (read from AUTH_IP_HEADERS), and a Turnstile token when Turnstile is on. It writes to one email address 5 times a day at most.
When a request is applied depends on what it gives:
| Request | Applied |
|---|---|
| Gives the contract number | At once |
| Comes from an address that manages a workspace’s billing | From the link in its acknowledgement |
| Any other | Waits in the staff console (Staff console) |
Every five minutes, the billing.contract-requests job takes up what the mail or Stripe couldn’t take. Requests are kept with their workspace, then for 5 years after it’s deleted. Those that no contract was found for are kept 5 years from their receipt.
Captcha
With TURNSTILE_SITE_KEY and TURNSTILE_SECRET_KEY set (Environment variables), signing up and asking for a password reset need a Cloudflare Turnstile token. The forms show the widget, and the server checks each token with Cloudflare before going on. Signing in relies on its rate limits instead.
Add your public address to the widget’s hostnames in Cloudflare. You can try it without a real widget, with Cloudflare’s test keys.
Who may sign up
Anyone who reaches your instance can make an account, then a workspace, and that workspace uses your platform apps and your writing assistant’s requests. To run Peal for your own people only, set PEAL_SIGN_UP=invitation (Environment variables).
An account can then be made only from an invitation’s link, while the invitation is pending, with the address it went to. The link is the one its email carries, or the one Copy link gives. Knowing an invited address isn’t enough.
Anyone else sees a sign-up page that says accounts are made by invitation. The server refuses every other sign-up the same way, so its answer never tells whether an address was invited.
Each person you let in can make a workspace of their own and invite anyone to it, and those people can do the same. Invitation mode trusts everyone it lets in to invite others.
Signing up through a sign-in provider needs the invitation’s link too, and the address the provider confirmed must be the one the invitation went to.
Signing in, password resets, accepting an invitation, the staff console, partner accounts and connect links work as before.
The first account
While the server has no account, the first one can be made without an invitation, so you can set up a new server. The sign-up page shows its form until then. An account counts as the first even before its address is confirmed.
If the first account can't be confirmed
The address may be mistyped, email may not work yet, or a stranger may have taken the slot first. Then nobody can sign in to invite anyone. As long as no account is confirmed, delete the unconfirmed ones, then sign up again:
docker compose --project-name peal --file compose.production.yaml \
--file compose.self-host.yaml --env-file /srv/peal/peal.env exec postgres \
psql -U peal -d peal -c 'DELETE FROM auth."user" WHERE NOT email_verified AND NOT EXISTS (SELECT 1 FROM auth."user" WHERE email_verified);'The command deletes nothing once an account is confirmed.