Skip to content

fix(backend): cancel active Stripe subscription on account deletion - #7679

Merged
mdmohsin7 merged 2 commits into
BasedHardware:mainfrom
ZachL111:fix/delete-account-cancel-stripe-subscription
Jun 7, 2026
Merged

mdmohsin7 merged 2 commits into
BasedHardware:mainfrom
ZachL111:fix/delete-account-cancel-stripe-subscription

Conversation

@ZachL111

@ZachL111 ZachL111 commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #6750

Summary

DELETE /v1/users/delete-account revokes Firebase auth and wipes Firestore but never cancels the user's Stripe subscription. A paying user who deletes their account keeps getting billed, and since their auth and data are gone they can't log back in to self-serve the cancellation. This cancels the active subscription (best-effort) before the wipe.

Root cause

delete_account (backend/routers/users.py) does: store feedback -> auth.delete_account(uid) (Firebase revoke) -> background Firestore wipe (_background_wipe_user_data -> delete_user_data). None of those touch Stripe, so the subscription stays active. The delete-account redesign (#6669) explicitly deferred Pinecone/GCS but didn't mention Stripe, so this reads as an unintentional gap.

Fix

Insert a best-effort cancel right after the feedback step and before the Firebase revoke, so the user doc (and subscription.stripe_subscription_id) is still readable:

try:
    sub = users_db.get_user_subscription(uid)
    if sub and sub.stripe_subscription_id:
        canceled = stripe_utils.cancel_subscription(sub.stripe_subscription_id)
        if not canceled:
            logger.error(f'delete_account stripe cancel returned None for {uid}')
except Exception as e:
    logger.error(f'delete_account stripe cancel failed for {uid}: {sanitize(str(e))}')

It uses the existing utils/stripe.py cancel_subscription helper (cancel_at_period_end=True), which is idempotent and already error-tolerant. The try/except guarantees a Stripe outage can never strand an account half-deleted; a None return or exception is logged so support can clean up manually.

Behavior change

  • Paid user deletes account: subscription is now canceled (was: continued billing).
  • Free user: no Stripe call.
  • Stripe error: logged, deletion proceeds exactly as before.

Testing

New backend/tests/unit/test_delete_account_stripe_cancel.py (registered in backend/test.sh):

  • paid user -> cancel_subscription called with the subscription id, deletion still proceeds
  • free user (stripe_subscription_id is None) -> Stripe not called
  • Stripe raises -> deletion still returns {'status': 'ok'}

Verified the paid-user test fails against current main (no cancel happens) and passes with this change.

Scope and possible follow-ups (kept out to keep this minimal)

  • Timing: uses the existing cancel_at_period_end=True helper for consistency. If the intent is "deleted account means no active paid service," an immediate-cancel variant could be added.
  • This cancels the user's own Omi subscription. Canceling app-marketplace / Connect subscriptions tied to the Stripe customer (discovered separately via the customer id) is a broader cleanup worth its own pass.
  • get_user_subscription writes a default subscription doc if none exists. It's harmless here since the doc is wiped moments later, but a non-mutating read would be slightly cleaner.

DELETE /v1/users/delete-account revoked Firebase auth and wiped Firestore
but never canceled the user's Stripe subscription, so a paying user kept
getting billed after deletion with no way to log back in and cancel (BasedHardware#6750).

Cancel the subscription (best-effort, via the existing cancel_subscription
helper) before the wipe, while the user doc is still readable. A Stripe
error is logged but never blocks deletion. Adds regression tests.

Fixes BasedHardware#6750
@greptile-apps

greptile-apps Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a billing gap where deleting an account through DELETE /v1/users/delete-account revoked Firebase auth and wiped Firestore but left the Stripe subscription active, continuing to charge the user with no way for them to log back in to cancel.

  • backend/routers/users.py: Inserts a best-effort Stripe subscription cancel (using the existing cancel_subscription helper with cancel_at_period_end=True) after storing deletion feedback but before the Firebase revoke, so the user's subscription document is still readable at cancel time. A Stripe failure is logged and never blocks the deletion.
  • backend/tests/unit/test_delete_account_stripe_cancel.py: Adds three unit tests covering the paid-user, free-user, and Stripe-error paths, using a module-level meta-path stub finder to avoid importing the full heavy dependency graph.
  • backend/test.sh: Registers the new test file as a standalone pytest invocation, matching the existing pattern.

Confidence Score: 4/5

The core fix is correct and the best-effort pattern is well-structured; the main risks are a misleading log message and the cancel_at_period_end=True choice leaving subscriptions technically active on Stripe until the period ends.

The fix addresses a real billing gap and is correctly ordered (cancel before Firebase revoke, before Firestore wipe). The outer except block log message misattributes Firestore read failures as Stripe failures, which could mislead operators during incidents. The cancel_at_period_end=True semantic means the Stripe subscription remains status=active until the billing period ends — anything reading Stripe subscription status directly will still see it as active after account deletion. The test global _StubFinder is safe for the test.sh invocation pattern but would contaminate other tests in a combined pytest run.

The cancel_at_period_end=True behavior in backend/routers/users.py deserves a second look if downstream systems (webhooks, entitlement checks) read Stripe subscription status directly rather than relying on Firebase auth being revoked.

Important Files Changed

Filename Overview
backend/routers/users.py Adds best-effort Stripe subscription cancellation before Firebase revoke in delete_account; outer except block can only be triggered by get_user_subscription errors but its log message implies a Stripe failure
backend/tests/unit/test_delete_account_stripe_cancel.py New regression test covering paid/free/error paths; installs a global meta-path stub finder at module level which could contaminate other tests if the suite is collected in a single pytest invocation
backend/test.sh Adds the new test file as its own standalone pytest invocation, consistent with the existing pattern

Reviews (1): Last reviewed commit: "fix(backend): cancel active Stripe subsc..." | Re-trigger Greptile

Comment thread backend/routers/users.py Resolved
Comment thread backend/routers/users.py Resolved
- delete_account: the except only fires on the subscription lookup (cancel_subscription
  swallows its own errors), so reword the log to 'subscription lookup failed' and add a note.
- test: install the meta-path stub finder only around the routers.users import, then remove it
  and pop the stub modules it created, so it can't affect other tests in a full-suite run.
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.

delete-account does not cancel active Stripe subscription

2 participants