fix: downgrade instead of delete key on subscription cancel
All checks were successful
Deploy to Production / Deploy to Server (push) Successful in 2m43s

- Replace revokeByCustomer with downgradeByCustomer in keys.ts
  - Sets tier='free' in cache and DB (UPDATE, not DELETE)
- Add isDocFastSubscription() product filter helper in billing.ts
  - Filters all subscription events by prod_TygeG8tQPtEAdE
- Handle customer.subscription.updated event
  - Downgrades on status=canceled/past_due/unpaid or cancel_at_period_end=true
- Handle customer.subscription.deleted with product filter
  - Downgrades to free (was incorrectly deleting the key)

Fixes revenue integrity bug: cancelled Pro subscribers kept Pro access.
This commit is contained in:
DocFast Bot 2026-02-17 10:46:12 +00:00
parent 2bfd893510
commit 855068a011
2 changed files with 56 additions and 10 deletions

View file

@ -108,12 +108,11 @@ export async function createProKey(email: string, stripeCustomerId: string): Pro
return entry;
}
export async function revokeByCustomer(stripeCustomerId: string): Promise<boolean> {
const idx = keysCache.findIndex((k) => k.stripeCustomerId === stripeCustomerId);
if (idx >= 0) {
const key = keysCache[idx].key;
keysCache.splice(idx, 1);
await pool.query("DELETE FROM api_keys WHERE key = $1", [key]);
export async function downgradeByCustomer(stripeCustomerId: string): Promise<boolean> {
const entry = keysCache.find((k) => k.stripeCustomerId === stripeCustomerId);
if (entry) {
entry.tier = "free";
await pool.query("UPDATE api_keys SET tier = 'free' WHERE stripe_customer_id = $1", [stripeCustomerId]);
return true;
}
return false;