Repository navigation
Prepaid code: portal reads Stripe only when needed, redeem-code refuses a prepaid plan (switch stays off) - #26171
Merged
Conversation
…es a prepaid plan The portal skips the Stripe subscription list for a live paid non-prepaid row and turns a Stripe error there into a logged 503. /redeem-code refuses a prepaid plan made with a once code. A refused prepaid cancel no longer stores cancellation feedback.
kodjima33
marked this pull request as ready for review
November 23, 2026 20:52
kodjima33
added a commit
that referenced
this pull request
Nov 23, 2026
…another live plan (switch stays off) (#26173) ## What changed and why Follow-up to #26171 from its review. Backend only. `PREPAID_CODE_REDEEM` stays `off` and nobody is added to the cohort. 1. **Switch off is a true no-op on the customer portal.** With the switch off, free, churned and past_due/unpaid readers (dunning users fixing a card) still took the extra Stripe subscription read, and got a 503 if Stripe failed there, where they used to get a portal session. Now: - When `PREPAID_CODE_REDEEM` is off and the stored row has no prepaid marker, the read is skipped. The new `config.prepaid_code_redeem.mode()` is an env read with no I/O. Those readers get exactly the session they got before #26146 (`customer` and `return_url` only). - In `cohort` or `on` mode the read turns itself back on for any row that isn't a live paid plan. - A stored prepaid row is refused whatever the switch says. 2. **`_resume` checks for another live plan.** If the first create never reached Stripe and the reader bought a card-billed plan in between, a retry could create a prepaid plan next to it. Portal and cancel would then return 409, so the reader couldn't cancel the card-billed plan in the app. Now: - A resumed redemption whose claim has no subscription id re-runs the live-plan check (Play row, live stored row, any Stripe subscription that can still bill). - That check ignores the subscription this same code created: one with the prepaid marker and `redeemed_promotion_code_id` equal to this promotion code, plus the stored row when it points at that subscription. - The reader's own half-finished plan completes. Any other live plan gives `409 has_plan` with no Stripe write. - The claim stays with the reader, so the code is used by nobody else and the same reader can finish once that plan has ended. ## Money-safety: changed lines - `backend/config/prepaid_code_redeem.py`: `mode()`, which `enabled_for` now uses (same behaviour). - `backend/routers/payment.py` `create_customer_portal_endpoint`: the read also requires `mode() != 'off'`. - `backend/utils/prepaid_code_redemption.py`: - `_has_live_plan(..., own_promotion_code_id)` and `_open_stripe_subscriptions`, which replace `_has_open_stripe_subscription`; - the resume guard in `_resume`. ## How it was verified - `tests/unit/test_prepaid_code_redemption.py` (66), covering: - switch unset, off or unknown, with a free or a churned/past_due row: zero Stripe list calls, and the portal session is created with only `customer` and `return_url`; - `cohort` or `on`, with a free row and a marked Stripe subscription: refused; - a stored prepaid row is refused in every mode; - `mode()` is an env read only; - resume with only its own half-finished subscription (in Stripe and on the stored row) completes without a second subscription; - resume next to another live card-billed plan is refused `409 has_plan`, with no Stripe write and the promotion code unredeemed; it finishes once that plan has ended. - 155 neighbouring backend files pass one by one, the `test.sh` timing guard passes, and `backend/scripts/typecheck.sh` has 0 errors. ## Not verified - Live Stripe and the production webhook grant (sandbox and stub only). - The live Stripe customer portal configuration. ## Product invariants affected none ## Line-count exceptions Line-Count-Exception: backend/routers/payment.py | 2445 -> 2447 | the switch-off skip belongs in the portal endpoint it guards <!-- This is an auto-generated description by cubic. --> <a href="https://cubic.dev/pr/BasedHardware/omi/pull/26173?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a> <a href="https://www.cubic.dev/action/auto-fix/pr/BasedHardware/omi/26173?returnTo=https%3A%2F%2Fgithub.com%2FBasedHardware%2Fomi%2Fpull%2F26173&source=description" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a> <!-- End of auto-generated description by cubic. -->
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.
What changed and why
Follow-up to #26166 from its review ("Ranked fixes before on for all"). Backend only.
PREPAID_CODE_REDEEMstaysoffand nobody is added to the cohort.POST /v1/payments/customer-portallisted the customer's Stripe subscriptions for every payer, switch on or off, and a Stripe error there was a raw 500 with no log line. Now:503 "Could not verify your subscription. Please try again.", the same shape the checkout path uses./v1/payments/redeem-coderefuses a prepaid plan. A plan made with aoncecode has no discount after its first invoice, so this path would attach a new discount and burn that code. It now refuses with 409 before any claim or Stripe write, and the code is not consumed._resumelive-plan check: not changed, known. It isn't a few-line fix. When the claim has no subscription id, the redemption may already have created its own prepaid subscription (Stripe succeeded, the server died before recording it). A plain_has_live_planre-run would see that subscription and refuse the reader's own redemption. Doing it right means excluding the subscription created for this promotion code from both the stored-row and Stripe checks. The path is only reachable after an ambiguous 503 more than 24 hours earlier.Out of scope here: hiding Manage and Cancel for a prepaid plan on desktop, Windows and web.
Money-safety: changed lines
backend/routers/payment.pycreate_customer_portal_endpoint: the live-paid-row skip, plus the Stripe error to logged 503.backend/routers/payment.pycancel_subscription_endpoint: feedback is stored after the prepaid guard.backend/utils/promotion_redemption.pyredeem_promotion_code: refuses a subscription with the prepaid marker.How it was verified
tests/unit/test_prepaid_code_redemption.py(51), covering:Subscription.listcalls;tests/unit/test_promotion_redemption.py(24): a once-shaped prepaid plan is refused with 409, with no claim, no promotion-code change and no subscription change.test.shtiming guard passes, andbackend/scripts/typecheck.shhas 0 errors.Not verified
Product invariants affected
none
Line-count exceptions
Line-Count-Exception: backend/routers/payment.py | 2432 -> 2445 | the skip and the 503 must sit in the portal endpoint they guard