fix(billing): downgrade orgs when Polar subscription is revoked (#759)

* fix(billing): downgrade orgs when Polar subscription is revoked

The Polar webhook handler had subscription.revoked as a no-op, so
subscriptions that expired or failed to renew never got the org's
organization_plan row deactivated, leaving them stuck on a paid plan
indefinitely. Handle subscription.revoked the same way as an inactive
subscription.updated event.

Also add a one-time backfill script (db:downgrade-expired-subscriptions)
that re-checks every currently-active org plan against Polar directly
and downgrades any whose subscription already ended, to fix orgs
affected before this webhook fix shipped.

* Update packages/db/src/scripts/downgrade-expired-subscriptions.ts

---------
This commit is contained in:
Rotimi Best
2026-07-16 00:57:31 +01:00
committed by GitHub
parent 3980aa0a9b
commit c9b7c79365
4 changed files with 149 additions and 1 deletions
@@ -157,6 +157,16 @@ async function onPayload(payload: PolarWebhookPayload) {
case 'subscription.active':
break;
case 'subscription.revoked':
try {
const result = await OrgPlanApiServer.cancelOrgPlan({
subscriptionId,
payload: data as unknown as Record<string, unknown>
});
console.log('Subscription revoked', result);
} catch (error) {
console.error('Error revoking org plan', error);
}
break;
case 'subscription.canceled':
break;