Skip to content

fix(backend): heal a lapsed stored paid plan from Stripe on subscription read - #21890

Merged
aryanorastar merged 1 commit into
BasedHardware:mainfrom
aryanorastar:fix/backend-heal-lapsed-paid-subscription
Oct 16, 2026
Merged

aryanorastar merged 1 commit into
BasedHardware:mainfrom
aryanorastar:fix/backend-heal-lapsed-paid-subscription

Conversation

@aryanorastar

@aryanorastar aryanorastar commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Refs #21869. A Neo (legacy Unlimited) subscriber with paid invoices through Sept 17 was shown Free Plan, 409 of 300 min in October.

GET /v1/users/me/subscription already heals from Stripe on read: it calls reconcile_basic_plan_with_stripe, then get_user_valid_subscription. But the reconcile returned early for any record not stored as basic. get_user_valid_subscription treats a stored paid plan whose current_period_end has passed as Basic. So if a renewal webhook never landed, the stored period stayed stale, the user read as Free everywhere, and the heal never ran for them.

The fix treats a stored paid plan with a lapsed or missing period end like basic. The reconcile then falls through to find_active_paid_subscription_for_user and writes the active Stripe subscription back.

  • A paid plan with a current period returns before any Stripe call, as before.
  • A lapsed paid plan with nothing active in Stripe is left as it was, with no write.
  • The extra Stripe lookups are bounded to lapsed paid records, the same lookup Basic records with a Stripe customer already do on this route.

This does not prove what happened to that specific account; #21869 still asks for the read-only reconciliation run. It removes the code path that would keep such an account on Free.

Testing

  • New tests/unit/test_subscription_lapsed_paid_heal.py (3 tests) covers three cases: a lapsed paid plan heals and writes; a current paid plan never calls Stripe; a lapsed plan with nothing active in Stripe is left alone. Against the old subscription.py, the heal and leave-alone tests fail.
  • BACKEND_UNIT_TEST_FILE_LIST=… bash test.sh with tests/routers/test_users.py, test_byok_security_enrollment.py, test_fsr_s4_caches.py and the new file: all pass (67 + 93 + 43 + 3).

Product invariants affected

none (subscription read reconciliation only; no entitlement is granted without an active paid Stripe subscription)

Failure class (fixes)

Failure-Class: FC-billing-webhook-acknowledges-failed-entitlement

UI-Evidence: none -- backend only

Line-Count-Exception: backend/utils/subscription.py | 1952 -> 1962 | The lapsed-period predicate belongs beside reconcile_basic_plan_with_stripe, the read-time Stripe heal it widens.

🤖 Generated with Claude Code

View guided diff

…ion read

GET /v1/users/me/subscription runs reconcile_basic_plan_with_stripe before
get_user_valid_subscription, but the reconcile returned early for anything
not stored as `basic`. A paid plan whose stored current_period_end had
passed (for example a renewal webhook that never landed) read as Basic
everywhere and was never repaired, so a subscriber who paid on Sept 17 was
metered as Free in October.

Treat a stored paid plan with a lapsed or missing period end like Basic:
look up the active paid subscription in Stripe and write it back. A
current paid period still never calls Stripe, and a lapsed plan with
nothing active in Stripe is left as it was.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aryanorastar
aryanorastar merged commit 48d73bc into BasedHardware:main Oct 16, 2026
52 checks passed
aryanorastar added a commit that referenced this pull request Oct 19, 2026
…m Stripe subscription ID (#21869) (#22193)

Refs #21869

Failure-Class: FC-billing-webhook-acknowledges-failed-entitlement
Line-Count-Exception: backend/utils/subscription.py | 1978 -> 1991 |
Allow lapsed stored paid subscriptions to heal directly from Stripe
subscription ID
Bounty: 
Claim: https://paypal.me/Dragonire9
Email: [email protected]

### Problem Analysis
In #21869, paying Neo (legacy Unlimited) subscribers who renewed in
Stripe were metered at 300 minutes and read as Free Plan.
While #21890 added lapsed-plan detection to 
econcile_basic_plan_with_stripe, line 1942 still checked if
period_end_dt >= datetime.now(timezone.utc): before calling
stripe.Subscription.retrieve(subscription.stripe_subscription_id).
Because the stored period was lapsed (in the past), direct retrieval by
subscription ID was bypassed entirely and fell back to
ind_active_paid_subscription_for_user(uid). For legacy subscribers whose
user root document lacks a populated stripe_customer_id, customer
listing returned None and left the account in lapsed status.

### Solution
- **�ackend/utils/subscription.py**:
  - Remove the precondition checking stored period end against 
ow, allowing direct stripe.Subscription.retrieve on
subscription.stripe_subscription_id.
- Validate that Stripe\'s current_period_end is active and in the
future, updating Firestore with the renewed period end, active status,
plan type, and persisting stripe_customer_id onto the user record.
  - If 
etrieve returns a canceled subscription or encounters errors, gracefully
falls through to the existing customer listing fallback.
- **Scope Note**:
- Scoped strictly to the subscription-read and metering heal path
(/v1/users/me/subscription and internal entitlements).
Presentation-layer storefront reconciliation (GET /v2/payments/plans) is
left untouched to prevent unprovisioned read paths from triggering
third-party Stripe API calls.
- **�ackend/tests/unit/test_subscription_lapsed_paid_heal.py**:
- Added unit test
est_lapsed_paid_heals_from_stripe_subscription_id_directly verifying
direct healing when ind_active_paid_subscription_for_user is None and
customer ID persistence.
- Added unit test
est_lapsed_paid_stripe_retrieve_fails_falls_back_to_customer_search
verifying fallback to customer listing on Stripe errors.
- Added unit test
est_lapsed_paid_canceled_in_stripe_falls_back_to_customer_search
verifying canceled Stripe subscriptions are not granted entitlements and
fall through to customer listing without writing to Firestore.

### Verification
- Ran pytest backend/tests/unit/test_subscription_lapsed_paid_heal.py: 6
passed in 3.73s.
- Formatted with �lack --line-length 120 --skip-string-normalization.
- Verified 0 \r carriage returns across all touched files.
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