Skip to content

feat(billing): self-serve Enterprise billing — auto-pay, quarterly seat true-ups, on-demand overage - #10374

Open
waleedlatif1 wants to merge 4 commits into
stagingfrom
feat/enterprise-self-serve-billing
Open

waleedlatif1 wants to merge 4 commits into
stagingfrom
feat/enterprise-self-serve-billing

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Admins opt an Enterprise contract into self-serve with a per-seat monthly rate (PATCH /api/v1/admin/dashboard/organizations/[id]/self-serve). Turning it on clears the reporting-period anchor so usage resets on Stripe billing dates; turning it off restores the hard caps and emailed invoices, and is refused while members exceed contracted seats
  • Auto-pay: owners/admins save a card or US bank account on Stripe's hosted setup page (/api/organizations/[id]/payment-method); the checkout.session.completed webhook switches the contract to charge_automatically with that default method. Admins can return a customer to invoices (POST .../invoice-payment)
  • Seats: contracted seats become a baseline. Invites, acceptance, SSO and SCIM admit members past it (resolveSeatCap → fixed | baseline | on-demand, honouring a pending admin change). An hourly sweep in reconcile-billing-seats records each window's peak seats past the contract and bills closed windows once (quarters of an annual term, or the monthly term) at the rate the window opened with — new enterprise_seat_true_up table
  • Usage: owners/admins set their own limit (stored in organization.self_serve_usage_limit so the contract webhook keeps it); usage past the included allowance is billed through the existing threshold billing, period close, and cancellation paths. Prepaid credit is spent first
  • Overage, true-up and final invoices follow the subscription's collection method: invoiced customers get an emailed invoice (30-day terms) instead of an open charge nothing collects
  • Billing settings: usage limit + on-demand switch, a three-row Seats section, and a payment-method row for self-serve contracts. Non-self-serve Enterprise is unchanged
  • Follow-ups (pre-existing, not changed here): ad-hoc overage invoices are created with auto_advance: false, so a failed auto-charge is not retried by Stripe; the final-overage invoice still duplicates the outbox invoice builder

Type of Change

  • New feature

Testing

  • lib/billing/enterprise-self-serve.integration.ts (12 tests, real Postgres, Stripe stubbed): usage-limit gating and floor, overage on/off by contract, joins past seats (self-serve admits, hard cap refuses, missing seat rate stays capped), true-up peak/once-only/open-window, auto-pay switch and refusal of foreign/non-self-serve sessions. Reverting the self-serve predicate, the once-only guard, and the join guard each turned their tests red
  • 1,084 affected unit tests across lib/billing, lib/admin, lib/invitations, lib/workspaces, billing/org routes
  • bun run lint, check:migrations origin/staging, drizzle schema sync, bun run generate; check:api-validation:strict, boundaries, client-boundary, comment-hygiene, test-patterns, file-names, source-conventions, explicit-any, react-query, utils, unused-exports (baseline shrunk)
  • Full type-check and test suite in CI

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • 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 Nov 24, 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 Nov 24, 2026 8:10pm UTC

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 51 files

This PR changes payment, billing, or quota logic. Ultrareviews find 2.4x more serious bugs than standard reviews. Comment @cubic-dev-ai ultrareview to run one.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/lib/admin/dashboard.ts Resolved
@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[High impact] The PR changes Enterprise usage charges, seat admission, scheduled billing, and Stripe invoice collection.

Fix the invoice-success path before merging so paying one invoice cannot clear blocks caused by another unpaid invoice.

Findings

  1. P1 Unpaid invoices lose their block ▶

Summary

Adds self-serve Enterprise billing with saved payment methods, customer-set usage limits, and seat true-ups.

  • Enterprise admins can switch contracts into self-serve billing.
  • Self-serve Enterprise customers can set a usage limit and receive overage invoices.
  • Self-serve Enterprise members can exceed their contracted seats and get billed later.
  • Self-serve Enterprise customers can turn on auto-pay and keep invoices on their chosen collection method.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  Admin[Admin enables self-serve] --> Contract[Enterprise contract]
  Contract --> Seats[Hourly seat peak sample]
  Seats --> Window[Closed true-up window]
  Window --> Queue[Invoice queue]
  Queue --> Stripe[Stripe invoice]
  Stripe --> Events[Payment events]
  Events --> Access[Organization billing block]
Loading

Reviews (4) · Last reviewed commit: "fix(billing): recheck self-serve under t..." · Reviewed by Greptile

Comment thread apps/sim/lib/admin/dashboard.ts Resolved
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

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

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 55 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 force-pushed the feat/enterprise-self-serve-billing branch from cdcf3c9 to 1c07331 Compare November 24, 2026 19:51
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

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

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 56 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

…at true-ups, on-demand overage

Admins opt an Enterprise contract into self-serve with a per-seat monthly rate. On a self-serve contract:
- owners and admins save a card or US bank account on Stripe's hosted setup page, and the contract switches from emailed invoices to automatic payment
- members may join past the contracted seats; each quarter (each month on a monthly contract) the peak seats past the contract are invoiced in arrears at the rate the window opened with
- owners and admins raise their own usage limit; usage past the included allowance is billed as overage through the existing threshold billing and period close

Contracts that are not self-serve keep their hard seat and usage caps. Overage, true-up, and final invoices follow the subscription's collection method, so invoiced customers get an emailed invoice instead of an open charge nothing collects.
@waleedlatif1
waleedlatif1 force-pushed the feat/enterprise-self-serve-billing branch from 1c07331 to c42155c Compare November 24, 2026 20:04
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

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

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 57 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

'overage_billing',
'overage_threshold_billing',
'overage_threshold_billing_org',
'enterprise_seat_true_up',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Unpaid invoices lose their block

Adding enterprise_seat_true_up to this lookup sends successful seat payments through unblockOrgMembers. That clears every payment_failed block for the organization, even when another true-up or overage invoice remains unpaid. These invoices are separate from renewals, so the subscription can remain active throughout. Check for other unpaid invoices before restoring access.

This branch was successfully deployed

1 active deployment
Preview — c42155c5 Deployed Nov 24, 2026 by vercel[bot]
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