Skip to content

fix(billing): scope subscription limit syncs to the exact payer - #7695

Merged
icecrasher321 merged 4 commits into
stagingfrom
codex/fix-subscription-billing-scope
Sep 9, 2026
Merged

fix(billing): scope subscription limit syncs to the exact payer#7695
icecrasher321 merged 4 commits into
stagingfrom
codex/fix-subscription-billing-scope

Conversation

@icecrasher321

@icecrasher321 icecrasher321 commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Resolve a subscription's billing pool from its exact reference. An organization owner's personal subscription must not update the organization's pool or other members' limits.
  • Keep personal limit syncs independent of organization membership and remove member fan-out from organization subscription syncs. Preserve paid personal custom caps and existing organization prepaid headroom.
  • Reconcile subscription updates through Stripe's onEvent hook so lookup or limit-sync failures fail the webhook and can be retried.
  • Remove a timing-dependent search setup test that asserted a URL change before its asynchronous flush completed.

Type of Change

  • Bug fix

Testing

  • Billing, membership, search setup, and provider management suites: 1,012 tests passed; 8 PostgreSQL tests skipped without their dedicated test database.
  • Lint, app type check, all 46 audits (including strict API validation), block registry, and docs manifest checks passed.
  • Regression coverage includes personal subscribers with owner/admin/member roles, separate personal and organization Enterprise subscriptions, and failed subscription lookups.
  • Signed webhook tests use the installed Stripe plugin to verify failed reconciliation returns an error and redelivery succeeds.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Sep 9, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
docs Ready Ready Preview Sep 9, 2026 11:54pm UTC

Request Review

@greptile-apps

greptile-apps Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR corrects billing reconciliation so subscription updates affect only the billing pool referenced by the persisted subscription.

  • Resolves organization subscriptions strictly from their organization reference instead of inferring scope from user membership.
  • Keeps personal usage limits independent from organization membership and removes organization-member fan-out.
  • Moves update reconciliation into Stripe’s retryable onEvent path and propagates lookup or synchronization failures.
  • Preserves custom personal limits and organization prepaid headroom.
  • Removes a timing-dependent Search setup test whose coverage tradeoff was explicitly accepted.

Confidence Score: 5/5

The PR appears safe to merge; no blocking billing-reconciliation failure remains.

The current implementation reloads the persisted payer reference through the retryable Stripe event hook, propagates reconciliation failures, and cleanly separates personal and organization billing pools. The earlier lookup-failure finding is fully fixed. icecrasher321 accepted the Search test’s reduced regression coverage because the removed case depended on event-loop timing, and Greptile conceded that tradeoff.

Important Files Changed

Filename Overview
apps/sim/lib/auth/auth.ts Moves subscription-update usage reconciliation from the non-retryable lifecycle callback into the Stripe event hook.
apps/sim/lib/billing/core/subscription.ts Restricts organization resolution to an exact organization reference and removes membership-based inference.
apps/sim/lib/billing/core/usage.ts Synchronizes personal usage limits from personal subscriptions without clearing them for organization members.
apps/sim/lib/billing/organization.ts Synchronizes only the subscription’s directly referenced billing pool and removes organization-member fan-out.
apps/sim/lib/billing/webhooks/subscription-usage.ts Reloads the persisted subscription during retryable Stripe update handling before reconciling its usage pool.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  Stripe[Stripe subscription update] --> Lifecycle[Subscription lifecycle callback]
  Lifecycle --> Persisted[(Persisted subscription)]
  Stripe --> OnEvent[Retryable onEvent hook]
  OnEvent --> Reload[Reload by Stripe subscription ID]
  Reload --> Scope{Exact reference type}
  Scope -->|User reference| Personal[Sync personal usage pool]
  Scope -->|Organization reference| Organization[Sync organization usage pool]
  Personal --> Success[Webhook succeeds]
  Organization --> Success
  Reload -->|Lookup failure| Retry[Webhook fails for redelivery]
  Personal -->|Sync failure| Retry
  Organization -->|Sync failure| Retry
Loading

Reviews (4): Last reviewed commit: "chore(billing): merge staging and preser..." | Re-trigger Greptile

Comment thread apps/sim/lib/billing/core/usage.ts
@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@icecrasher321 I have started the AI code review. It will take a few minutes to complete.

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@icecrasher321 I have started the AI code review. It will take a few minutes to complete.

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@icecrasher321 I have started the AI code review. It will take a few minutes to complete.

@icecrasher321
icecrasher321 merged commit 768c389 into staging Sep 9, 2026
34 checks passed
@icecrasher321
icecrasher321 deleted the codex/fix-subscription-billing-scope branch September 9, 2026 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant