Skip to content

feat(billing): Paddle webhooks + applyBillingEvent (L3) - #7613

Merged
mergify[bot] merged 2 commits into
devfrom
feat/paddle-l3-webhooks
Nov 25, 2026
Merged

mergify[bot] merged 2 commits into
devfrom
feat/paddle-l3-webhooks

Conversation

@bobikenobi12

Copy link
Copy Markdown
Collaborator

Lane L3 of the Paddle Billing plan (after L1 #7499). No migration.

What

  • app/api/webhooks/paddle/route.ts — verify → normalize → apply exactly once via the shared (provider, event_id) ledger. 503 without (or with an unloadable) Paddle config; 400 on a bad signature; 200 on an applied, ignored or deliberately refused event; 500 on a parse failure, a store failure, or an adjustment whose transaction is not mirrored yet (Paddle redelivers); 413 above 512 KB.
  • lib/billing/paddle/signature.ts — Paddle-Signature (ts + one or more h1), HMAC-SHA256 over ts:rawBody, constant-time, ±5 min window.
  • lib/billing/paddle/events.ts — zod schemas (loose objects, no as) and normalization into the seam's event types. Plan or AI tier comes from configured price ids, never from custom_data. custom_data that does not parse is dropped.
  • lib/billing/paddle/provider.ts — the Paddle adapter, registered in select.ts. parseWebhook, mode and isConfigured work; the purchase/plan methods (L4) and the usage/document methods (L5) throw PaddleNotImplementedError.
  • lib/billing/events/apply.ts + store.ts — provider-neutral applyBillingEvent (pure decision core plus a store port) and its Postgres store.
  • lib/billing/plan.ts entitlementStatus — the 7-day past_due grace for Paddle rows only, wired into resolveOrgEntitlements, the usage guard, the usage and members procedures, and the workspace plan label. Stripe rows behave exactly as before.

Event → state

Paddle event Effect
subscription.created/updated/activated/trialing/resumed (active/trialing) slot set, plan/tier granted, period, seats, cancel_effective_at from a scheduled cancel (row stays active to that date)
subscription.past_due status=past_due, plan kept → granted for 7 days from period start, then not; AI tier → ai_free (no grace)
subscription.paused status=none, plan → community
subscription.canceled status=canceled, plan → community, never revived by a stale event
transaction.completed mirror upsert; credit pack → ai_credit_grant once per txn (pack size must be one we sell)
transaction.paid/past_due/payment_failed mirror upsert (payment_failed also logs)
adjustment.* approved refund (full) mirror refunded_at; pack credits zeroed
refund (partial) operator alert, nothing reversed
chargeback mirror chargeback_at, pack credits zeroed, operator alert
chargeback_reverse / reversed cleared / operator alert, nothing restored automatically
pending/rejected/credit, other types acknowledged, no change

Guards: an occurred_at watermark per slot and per mirrored transaction (same-instant tie → later lifecycle stage); one subscription per slot (a paid sub supersedes only past_due or a sub-less grant, with an alert); a row with a live Stripe subscription is refused and alerted (dead Stripe ids are cleared); the org is the row already holding the subscription, otherwise the custom_data org if it exists; a mismatch is refused and alerted; a business id held by another org is not copied.

Tests

Unit: signature, normalizer, applyBillingEvent (in-memory store: out-of-order, duplicate, unknown org, mismatch, adjustment reversal, …), route, adapter, grace. Integration: the Postgres store against the real schema and constraints.

Left for later lanes

L4: branded lifecycle emails (welcome/dunning/cancel) for Paddle; writing paddle_business_id/address at checkout; billing UI for past_due grace and cancel_effective_at. L5: usage charges and documents.

/api/webhooks/paddle verifies Paddle-Signature (HMAC-SHA256 over ts:body,
constant-time, 5-minute window, any h1 during rotation; fails closed without
config), normalizes subscription/transaction/adjustment events into the
provider-neutral seam types, and applies each exactly once through the shared
(provider, event_id) ledger.

applyBillingEvent writes organization_billing under an occurred_at watermark
(no regression from out-of-order or replayed events, no revival of a canceled
subscription, one subscription per slot, never over a live Stripe one), keeps
the paddle_transaction mirror, grants credit packs once and reverses them on a
full refund or chargeback, and alerts the operator on chargebacks, partial
refunds and org mismatches. custom_data is untrusted: the row holding the
subscription decides the org; custom_data only names one that exists.

A Paddle past_due subscription keeps its plan for a 7-day grace
(entitlementStatus), wired into the entitlement readers; Stripe rows are
unchanged. The Paddle adapter is registered in the seam with parseWebhook;
purchase/plan/usage methods throw until L4/L5.
@mergify

mergify Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Merge Protections

🟢 All 2 merge protections satisfied — ready to merge.

Show 2 satisfied protections

🟢 dev required CI

  • check-success=CI required
  • check-success=contribution-legal

🟢 🚦 Auto-queue

When all merge protections are satisfied and these conditions match, this pull request will be queued automatically.

  • #review-threads-unresolved = 0
  • -draft
  • base = dev

@mergify mergify Bot added the queued label Nov 25, 2026
@mergify

mergify Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

✅ Merged into dev as ed1af82 at 10:03 UTC

Merged together with #7529. 4 h 45 min in the queue, 1 h 59 min of it CI.

So far

6 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub repository ruleset rule protect-dev
  • check-success=CI required · required by Merge Protections rule dev required CI
  • check-success=contribution-legal · required by Merge Protections rule dev required CI
  • github-review-approved · required by GitHub repository ruleset rule protect-dev
  • any of · required by GitHub repository ruleset rule protect-dev:
    • check-success = contribution-legal
    • check-neutral = contribution-legal
    • check-skipped = contribution-legal
  • any of · required by GitHub repository ruleset rule protect-dev:
    • check-success = CI required
    • check-neutral = CI required
    • check-skipped = CI required

Times in UTC · dashboard · how the merge queue works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant