Article · 8 min read · Plans and costs

Setting up billing with Stripe

For whoever runs kubespin. How to connect a Stripe account so people can pay for a plan. Written against 0.104 (billing arrived in 0.66.0; spec plans-v2 PL15–PL23).

For whoever runs kubespin. How to connect a Stripe account so people can pay for a plan. Written against 0.104 (billing arrived in 0.66.0; spec plans-v2 PL15–PL23).

How it fits together

  • People pay on Stripe Checkout and manage cards, invoices and cancelling in Stripe’s customer portal. kubespin never sees card details.
  • kubespin creates a Stripe Product for each plan and its Prices (monthly and yearly; per member for Team), in GBP, from the amounts in Admin › Plans. You don’t make products or prices in Stripe yourself.
  • Stripe tells kubespin what happened through a signed webhook. Each event is handled once.
  • The two secrets live only in the server’s environment, never in the database or a screen: STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET (or STRIPE_SECRET_KEY_FILE and STRIPE_WEBHOOK_SECRET_FILE, files holding them).
  • Admin › Plans › Billing with Stripe shows whether each secret is set, the webhook address, whether the paywall is on, and each plan’s state, with Sync prices to Stripe.

Before you start: test or live?

Do the whole set-up in test mode first, then repeat it in live mode. Test and live are separate in Stripe: different keys, different webhook secrets, and different product and price IDs.

Moving from test to live. A sync stores each plan’s Stripe product and price IDs on the plan. A later sync with a live key reuses those test IDs, which don’t exist in live mode, and fails. Until kubespin handles this, clear the stored IDs before the first live sync:

sql
UPDATE plans SET stripe = NULL;

and the add-on prices kept in the settings table under addon_prices. Then sync with the live key. (Backlogged: keep test and live IDs apart.)

1. Your Stripe account

  1. Activate the account (Settings › Business details): business type, address, bank account for payouts. Test mode works before this; live payments don’t.
  2. Branding (Settings › Branding): logo, colours and the name people see on Checkout, receipts and the portal.
  3. Emails (Settings › Customer emails): turn on receipts for successful payments and refunds.
  4. Tax (optional): kubespin doesn’t turn on Stripe Tax for you. If you’re VAT-registered, decide with your accountant how VAT is shown; Stripe Tax is set up under Settings › Tax.

2. The API key

  1. In Stripe, open Developers › API keys (in the newer dashboard: Workbench › API keys).

  2. Prefer a restricted key named “kubespin”, with these permissions:

    Resource Permission
    Products Write
    Prices Write
    Customers Write
    Checkout Sessions Write
    Customer portal Write
    Subscriptions Write
    Charges Read (chargebacks)
    Identity verification sessions Read (only if you use Stripe Identity)

    A standard secret key (sk_test_… / sk_live_…) also works, but can do far more.

  3. On the kubespin host, add it to the environment file next to docker-compose.prod.yml:

    bash
    STRIPE_SECRET_KEY=rk_test_...
    

    Then restart the API so it reads it:

    bash
    docker compose -f docker-compose.prod.yml up -d api
    
  4. In kubespin, Admin › Plans › Billing with Stripe now shows Secret key: Set.

3. The webhook

  1. In kubespin, copy the webhook address shown under Billing with Stripe. It’s your kubespin’s address followed by /api/v1/billing/webhook, for example https://kubespin.io/api/v1/billing/webhook.

  2. In Stripe, open Developers › Webhooks (newer dashboard: Workbench › Webhooks) and Add destination (or Add endpoint). Choose Your account, events from your account (not connected accounts), and the latest API version.

  3. Select these events:

    Event What kubespin does
    checkout.session.completed Links the new subscription to the account
    customer.subscription.created Sets the account’s plan
    customer.subscription.updated Follows plan changes, renewals and add-ons
    customer.subscription.deleted Moves the account to Free; nothing is deleted
    invoice.paid Clears a failed-payment warning
    invoice.payment_failed Warns the account, then pauses as plans-v2 describes
    charge.succeeded Trust and safety: risky or shared cards (TS3)
    charge.dispute.created Trust and safety: records a chargeback (TS7)
    identity.verification_session.verified Only with Stripe Identity (TS3)
    identity.verification_session.requires_input Only with Stripe Identity (TS3)
  4. Paste the webhook address as the endpoint URL and save.

  5. Open the new endpoint and reveal the signing secret (whsec_…). Add it to the host’s environment and restart the API:

    bash
    STRIPE_WEBHOOK_SECRET=whsec_...
    
  6. kubespin now shows Webhook secret: Set.

kubespin refuses any webhook whose signature doesn’t match, or whose timestamp is more than five minutes off, so check the host’s clock if every delivery fails with “Bad signature”.

4. The customer portal

People use Stripe’s portal for cards, invoices and cancelling (Manage billing in kubespin).

  1. Open Settings › Billing › Customer portal and activate it (test and live separately).
  2. Turn on: update payment methods, view invoice history, update billing details.
  3. Cancellations: allow cancelling at the end of the billing period. When the subscription ends, kubespin moves the account to Free.
  4. Switching plans: leave off. People change plan in kubespin, which checks the new plan’s limits first and handles upgrades now and downgrades at the period end.
  5. Add your terms and privacy links when they’re published.

5. Prices

  1. In kubespin, set each plan’s prices in Admin › Plans (price a month, a year, or per member for Team).
  2. Choose Sync prices to Stripe. kubespin creates a Product called “kubespin ” with its Prices, and the add-ons’ prices. Each plan shows In Stripe when it has every price it needs.
  3. Changing an amount later and syncing again makes a new Price and archives the old one (Stripe prices can’t change). Existing subscribers keep the old amount until they change plan.

Don’t edit or delete these products and prices in Stripe by hand: kubespin keeps them in step.

6. Turn on the paywall, then test

Plans can only be bought with the paywall on. While PAYWALL_ENABLED=false, everyone has every feature and Settings says so instead of showing plans. Turning it on also starts enforcing trials and plan limits for everyone (over-limit servers are paused, never cut off), so test on a staging kubespin if you have one, or turn it on when you’re ready for limits to apply.

bash
PAYWALL_ENABLED=true

Restart the API, then:

  1. Sign in as an ordinary account (not a superadmin) and open Settings: the plan and the choices are on the first tab. Choose a plan; Stripe Checkout opens.
  2. Pay with Stripe’s test card 4242 4242 4242 4242, any future expiry, any CVC.
  3. You return to Settings, and the account shows the new plan.
  4. In Stripe, the webhook endpoint’s Event deliveries shows each event with 200. kubespin’s reply says what it did (for example "result": "paid").
  5. Try Manage billing to open the portal, and a failed payment with 4000 0000 0000 0341 (the card is accepted, then the next charge fails).

7. Go live

  1. Repeat sections 2–5 with live keys: a live key, a live webhook endpoint and its own whsec_…, and the live customer portal. Clear the stored test IDs first (see the box at the top).
  2. Sync prices again, check every plan shows In Stripe, and make one real purchase on a cheap plan, then refund it in Stripe.

Troubleshooting

What you see Why
“Billing isn’t set up: STRIPE_SECRET_KEY is missing.” The key isn’t in the API’s environment, or the API wasn’t restarted.
“ has no monthly price in Stripe yet. Sync the plans first.” The plan has no price for that interval in Stripe: set it in Admin › Plans and sync.
Webhook deliveries fail with 400 “Bad signature.” Wrong STRIPE_WEBHOOK_SECRET (test and live differ), or the host’s clock is off.
Webhook deliveries fail with 502 Stripe refused a call kubespin made while handling the event; Stripe retries. Read the reply body.
Sync fails after switching to live Test IDs stored on the plans: see the box at the top.