From d44a2a1fd18ff2209385103a181260e2b950e2f2 Mon Sep 17 00:00:00 2001 From: Hydra Date: Sat, 14 Nov 2026 22:56:29 +0200 Subject: [PATCH] Fix billing access during active pending renewals --- .../src/modules/billing/billing-reconcile.test.ts | 14 ++++++++++++-- .../src/modules/billing/oblien-billing-api.test.ts | 10 ++++++---- .../platform/src/engine/lib/oblien-billing-api.ts | 11 ++++++----- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/apps/api/src/modules/billing/billing-reconcile.test.ts b/apps/api/src/modules/billing/billing-reconcile.test.ts index 5d92b3621..3ee761206 100644 --- a/apps/api/src/modules/billing/billing-reconcile.test.ts +++ b/apps/api/src/modules/billing/billing-reconcile.test.ts @@ -116,15 +116,25 @@ describe("Oblien-managed entitlements", () => { h.balance.mockResolvedValue({ ...state.balance, billingMode: "payg" }); await expect(assertCloudCanSpend("org_1")).rejects.toMatchObject({ code: "CLOUD_BILLING_BLOCKED" }); }); - it("allows billing recovery after an unpaid renewal without granting compute or credits", async () => { + it.each(["active", "past_due"] as const)("allows billing recovery during a %s unpaid renewal without granting compute or credits", async status => { const state = unpaidMonthlyCloudBilling(); + state.subscription.status = status; h.entitlement.mockResolvedValue(state.entitlement); h.subscription.mockResolvedValue({ namespace: state.entitlement.namespace, subscription: state.subscription }); h.balance.mockResolvedValue(state.balance); const result = await syncOblienEntitlement("org_1", { syncResourceLimits: false }); - expect(result).toMatchObject({ tier: "pro", subscription: { status: "past_due" }, entitlement: { status: "canceled", computeCovered: false } }); + expect(result).toMatchObject({ tier: "pro", subscription: { status }, entitlement: { status: "canceled", computeCovered: false } }); await expect(assertNamespaceHasQuota("org_1")).resolves.toBeUndefined(); await expect(assertCloudCanSpend("org_1")).rejects.toMatchObject({ statusCode: 402, code: "CLOUD_BILLING_BLOCKED" }); + expect(h.mirror).toHaveBeenCalledWith("workspace_1", "org_1", "os-customer", { + planTierId: "pro", subscriptionStatus: "canceled", + currentPeriodStart: new Date(state.entitlement.periodStart!), + currentPeriodEnd: new Date(state.entitlement.periodEnd!), + }); + // A newer balance response alone must not turn old, uncovered capacity into + // paid compute. Both authoritative reads must confirm the funded month. + h.balance.mockResolvedValue({ ...state.balance, blocking: false, computeCovered: true }); + await expect(assertCloudCanSpend("org_1")).rejects.toMatchObject({ statusCode: 402, code: "CLOUD_BILLING_BLOCKED" }); expect(h.limits).not.toHaveBeenCalled(); expect(h.setQuota).not.toHaveBeenCalled(); expect(h.resetQuota).not.toHaveBeenCalled(); diff --git a/apps/api/src/modules/billing/oblien-billing-api.test.ts b/apps/api/src/modules/billing/oblien-billing-api.test.ts index 881c093c6..1723cabb3 100644 --- a/apps/api/src/modules/billing/oblien-billing-api.test.ts +++ b/apps/api/src/modules/billing/oblien-billing-api.test.ts @@ -659,7 +659,7 @@ describe("Oblien monthly capacity contract", () => { .toThrow(/does not match/); } }); - it.each(["past_due", "unpaid", "paused", "canceled"] as const)("keeps billing readable for an expired paid month and a %s renewal", async status => { + it.each(["active", "past_due", "unpaid", "paused", "canceled"] as const)("keeps billing readable for an expired paid month and a %s renewal", async status => { const state = unpaidMonthlyCloudBilling("org-one", "os-one"); state.subscription.status = status; const entitlement = await setup(state.entitlement).api.getEntitlement("os-one"); @@ -668,9 +668,8 @@ describe("Oblien monthly capacity contract", () => { expect(entitlement).toMatchObject({ status: "canceled", computeCovered: false, capacity: { status: "expired", computeCovered: false } }); expect(entitlement.periodEnd).not.toBe(subscription!.periodEnd); }); - it("still rejects funded, overlapping, foreign or inconsistent unpaid renewal records", () => { + it.each(["active", "past_due"] as const)("still rejects funded, overlapping, foreign or inconsistent %s renewal records", status => { for (const mutate of [ - (state: ReturnType) => { state.subscription.status = "active"; }, (state: ReturnType) => { state.subscription.status = "trialing"; }, (state: ReturnType) => { state.entitlement.status = "active"; }, (state: ReturnType) => { state.entitlement.computeCovered = state.entitlement.capacity!.computeCovered = true; }, @@ -681,9 +680,12 @@ describe("Oblien monthly capacity contract", () => { (state: ReturnType) => { state.subscription.periodStart = "2026-10-15T00:00:00Z"; }, (state: ReturnType) => { state.subscription.periodStart = null; }, (state: ReturnType) => { state.subscription.periodEnd = state.subscription.periodStart; }, + (state: ReturnType) => { state.entitlement.periodStart = state.entitlement.capacity!.periodStart = state.entitlement.periodEnd!; }, (state: ReturnType) => { state.entitlement.capacity!.periodEnd = "2026-10-31T00:00:00Z"; }, ]) { - const state = unpaidMonthlyCloudBilling(); mutate(state); + const state = unpaidMonthlyCloudBilling(); + state.subscription.status = status; + mutate(state); expect(() => assertOblienEntitlementMatchesSubscription(state.entitlement, state.subscription)).toThrow(/does not match/); } }); diff --git a/packages/platform/src/engine/lib/oblien-billing-api.ts b/packages/platform/src/engine/lib/oblien-billing-api.ts index f03eb63ce..6d9e7ee50 100644 --- a/packages/platform/src/engine/lib/oblien-billing-api.ts +++ b/packages/platform/src/engine/lib/oblien-billing-api.ts @@ -231,12 +231,13 @@ export function assertOblienEntitlementMatchesSubscription(entitlement: OblienEn const monthly = subscription?.offer?.billingMode === "monthly"; const expectedTier = monthly ? "capacity" : subscription?.tierId ?? "free"; const capacity = entitlement.capacity; - // An unpaid renewal advances Stripe's invoice period without buying another - // month of compute. The retained, expired capacity still describes the last - // funded period. Keep billing readable so the customer can pay, while the - // provider's uncovered entitlement continues to block new billable work. + // Stripe can advance an active subscription's period while its renewal invoice + // is still a draft, before attempting payment. An overdue renewal has the same + // period split: expired capacity describes the last funded month. Accept this + // uncovered state for billing/management reads without granting compute or + // inventing a grace period; billable work still requires provider coverage. const unpaidRenewal = monthly && subscription && - ["past_due", "unpaid", "paused", "canceled"].includes(subscription.status) && + ["active", "past_due", "unpaid", "paused", "canceled"].includes(subscription.status) && entitlement.status !== "active" && entitlement.computeCovered === false && capacity?.computeCovered === false && capacity.status === "expired" && entitlement.periodStart !== null && entitlement.periodEnd !== null &&