Paddle

Version 0.0.1 · Owners: @benallfree

Paddle is a payment processor for Billing. Paddle sells as merchant of record: buyers pay Paddle, Paddle collects and files sales tax and VAT, and it handles refunds' approval and disputes. Paddle's webhooks keep Billing's items current, and owners manage cards and invoices in Paddle's customer portal.

Install · Configure · Use · Integrations · Example

Install

extkit.Setup(app, []extkit.Extension{settings.New(), billing.New(billing.Config{}), paddle.New(paddle.Config{})}, nil, nil)

The module is pocketone.dev/ext/paddle. It talks to Paddle Billing's API over plain HTTP, with no Paddle SDK.

Configure

Open Billing → Settings in the dashboard. Paddle's section is below Billing's own.

Setting What it does
API key A server-side key from Developer tools → Authentication. A sandbox key (pdl_sdbx_apikey_) uses Paddle's sandbox, which is a separate Paddle account with its own keys.
Client-side token From the same page (live_ or test_). The checkout page loads Paddle.js with it. It isn't secret.
Webhook secret key The secret key (pdl_ntfset_) of the notification destination below.

Test checks the API key with one read call.

In Paddle, add a notification destination (Developer tools → Notifications) at the webhook URL shown on the settings page (<your app>/api/_c/billing/v1/webhook?provider=paddle), with these events:

  • transaction.completed, transaction.payment_failed
  • subscription.created, subscription.updated, subscription.activated, subscription.canceled, subscription.paused, subscription.resumed, subscription.past_due, subscription.trialing
  • adjustment.created, adjustment.updated

Paddle sends webhooks only to a public HTTPS address. To try it on your own machine, put the app behind a tunnel (ngrok, cloudflared) and use the tunnel's address.

Live checkouts open on your app's own domain, so add that domain under Checkout → Website approval in Paddle.

Each product on Billing → Products names its Paddle price: prices: {"paddle": "pri_..."}. A subscription product needs a price with a billing cycle, a one-time product one without; Check prices tells you if one doesn't match.

Use

Billing is the API; this extension only answers it. What Billing's calls do at Paddle:

Billing Paddle
checkout A transaction for the product's price and quantity, with Billing's checkout in custom_data. Billing serves a page at /api/_c/billing/v1/pay that opens Paddle's checkout over it with Paddle.js; closing the checkout goes back to the Cancel URL.
portal A customer portal session for the owner's Paddle customer.
cancel Cancels at the next billing period, or now.
resume Removes a scheduled cancel.
quantity, plan Updates the subscription's item. Proration now charges the prorated difference at once, next_period adds it to the next bill, none bills the new amount from the next period.
refund A refund adjustment on a one-time purchase's transaction, or on a subscription's latest completed one. Paddle approves refunds itself, so most stay pending until it does; the item changes when Paddle's adjustment.updated arrives.

A one-time purchase is its transaction (txn_...). Paddle doesn't include the customer's email on a subscription, so reading one makes a second call for the customer.

Integrations

Extension How they fit
Billing Paddle provides payments/v1, the processor contract Billing uses. Several processors can be installed; Billing's settings pick the one that takes new checkouts.
Settings Paddle's settings are a section on Billing → Settings.
KV The keys are stored privately in the key-value store, never readable over its API.

Example

// Command example runs one PocketOne app that sells through Paddle: billing (ext/billing) with Paddle as its only
// processor.
//
//	go run ./example serve
//
// Then, in the dashboard ([email protected] / password): Billing → Settings, paste a sandbox API key
// (pdl_sdbx_apikey_...) and client-side token (test_...) from Paddle's sandbox, and add a product on Billing →
// Products with prices {"paddle": "pri_..."}. Paddle sends webhooks only to a public HTTPS address, so put the app
// behind a tunnel (ngrok, cloudflared) and add <tunnel>/api/_c/billing/v1/webhook?provider=paddle as a notification
// destination. As [email protected], POST /api/_c/billing/v1/checkout {"product": "<key>"}, open the URL it returns
// and pay with card 4242 4242 4242 4242.
//
// 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/paddle"
	"pocketone.dev/ext/settings"
)

func main() {
	app := pocketbase.New()
	devusers.Register(app) // local testing: superuser [email protected] and user [email protected], password "password"
	exts := []extkit.Extension{settings.New(), billing.New(billing.Config{}), paddle.New(paddle.Config{})}
	if _, err := extkit.Setup(app, exts, nil, nil); err != nil {
		log.Fatal(err)
	}
	if err := app.Start(); err != nil {
		log.Fatal(err)
	}
}

License

MIT