fix(web): keep the discount code until the upgrade actually completes - #1568
Open
rajarshidattapy wants to merge 1 commit into
Open
fix(web): keep the discount code until the upgrade actually completes#1568rajarshidattapy wants to merge 1 commit into
rajarshidattapy wants to merge 1 commit into
Conversation
Every checkout call site cleared the stored promo code as soon as `autumn.attach()` resolved, but attach resolving only means Stripe handed back a payment URL. A user who closed or abandoned that checkout page came back with the code already gone from localStorage and the `?discountCode=` param long since stripped, with no way to get it back. Clear centrally instead: `PromoCodeHost` spends the code once the org moves up a plan, which is the point at which the checkout it was captured for actually went through. Downgrades and trial expiries leave an unused code alone. Codes also carry a 30-day expiry now, so one that is never redeemed stops applying to future checkouts instead of discounting them forever.
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.
Relates to #1546 — see the note at the bottom, the reported bug is the opposite of the real one.
Problem
All eight checkout entry points cleared the stored promo code the instant
autumn.attach()resolved:attach()resolving only means Stripe handed back a payment URL — the user has not paid yet. Close that tab, hit back, or let the card decline, and the code is already gone fromlocalStorage;?discountCode=was stripped from the URL on capture, so there is no way to recover it short of finding the original link again.Fix
Clear centrally, on the signal that actually means "this checkout went through": the org moving up a plan.
PromoCodeHostalready mounts on every app page and now reads the plan fromuseTokenUsage, so the eightpromoCode.clear()calls are gone andusePromoCode()is down to{ getDiscounts }.PLAN_RANK[current] > PLAN_RANK[stored]spends them.autumn.isLoading— recording a placeholderfreewould spend the code on the next render.Codes now also carry a 30-day expiry. Without the eager clear, a code that never gets redeemed would otherwise sit in
localStoragediscounting every future checkout indefinitely, which is the leak #1546 was worried about. The expiry bounds it.Storage moves from a bare string to JSON. Bare values written by #1523 still work and get stamped with a plan and expiry on the next mount.
Tests
apps/web/hooks/use-promo-code.test.ts(bun:test, 8 cases) covers the two pure pieces:parseStoredPromoCode(JSON, expired, legacy bare code, malformed) andisPromoCodeSpent(upgrade spends, unfinished checkout does not, downgrade/trial-expiry does not, unstamped code never does).tsc --noEmitonapps/webreports the same 84 pre-existing errors asmain;biome ci --changedis clean.On issue #1546
The issue reports that
usePromoCode().clearhas zero call sites, so a code applies to every checkout forever. That is not the case onmain— all eight sites callpromoCode.clear(), and have since #1523 introduced the feature; the issue'sgreponly captured thediscounts:lines. The real defect in that code is the inverse, which is what this PR fixes. The permanent-toast half of the report follows from the same mistaken premise: the toast is dismissed on clear.