Selling: checkout
A checkout sells one product to one buyer. Your app asks Billing for a checkout, sends the browser to the URL it returns, and the buyer pays on the processor's page. The processor's webhook tells Billing, Billing reads the purchase back, and the buyer's item and entitlements are there by the time they land on your success page, or moments after.
From page code
$billing acts as the auth record you give it, with that user's rights. In Pages,
that's ctx.auth:
// pb_pages/checkout/[product].ts: POST /checkout/pro starts a checkout for "pro"
module.exports = {
async post(ctx) {
if (!ctx.auth) return ctx.res.redirect('/login')
const { url } = await $billing.checkout.async(ctx.auth, {
product: ctx.params.product,
quantity: 1,
successUrl: 'https://example.com/welcome',
})
return ctx.res.redirect(url)
},
}
<form method="post" action="/checkout/pro"><button>Upgrade to Pro</button></form>
checkout and portal call the processor, so page code awaits their .async forms. entitlements and items read
your own database and are fine to call directly.
From the browser
Billing's API is the billing/v1 contract at /api/_c/billing/v1/. With the PocketBase
JS SDK, signed in:
const { url } = await pb.send('/api/_c/billing/v1/checkout', {
method: 'POST',
body: { product: 'pro', quantity: 2 },
})
location.href = url
From Go
res, err := billingv1.Contract.Must(app).Checkout(ctx, auth, billingv1.CheckoutInput{Product: "pro", Quantity: 2})
// res.URL is where to send the browser
billingv1 is pocketone.dev/ext/contracts/billing/v1; auth is the buyer's extkit.Auth.
Go code can also set Server, which a checkout over the wire or from $billing never carries, so only the app decides
it. Use it to move a customer from another processor without charging them twice:
in := billingv1.CheckoutInput{Product: "pro", Quantity: 2, Server: billingv1.CheckoutServer{
TrialDays: 12, // first charge in 12 days, when the old plan would renew
Metadata: map[string]string{"replaces": "sub_old"}, // on the purchase at the processor (Stripe: the subscription)
AllowOffSalePurchase: true, // the product may be bought while off sale (a retired plan they keep)
}}
Billing's own metadata keys (checkout, owner, owner_collection) win over the app's. The new item's data holds
the processor's object, metadata included, for the app's OnItemChanged handler.
What a checkout takes
| Field | What it does |
|---|---|
product |
The product's key. |
quantity |
How many units; default 1. |
email |
Signed out: fills in the processor's form. |
discount |
A discount or promotion code the processor knows. |
successUrl, cancelUrl |
Where the buyer goes after paying or giving up. Default: Billing's settings. |
The result has url (send the browser there) and processor (which one took it).
Pay first
With Signed-out checkout (pay first) on in Billing → Settings, a visitor can buy before signing up. Pass null as
the auth record:
// pb_pages/buy.ts
module.exports = {
async post(ctx) {
const { url } = await $billing.checkout.async(ctx.auth, { product: 'lifetime', email: ctx.req.body().email })
return ctx.res.redirect(url)
},
}
When the payment comes in, Billing gives the purchase to the user whose email matches the checkout email (ignoring
case). A buyer with no account gets one only if your app makes it: what a new account needs is up to the app, so
Billing asks through the OnOwnerNeeded hook. The handler runs inside the purchase's transaction; save the record with
e.App and set e.Owner. Without a handler, or with no Owner set, the purchase waits until a user with that email
exists.
billingv1.OnOwnerNeeded(app).BindFunc(func(e *billingv1.OwnerEvent) error {
user := core.NewRecord(e.Collection)
user.SetEmail(e.Email)
user.SetRandomPassword()
if err := e.App.Save(user); err != nil {
return err // the processor sends the webhook again
}
e.Owner = user.Id
return e.Next()
})
Once the purchase is saved, welcome the buyer, for example with a sign-in link, from the OnGuestPurchase hook
(e.Created is true when your handler made the account):
billingv1.OnGuestPurchase(app).BindFunc(func(e *billingv1.GuestEvent) error {
if e.Created {
user, err := e.App.FindAuthRecordByEmail("users", e.Email)
if err != nil {
return err
}
if err := mails.SendRecordPasswordReset(e.App, user); err != nil {
return err
}
}
return e.Next()
})
With pay first off, a signed-out checkout is refused.
After the checkout
The buyer lands on your success URL. The webhook usually arrives first; if it hasn't yet, show "Payment received, setting up your account" and read entitlements again in a moment. Nothing your app does is needed for the purchase to be recorded.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.