payments/v1
A payment processor behind one interface: start a hosted checkout, link to the customer portal, read, cancel, resume and change a subscription, read an order, ask for a refund, and turn the processor's webhooks into normalized events. A provider talks to its processor's API and translates; it keeps no state and knows no users. billing/v1 keeps the state and is what apps build against.
A service (payments.go), keyed by processor name (stripe, paddle, creem). Every installed
processor provides it, so items bought through one keep renewing while new checkouts go to another (billing's
settings pick which).
Use
Apps call billing/v1, not this. An extension that needs one processor directly:
p, ok := paymentsv1.Contract.Get(app, "stripe")
sub, err := p.Subscription(ctx, paymentsv1.Ref{Kind: paymentsv1.KindSubscription, ID: "sub_123"})
What every provider promises:
- Statuses are normalized. Subscriptions:
trialing,active,past_due(retrying a failed payment),unpaid(gave up),paused,cancelling(active untilendsAt),ended. Orders:paid,partially_refunded,refunded,disputed. - Events name what they're about; callers re-read it. Processors don't promise order, and some give a resent event a new id, so billing never applies an event's payload: it reads the subscription or order again.
- Checkout is a URL or a page.
Checkoutreturnsurl, orpage(HTML) when the processor's checkout has to run on a page of the app's (Paddle.js); billing serves it at/api/_c/billing/v1/pay. - "Resume" undoes a scheduled cancel. Pausing isn't an operation;
pausedis a status you read. - Changes say how to prorate:
now,next_periodornone. - Refunds are requests.
pendinguntil the processor approves; the item changes when its event arrives. - What a processor can't do is
ErrUnsupported(Creem refunds only in full); a processor without keys isErrNotConfigured. - Money is minor units plus an ISO currency, as the processor reports it. Tax, invoices and receipts stay with the processor.
Provide
Implement paymentsv1.Processor (its Name() is the key) and provide it in Register:
paymentsv1.Contract.Provide(app, &stripe{app: app})
Declare Implements: extkit.IDs("payments/v1"). Keep the processor's keys and mode in a
settings/v1 section with Group: "billing" and Fields, so it shows on the Billing
page with no JS of your own. Test ParseWebhook against webhooks recorded from the processor's sandbox.
Extensions
| Extension | |
|---|---|
| creem | provides |
| paddle | provides |
| stripe | provides |
| billing | works with |
License
MIT
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.