Skip to content

Prepaid code: portal reads Stripe only when needed, redeem-code refuses a prepaid plan (switch stays off) - #26171

Merged
kodjima33 merged 1 commit into
mainfrom
feat/prepaid-code-portal-and-promo-guards
Nov 23, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
feat/prepaid-code-portal-and-promo-guards

Conversation

@kodjima33

@kodjima33 kodjima33 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

What changed and why

Follow-up to #26166 from its review ("Ranked fixes before on for all"). Backend only. PREPAID_CODE_REDEEM stays off and nobody is added to the cohort.

  1. Portal reads Stripe only when needed. POST /v1/payments/customer-portal listed 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:
    • When the stored row is a live paid plan without the prepaid marker (active, paid plan, period end not passed), the list call is skipped. Redeem refuses anyone with a live plan, so such a row can't sit next to a prepaid plan. This is the normal payer, who now makes zero extra Stripe calls.
    • For a free, ended or unmarked row, the list still runs. That is where the before-webhook and lost-webhook window lives. It is not gated on the switch.
    • A Stripe error there is logged and answered with 503 "Could not verify your subscription. Please try again.", the same shape the checkout path uses.
  2. /v1/payments/redeem-code refuses a prepaid plan. A plan made with a once code 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.
  3. Small items from the review:
    • Cancel feedback order: fixed. A refused prepaid cancel no longer stores cancellation feedback; feedback is now stored after the prepaid check.
    • _resume live-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_plan re-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.py create_customer_portal_endpoint: the live-paid-row skip, plus the Stripe error to logged 503.
  • backend/routers/payment.py cancel_subscription_endpoint: feedback is stored after the prepaid guard.
  • backend/utils/promotion_redemption.py redeem_promotion_code: refuses a subscription with the prepaid marker.

How it was verified

  • tests/unit/test_prepaid_code_redemption.py (51), covering:
    • a normal monthly payer opens the portal with zero Subscription.list calls;
    • a free row with a marked Stripe subscription is refused, while an ended prepaid plan or an ordinary subscription opens the portal;
    • a Stripe list error gives 503 with a log line and the portal not opened;
    • a refused prepaid cancel with a reason stores no feedback.
  • 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.
  • 121 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.
  • Real portal volume and latency (not read from analytics).

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

View guided diff Turn on auto-fix

…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 kodjima33 added the mux-proposal Mobile UX improver (MUX) proposal; waits for Nik's ship label Nov 23, 2026
@kodjima33
kodjima33 marked this pull request as ready for review November 23, 2026 20:52
@kodjima33
kodjima33 merged commit 1457d9d into main Nov 23, 2026
75 of 76 checks passed
@kodjima33
kodjima33 deleted the feat/prepaid-code-portal-and-promo-guards branch November 23, 2026 21:12
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. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mux-proposal Mobile UX improver (MUX) proposal; waits for Nik's ship

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant