Repository navigation
fix(backend): heal a lapsed stored paid plan from Stripe on subscription read - #21890
Merged
aryanorastar merged 1 commit intoOct 16, 2026
Conversation
…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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/subscriptionalready heals from Stripe on read: it callsreconcile_basic_plan_with_stripe, thenget_user_valid_subscription. But the reconcile returned early for any record not stored asbasic.get_user_valid_subscriptiontreats a stored paid plan whosecurrent_period_endhas 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 tofind_active_paid_subscription_for_userand writes the active Stripe subscription back.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
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 oldsubscription.py, the heal and leave-alone tests fail.BACKEND_UNIT_TEST_FILE_LIST=… bash test.shwithtests/routers/test_users.py,test_byok_security_enrollment.py,test_fsr_s4_caches.pyand 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