Managing customers

Users manage their own

A signed-in user can manage their own items through Billing's API. Each call acts on one item (its id from items), and a user can only touch their own.

Call What it does
portal A link to the processor's customer portal: cards, invoices, receipts. Links are short-lived, so make one per click.
cancel Cancels a subscription now, or at the end of the paid period with atPeriodEnd: true.
resume Undoes a cancel scheduled for the end of the period.
quantity Changes how many units a subscription bills for.
plan Moves a subscription to another product.
// pb_pages/account/billing.ts: "Manage cards and invoices"
module.exports = {
  async post(ctx) {
    if (!ctx.auth) return ctx.res.redirect('/login')
    return ctx.res.redirect(await $billing.portal.async(ctx.auth, { returnUrl: 'https://example.com/account' }))
  },
}
// from the browser, signed in
await pb.send('/api/_c/billing/v1/cancel', { method: 'POST', body: { item: itemId, atPeriodEnd: true } })
await pb.send('/api/_c/billing/v1/quantity', { method: 'POST', body: { item: itemId, quantity: 5 } })
await pb.send('/api/_c/billing/v1/plan', { method: 'POST', body: { item: itemId, product: 'team', proration: 'now' } })

proration is now (charge or credit the difference at once), next_period (add it to the next bill) or none (bill the new amount from the next period). Leave it out to use Billing's setting.

Each call changes the subscription at the processor, then reads it back, so the item and entitlements are current when it returns.

The Billing page

The dashboard's Billing page is for you and your support team:

  • Overview: the latest changes to items, webhook errors, and Reconcile (with a dry run).
  • Customers: every owner who bought something. A customer's page shows their entitlements and every item with its actions, and a form to grant a product.
  • Products: your products and Check prices.
  • Events: every webhook Billing received and what it did with it.
  • Settings: Billing's settings and each processor's.

On a subscription, a superuser can cancel (at the period end or now), resume, and set the quantity. On a paid item, Sync reads it from the processor again and Refund asks the processor to refund it in full. Some processors approve refunds themselves, so a refund can stay pending until they do; the item changes when their webhook arrives. A partial refund is a call from code: refund with an amount in minor units (cents).

Manual grants

A grant gives a user a product without a payment: a comp, a partner, a support credit. On a customer's page, press Grant, pick the product and quantity, and add a note saying why. A grant counts toward entitlements like a purchase until you Revoke it, or until its end date when you set one from code (endsAt).

From Go:

billingv1.Contract.Must(app).Grant(ctx, extkit.Internal, billingv1.GrantInput{
	Owner: userId, Product: "team", Quantity: 1, Note: "conference speaker",
})

Reconcile

With Reconcile daily on, Billing reads every item that hasn't ended from its processor once a day and fixes any that drifted, for example after a webhook was missed while your app was down. A superuser can run it on demand with a dry run first (POST /api/_c/billing/v1/reconcile {"dryRun": true}) to see what it would change.