Billing
Version 0.0.1 · Owners: @benallfree
Billing lets an app sell. You list products, each with a price at your payment processor and what it grants (instances: 1,
seats: 5). Signed-in users, or visitors when pay-first is on, check out. Billing keeps every subscription and order
in step with the processor, and your code asks one question: what does this owner have?
Processors are separate extensions that provide payments/v1, such as Stripe, Paddle or Creem. Several can be installed at once; one takes new checkouts, and the others keep renewing what was bought through them.
Install · Configure · Use · Integrations · Example
Install
extkit.Setup(app, []extkit.Extension{settings.New(), billing.New(billing.Config{}), stripe.New(stripe.Config{})}, nil, nil)
The module is pocketone.dev/ext/billing. It adds four collections: _billing_products, _billing_items (one per
subscription, order or manual grant), _billing_events (every webhook) and _billing_checkouts.
Configure
Open Billing → Settings in the dashboard. Each processor's own settings section is on the same page.
| Setting | What it does |
|---|---|
| Checkout processor | Which installed processor takes new checkouts. Leave it empty when only one is installed. |
| Owner collection | The auth collection whose records buy (users). |
| Signed-out checkout (pay first) | Visitors can buy before they have an account. The purchase goes to the owner with the checkout email, matched without regard to case, or to the account your app makes for a new buyer in OnOwnerNeeded; the checkout returns a claim token for your app's welcome email. |
| Success URL, Cancel URL | Where the processor sends buyers back. Both default to the Application URL. |
| Proration | How quantity and plan changes are charged when the caller doesn't say: now, at the next period, or not at all. |
| API URL | The base of webhook and checkout-page URLs when the Application URL doesn't reach this app's API. |
| Reconcile daily | Re-reads every item that hasn't ended from its processor at 04:17 UTC and fixes anything that drifted. |
Copy each processor's webhook URL from the settings page into the processor's dashboard.
Products live in _billing_products. Add them under Billing → Products. prices maps a processor to its price
id ({"stripe": "price_123"}), and grants lists what one unit grants ({"instances": 1}). Check prices asks
each processor whether the price exists and matches the product's kind.
Use
In page code and pb_hooks, $billing acts as the auth record you pass it (ctx.auth in Pages), with that user's
rights:
const { totals } = $billing.entitlements(ctx.auth) // totals.instances === 2
const { url } = await $billing.checkout.async(ctx.auth, { product: "pro" })
return ctx.res.redirect(url)
items and portal work the same way; checkout and portal call the processor, so page code awaits their .async
forms. The guides on pocketone.dev (Billing) cover checkout, pay first, entitlements and processors.
The billing/v1 contract is the API. From the browser:
// signed in: start a checkout and send the browser to its URL
const { url } = await pb.send("/api/_c/billing/v1/checkout", { method: "POST", body: { product: "pro", quantity: 2 } });
location.href = url;
// what the signed-in user has
const { totals } = await pb.send(`/api/_c/billing/v1/entitlements?collection=users&owner=${pb.authStore.record.id}`);
// totals.instances === 2
Owners manage their own subscriptions with portal, cancel, resume, quantity and plan. Superusers can also
refund, grant, revoke, sync and reconcile. In Go, billing.For(app).Entitlements(collection, owner) gives
the same totals, and billingv1.OnItemChanged(app) fires after any item changes, with the entitlements before and
after, so you can provision or tear down what was paid for.
A webhook never trusts its payload. Billing reads the subscription or order back from the processor and writes what it finds. Webhooks can arrive twice or out of order and the result is the same.
In the dashboard, Billing shows recent changes and webhook errors. Each customer page lists their entitlements and items, with actions (cancel, resume, change quantity, sync, refund) and manual grants for comps and support.
Integrations
| Extension | How they fit |
|---|---|
| Settings | Billing's settings, and every processor's, are settings/v1 sections on the Billing → Settings page. |
| KV | The settings are stored in the key-value store. |
| Contracts | billing/v1 is what apps call; payments/v1 is what processors provide. |
Example
// Command example runs one PocketOne app with billing: a Billing page in the dashboard, two products, and a demo
// payment processor (demo.go) whose checkout page has a Pay button, so the whole flow runs with no account anywhere.
//
// go run ./example serve
//
// Then, as [email protected] (an app user), POST /api/_c/billing/v1/checkout {"product": "pro", "quantity": 2},
// open the URL it returns, press Pay, and see the item and its entitlements on Billing → Customers.
//
// Logins (ext/devusers, local testing only): dashboard [email protected] / password, app user [email protected] / password.
package main
import (
"log"
"ganigeorgiev.com/pocketbase/pocketbase"
"pocketone.dev/ext/billing"
"pocketone.dev/ext/devusers"
"pocketone.dev/ext/extkit"
"pocketone.dev/ext/settings"
)
func main() {
app := pocketbase.New()
devusers.Register(app) // local testing: superuser [email protected] and user [email protected], password "password"
if _, err := extkit.Setup(app, []extkit.Extension{settings.New(), billing.New(billing.Config{}), demoProcessor{}}, nil, nil); err != nil {
log.Fatal(err)
}
seedProducts(app)
if err := app.Start(); err != nil {
log.Fatal(err)
}
}
License
MIT
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.