fix: paying customers see "Paid" and can't be double-charged#2397
Conversation
A company that upgrades during its 14-day trial kept being labelled Pro Trial / trialing because current_plan_label checked trial_active? before plan_tier, and apply_stripe_subscription! never cleared the trial timestamps. A paying customer was told they were still on a free trial. Prefer the paid plan and Stripe status over an active trial, and clear trial_started_at/trial_ends_at when a paid subscription is applied. Guard the checkout action against an already-paid workspace: a second checkout attached a second active Stripe subscription to the same customer and double-billed. Return 422 already_subscribed and point the customer at the billing portal instead.
Adversarial review: nulling trial_started_at when applying a paid subscription erased the only record of a used trial. trial_available? keys on trial_started_at.blank?, so a subscribe then cancel cycle would make the workspace eligible for a fresh 14-day trial every time. Null only trial_ends_at (stops a leftover in-flight trial from re-granting access after cancel) and keep trial_started_at (one trial per company). Add a regression spec for the subscribe-cancel cycle.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughChangesSubscription state and checkout
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@app/controllers/api/v1/subscriptions_controller.rb`:
- Around line 13-16: Make the checkout creation flow in SubscriptionsController
idempotent before creating a Stripe Checkout Session: under a current_company
lock, persist and reuse an in-progress session, or apply a company-scoped Stripe
idempotency key, while preserving the existing paid-plan guard. Add a regression
spec that issues concurrent free-company requests and verifies only one checkout
session is created and reused.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 77608eb3-eef3-4741-923c-efbe9dfe7e6a
📒 Files selected for processing (5)
app/controllers/api/v1/subscriptions_controller.rbapp/models/company.rbconfig/locales/en.ymlspec/models/company_spec.rbspec/requests/api/v1/subscriptions/checkout_spec.rb
| if current_company.plan_tier == "paid" | ||
| render json: { errors: I18n.t("subscriptions.already_subscribed") }, status: 422 | ||
| return | ||
| end |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Make checkout creation idempotent, not only post-subscription guarded.
Two concurrent requests from a free workspace both pass this check and can each create a Stripe Checkout Session before plan_tier becomes paid. Persist/reuse an in-progress checkout session under a company lock or use a scoped Stripe idempotency key, then add a concurrent-request regression spec.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@app/controllers/api/v1/subscriptions_controller.rb` around lines 13 - 16,
Make the checkout creation flow in SubscriptionsController idempotent before
creating a Stripe Checkout Session: under a current_company lock, persist and
reuse an in-progress session, or apply a company-scoped Stripe idempotency key,
while preserving the existing paid-plan guard. Add a regression spec that issues
concurrent free-company requests and verifies only one checkout session is
created and reused.
Part of the conversion-funnel audit (see
plans/018-023). First in the priority order — protects your earliest paying customers.Problems fixed
1. A customer who upgrades during their trial was still shown "Pro Trial".
Company#current_plan_labelreturned"pro_trial"before checking"paid",current_subscription_statusreturned"trialing"before the real Stripe status, andapply_stripe_subscription!never cleared the trial's end date. A paying customer was told they were still on a free trial (billing page + subscription API), untiltrial_ends_atpassed.Fix: prefer the paid plan and the real Stripe status over an active trial, and null
trial_ends_atwhen a paid subscription is applied so trial predicates stop firing for a paying company.2. No already-paid guard on
checkout→ double charge.A paid workspace that reached
POST /subscription/checkoutagain (the billing UI still showed the Upgrade button during the post-checkout webhook race — fixed separately in plan 020 — and the raw API / agent path is always reachable) created a second active Stripe subscription on the same customer and was billed twice.Fix: early-return
422 already_subscribedwhenplan_tier == "paid"and point the customer at the billing portal.Adversarial-review catch (fixed in this PR)
The first cut nulled
trial_started_attoo. Becausetrial_available?keys ontrial_started_at.blank?, that would have let a subscribe → cancel cycle grant a fresh 14-day trial every time (unbounded free-tier abuse). Corrected to null onlytrial_ends_atand keeptrial_started_at(one trial per company), with a regression spec for the subscribe→cancel cycle. Re-review confirmed the asymmetric (present-start / nil-end) state is nil-guarded everywhere andTrialEmailsJobfilters it out before any dereference.Verification
spec/models/company_spec.rb,spec/requests/api/v1/subscriptions,spec/jobs/trial_emails_job_spec.rb,spec/services/subscriptions,spec/mailers/subscription_mailer_spec.rb, including new examples for the paid-over-trial precedence, the downgrade-preserves-timestamps case, the subscribe→cancel re-trial guard, and the already-paid checkout 422 (assertsStripe::Checkout::Session.createis not called).current_plan_label == "paid"/ statusactive(waspro_trial/trialing); a genuine trial still readspro_trial/trialing.Follow-ups (planned, not in this PR)
plans/README.mdhas the full funnel audit: 020 (post-checkout webhook-race polling), 019 (Stripe payment-link fulfillment), 021 (in-app trial countdown banner), 023 (trial-expired email), 022 (tease Reports). Plus three product decisions flagged for you: Analytics ungated while Reports is paywalled, per-seat billing never reconciled to Stripe, and the self-hosted trial dead-ending at day 14.Summary by CodeRabbit
New Features
Bug Fixes
Tests