Skip to content

fix(billing): bill a company once it stops being managed by Aixle - #456

Merged
artempartos merged 2 commits into
developfrom
artempartos/flow-project-56-task
Oct 9, 2026
Merged

artempartos merged 2 commits into
developfrom
artempartos/flow-project-56-task

Conversation

@artempartos

@artempartos artempartos commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes board task #168 (project 56): unticking Managed by Aixle in the admin left the company in a broken billing state.

Problem

Admin::CompaniesController#apply_billing_terms sets a company's billing terms on create only. Unticking the box on an existing company never ran it, so the company stayed active with no Stripe subscription. On the Billing tab this showed:

  • an Active badge with an empty period (— – —),
  • a Cancel subscription button that refused with "Only a paying workspace has a subscription to cancel.",
  • no trial and no way to add a card.

The meter also skipped the company, because it had no Stripe customer.

Change

When managed_by_aixle goes from true to false in saas, Company now sets billing state in a before_validation. It sits next to keep_managed_company_running, so console changes are covered too.

Company when unticked Result
No subscription, allowance not used up trialing: Billing tab shows free capacity + Add a card
No subscription, allowance already used blocked / allowance: Stopped + Add a card
Still has a live subscription from before it was managed stays active, keeps paying through it (a scheduled cancellation stays scheduled)
Subscription ended before or while it was managed blocked / canceled: Add a card opens Checkout
No worker limit refused with the same error as creating a paying company

The allowance counts all recorded usage, including the hours run while managed. This was a product decision: nobody gets a second free allowance.

The Billing tab also gets a new has_subscription flag. An active company without a subscription (for example, billing_state set by hand in the admin) is now offered Add a card instead of Cancel subscription. Checkout confirms "Card added" only when a subscription exists.

The ended case is needed because keep_managed_company_running holds a managed company active. A customer.subscription.deleted webhook therefore leaves only billing_cancels_at behind, and a past end date is how the ended subscription is recognized on untick.

UNBOUNDED_PAYING_COMPANY moved from the admin controller to Company, since both now use it.

Not in this PR

  • Companies already broken before this change (the staging one) stay active until someone acts: re-tick, save, untick, save in the admin, or a company admin adds a card from the Billing tab.
  • Ticking a company that has a live Stripe subscription still does not cancel that subscription. It stops metering and hides the Billing tab, as before.

Verification

  • A one-off integration test (not committed) walked the ticket's steps: untick in the admin, then open Billing as the company admin.
    • Before the fix: billing_state=active, no subscription, no period, Cancel refused.
    • After the fix: billing_state=trialing, 100 h allowance shown, no Cancel.
  • A second one-off probe ticked and unticked companies in each billing state (trialing, paying, ended, ended while managed). It found the ended-subscription case fixed in the second commit.
  • New tests: model (each row of the table above, and self-hosted unchanged), admin PATCH (untick → trialing, untick without a limit → 422), billing props (hasSubscription), and vitest for the page.
  • Run locally in Docker: the touched test files, changelog_test, rubocop, brakeman, tsc and eslint all pass. The full suite is left to CI.

🤖 Generated with Claude Code

artempartos and others added 2 commits October 9, 2026 20:04
Unticking "Managed by Aixle" on an existing company left it `active`
with no Stripe subscription: the Billing tab showed an Active
subscription with an empty period and a Cancel that refused, and offered
no way to add a card. Only admin create applied the billing terms.

Leaving management now puts a company without a subscription on the
allowance, or blocks it for `allowance` when the hours it ran while
managed already used it up. A company that still has a subscription
from before keeps paying through it. Unticking needs a worker limit, as
creating a paying company does. The Billing tab offers Add a card
instead of Cancel to an active company with no subscription.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
A company we carry is kept active, so the deleted webhook leaves only
its end date behind. Unticking it read the stale subscription id as a
live one and showed a cancellation in the past with a Keep subscription
button Stripe refuses. It now goes back to blocked/canceled, where Add a
card opens Checkout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
@artempartos
artempartos merged commit c8e566a into develop Oct 9, 2026
9 checks passed
artempartos added a commit that referenced this pull request Oct 30, 2026
Unticking Managed by Aixle counted the trial over everything the company
ever ran (#456), so a company we had carried for months landed stopped
the moment it was unticked. It now starts a fresh trial of the default
length, or to the last day set in the same save. A live subscription
still keeps billing it instead.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
artempartos added a commit that referenced this pull request Oct 30, 2026
…621)

* feat(billing): make the hosted trial a number of days on one worker

The trial was 100 worker-hours spent at whatever number of workers a
workspace chose, so a big limit bought a trial of a few hours and nobody
could say when it would end. A trialing company now runs one worker and
cannot change that until it adds a card; the signup form no longer asks.

The allowance is still counted in worker-hours from the usage ledger.
One worker is offered around the clock, so an hour of the trial is an
hour on the clock and the banner and Billing tab can speak in days and a
last day. TRIAL_DAYS (30) replaces TRIAL_QUEUE_HOURS; each company keeps
the length of the trial it started, and an operator moves a company's
last day from its admin page.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

* style(web): wrap the how-it-works call to action the way prettier wants

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>

* test(signup): expect the trial's one worker when the emailed link is opened

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>

* fix(billing): end every trial with its last day and keep workers fixed until a card

A browser walkthrough found the rough edges of the first cut:

- A default trial ran 720 hours from signup, so its "last day" was only the
  first hours of that day; it now ends with the last day's end, like a last
  day an operator sets, and day one is the signup day.
- A company whose trial had ended could change its workers without a card.
- The Workers card showed the stored limit's reservations under the trial's
  one worker, and the trial hint pushed the admin form sideways.
- Dates read "29 нояб. 2026 г.." in a Russian browser and "November 01" in
  the admin; the Billing tab offered a card where there is nowhere to pay.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(billing): date the trial from the meter and lock project reservations too

Counted from now, the last day ran ahead of the meter, which writes only
closed hours and left out the hour a trial began in: a trial ending at
23:40 showed the next day as its last. What is left now runs on from the
end of the last metered hour, the start hour counts, and days left is
calendar days to the last one.

A company whose ended subscription put it on a trial kept the
subscription id and so could never have its last day moved; whether a
subscription still bills is now read from the state. Projects can no
longer reserve workers while the company's workers are fixed.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(billing): a trial after an ended subscription can still be moved

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>

* feat(billing): hold a trial to one worker for everyone, and let every writer pay

Nobody chooses the number of workers before a card is added, the
platform admin included: Company and SessionConcurrencyLimit refuse any
number but one, and a company going onto a trial — or stopped at its
end — drops to one. A company that needs more is one we carry.
Unticking Managed by Aixle now says when it leaves the company stopped
for a trial it already used up.

The banner's Add a card and Pay invoice were an administrator's only, so
a workspace whose admin was away stayed stopped. Every member who can
make changes may now pay; the Billing tab and cancelling stay the
admins', and Checkout brings anyone else back to the workspace.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(billing): give a company we stop carrying a fresh trial

Unticking Managed by Aixle counted the trial over everything the company
ever ran (#456), so a company we had carried for months landed stopped
the moment it was unticked. It now starts a fresh trial of the default
length, or to the last day set in the same save. A live subscription
still keeps billing it instead.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(billing): put companies already on a trial onto one worker

Companies that signed up before kept the number of workers they typed,
which they would have been billed for the moment they paid; new signups
get one. Where we host, every company on a trial or stopped at its end
now has one, and chooses again once a card is added.

The capacity log is written in the same statement: the meter reads a
company's limit from it, so a limit changed without it would be billed
at the old number once the company pays.

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Signed-off-by: Artem Petrov <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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