Billing 0.2.0: Webhook signatures, recorded refunds and safer renewals
Billing 0.2.0 makes payment webhooks verifiable and records the refunds they report, runs each subscription renewal as its own job, and refreshes the invoice page live when money comes in.
Webhooks that can be verified
A provider signs the exact bytes of its webhook, but Phoenix reads and parses the body before the billing routes see it. A new body reader, PhoenixKitBilling.Plugs.CacheBodyReader, keeps the raw body for /webhooks/billing/* requests only, so the signature can be checked. If the body is missing, the webhook controller now logs a clear error instead of failing silently.
Refunds reported by a provider are now recorded on the invoice; before, they were only logged. A refund larger than what was paid is handled. A webhook that is delivered again is told apart from a new event and from one already processed.
Renewals and dunning
The daily renewal run now starts one job per subscription instead of handling them all at once, so one broken subscription cannot stop the rest, and the same subscription cannot be renewed twice at the same time. Failed-payment retries (dunning) had an off-by-one: a subscription is now cancelled when the next attempt would pass the limit, and a retry is only scheduled while one is still allowed.
Admin pages
The invoice page refreshes by itself when a payment or refund lands. Invoice line items are now checked for a name, a positive quantity and a total. Several admin pages load their data once instead of twice.
Upgrading
Add the body reader to Plug.Parsers in your app’s Endpoint: body_reader: {PhoenixKitBilling.Plugs.CacheBodyReader, :read_body, []}. The install task prints the snippet. Without it, webhook signatures cannot be verified and webhook requests are rejected. No migration.
Full release notes: Billing 0.2.0
0.2.0 · 12 May 2026
Added
PhoenixKitBilling.Plugs.CacheBodyReader— body reader forPlug.Parsersthat stashes the raw request body onconn.assigns.raw_bodyfor/webhooks/billing/*paths so provider signatures can be verified. Required for webhook signature verification to function (see install instructions).- Live updates on the invoice detail page: subscribes to invoice + transaction PubSub events and refreshes when payments or refunds land.
- Line-item validation on the
Invoicechangeset (name, positive quantity, total). - Per-subscription
uniquekey onSubscriptionRenewalWorkerso concurrent enqueues for the same subscription collapse correctly.
Changed
- Webhook processor: refunds are now actually recorded via
Billing.record_refund/3(previously just logged), with handling for:exceeds_paid_amount. Idempotency now uses an upsert that distinguishes new / retry / already-processed events, and stack traces are formatted withException.format/3. - Subscription renewal worker: batch runs now fan out into one job per subscription instead of processing inline, so a single bad subscription cannot poison the whole daily run and the per-subscription unique key + row lock apply correctly.
- Subscription dunning worker: fixed off-by-one in max-attempts comparison — a subscription is now cancelled when the next attempt would exceed the cap, and the next retry is only scheduled when a future attempt is still allowed.
- LiveView mount/handle_params split: moved DB reads out of
mount/3and intohandle_params/3forBillingProfileForm,OrderForm,Subscriptions, andInvoiceDetail(mount runs twice — once for the dead render and once on connect — so queries belong inhandle_params). - Webhook controller: logs an explicit, actionable error when
raw_bodyis missing fromconn.assignsinstead of silently failing.
Required action for host applications
Wire the new body reader into your Endpoint’s Plug.Parsers (see PhoenixKitBilling.Plugs.CacheBodyReader moduledoc and the updated install task output for the exact snippet). Without it, webhook signature verification cannot run and webhook requests will be rejected.
Billing 0.2.0. Install or update: {:phoenix_kit_billing, "~> 0.2"}, then mix deps.get && mix phoenix_kit.update. Full history: CHANGELOG.