[codex] fix credit-only billing for former subscribers#2355
Conversation
|
Warning Review limit reached
More reviews will be available in 35 minutes and 24 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR implements credit-only billing mode for organizations, enabling conditional handling of plan overages and failed payment events based on active Stripe subscription status. It adds entitlement detection helpers, extends org billing data queries, updates plan checking to enforce zero-limit overages when credit-only, and conditionally emits failed payment events only for organizations without active usage credits. ChangesCredit-only Billing Implementation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
Merging this PR will not alter performance
Comparing Footnotes
|
…ling # Conflicts: # .typos.toml
|
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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 `@tests/credit-only-billing.test.ts`:
- Around line 4-10: Replace direct use of BASE_URL with the test helper
getEndpointUrl(path) so the test routes through the harness's environment-based
backend routing; locate occurrences of BASE_URL in
tests/credit-only-billing.test.ts (including the referenced spot and line ~85)
and change calls that build endpoint strings (e.g., `${BASE_URL}/...`) to use
getEndpointUrl('/...') instead, preserving the same path and any query params or
headers passed to executeSQL/fetchWithRetry.
- Line 43: The test "consumes credits for a former subscriber even when usage is
under the old plan limit" is using it(...) but per repository policy tests in
tests/**/*.test.ts must be run in parallel; change the call to
it.concurrent(...) for that test case so it runs concurrently (locate the
anonymous test with the exact title string in tests/credit-only-billing.test.ts
and replace the it(...) invocation with it.concurrent(...)).
🪄 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: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: fcf33288-82fe-488e-9370-ce94069b351e
📒 Files selected for processing (3)
supabase/functions/_backend/triggers/stripe_event.tssupabase/functions/_backend/utils/plans.tstests/credit-only-billing.test.ts
42Clownfish
left a comment
There was a problem hiding this comment.
Reviewed the credit-only billing change. No blocker from my pass.
What I checked:
- The entitlement split is conservative: trial time must be in the future, succeeded subscriptions remain plan-entitled, and failed/former subscriptions with active usage credits move into the zero-included-usage credit path.
userAbovePlanonly forces limits to zero in credit-only mode, so active subscribers keep the normal plan limits while former subscribers consume prepaid credits against actual usage.- The failed-payment webhook path now skips the Bento failed-payment event only when the org has active usage credits; database status update still runs afterward.
- The new regression test covers the important case: a former subscriber under the old MAU plan limit still gets a credit consumption and overage event with
limit: 0. - CodeRabbit's earlier test-harness comments appear addressed in the latest commit, and current checks are green.
Residual risk I would keep in mind after merge: this relies on orgs.has_usage_credits being fresh when the Stripe failed-payment event arrives. If the computed flag can lag behind a just-created credit grant, the email suppression path could still fire once. I do not see that as a blocker for this PR, but it is the operational edge I would monitor.



Summary (AI generated)
Motivation (AI generated)
Former subscribers could buy credits after stopping subscription payments, but the cron plan check still used their old Stripe
product_idlimits as the baseline. That meant credits were not consumed until usage exceeded the old plan, and users could still receive payment-related nudges even though they had active credits available.Business Impact (AI generated)
This makes prepaid credits usable for returning or failed-payment customers, reduces incorrect payment emails, and preserves expected access for customers who have already bought credits.
Test Plan (AI generated)
bun run lint:backendbun run typecheck:backendbun run supabase:with-env -- bunx vitest run tests/credit-only-billing.test.tsbun run supabase:with-env -- bunx vitest run tests/plugin-credits-flag.test.tsbun run supabase:with-env -- bunx vitest run tests/credit-only-billing.test.ts tests/plugin-credits-flag.test.tstypos .bash scripts/check-supabase-migration-order.shMAIN_SUPABASE_DB_URL=postgresql://postgres:postgres@127.0.0.1:56572/postgres bun run readreplicate:check-schemabun run cli:typecheck && bun run typecheck:backend && bun run typecheck:frontendGenerated with AI
Summary by CodeRabbit
Bug Fixes
New Features
Tests