billing/v1
Selling from an app, whatever processor takes the money. The provider keeps who bought what (items: a
subscription, an order or a manual grant, each owned by an auth record), turns processors' webhooks into item changes,
sums what each owner's active items grant (entitlements, such as instances = 5), and raises an event after every
change so the app can react. Processors are payments/v1 providers.
A wire contract (billing.go) at /api/_c/billing/v1/. Products and items are records in the provider's
collections, edited with PocketBase's records API or the dashboard's Billing page.
Use
| Operation | HTTP | Who |
|---|---|---|
| Checkout | POST /api/_c/billing/v1/checkout {"product": "pro", "quantity": 1} |
anyone; signed out only when the app allows pay-first |
| Pay | GET /api/_c/billing/v1/pay?checkout=<id> |
anyone (the page some processors need) |
| Portal | POST /api/_c/billing/v1/portal {"item": ""} |
the owner, or a superuser |
| Items | GET /api/_c/billing/v1/items |
the owner (their own), or a superuser (?owner=) |
| Entitlements | GET /api/_c/billing/v1/entitlements |
the owner, or a superuser |
| Cancel, Resume, Quantity, Plan | POST /api/_c/billing/v1/cancel (resume, quantity, plan) |
the owner, or a superuser |
| Webhook | POST /api/_c/billing/v1/webhook?provider=stripe |
the processor (checked by its signature) |
| Refund, Grant, Revoke, Sync, Reconcile | POST /api/_c/billing/v1/refund (grant, revoke, sync, reconcile) |
superusers (billing scope) |
| Check, Processors | POST check, GET processors |
superusers (billing:read) |
A signed-out checkout returns claim; keep it in the browser. When the purchase completes, the provider finds the
owner by the checkout email; when none has it, it raises OnOwnerNeeded, where the app may make the account (inside
the purchase's transaction). Then it raises OnGuestPurchase, where the app sends its own claim email.
React to purchases in Go:
billingv1.OnItemChanged(app).BindFunc(func(e *billingv1.ItemEvent) error {
if e.EntitlementsAfter["instances"] != e.EntitlementsBefore["instances"] {
// apply the new allowance
}
return e.Next()
})
or in pb_hooks, with the action hooks every wire contract has (onBillingCheckout, onBillingCancel, ...).
Provide
Implement billingv1.Billing, provide it with billingv1.Contract.ProvideDefault(app, impl), and declare
Implements and Requires: extkit.IDs("payments/v1"). A provider:
- re-reads the subscription or order an event names and writes what it finds, so the order events arrive in, and repeats, never matter; first-time work (a new purchase's handlers) is keyed by the order or checkout, not the event id;
- lets an owner act only on their own items, and a superuser on anyone's;
- raises
OnItemChangedafter each item is saved, with the owner's entitlements before and after, andOnGuestPurchaseonce a signed-out purchase's owner is known; when no owner has a signed-out purchase's email, it raisesOnOwnerNeededin the purchase's transaction and takes the owner the handler sets, never making one itself.
Extensions
| Extension | |
|---|---|
| billing | provides (default) |
License
MIT
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.