Skip to content

Commit 5d85711

Browse files
authored
Fix billing access during active pending renewals (#1780)
1 parent b88e3df commit 5d85711

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

‎apps/api/src/modules/billing/billing-reconcile.test.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,25 @@ describe("Oblien-managed entitlements", () => {
116116
h.balance.mockResolvedValue({ ...state.balance, billingMode: "payg" });
117117
await expect(assertCloudCanSpend("org_1")).rejects.toMatchObject({ code: "CLOUD_BILLING_BLOCKED" });
118118
});
119-
it("allows billing recovery after an unpaid renewal without granting compute or credits", async () => {
119+
it.each(["active", "past_due"] as const)("allows billing recovery during a %s unpaid renewal without granting compute or credits", async status => {
120120
const state = unpaidMonthlyCloudBilling();
121+
state.subscription.status = status;
121122
h.entitlement.mockResolvedValue(state.entitlement);
122123
h.subscription.mockResolvedValue({ namespace: state.entitlement.namespace, subscription: state.subscription });
123124
h.balance.mockResolvedValue(state.balance);
124125
const result = await syncOblienEntitlement("org_1", { syncResourceLimits: false });
125-
expect(result).toMatchObject({ tier: "pro", subscription: { status: "past_due" }, entitlement: { status: "canceled", computeCovered: false } });
126+
expect(result).toMatchObject({ tier: "pro", subscription: { status }, entitlement: { status: "canceled", computeCovered: false } });
126127
await expect(assertNamespaceHasQuota("org_1")).resolves.toBeUndefined();
127128
await expect(assertCloudCanSpend("org_1")).rejects.toMatchObject({ statusCode: 402, code: "CLOUD_BILLING_BLOCKED" });
129+
expect(h.mirror).toHaveBeenCalledWith("workspace_1", "org_1", "os-customer", {
130+
planTierId: "pro", subscriptionStatus: "canceled",
131+
currentPeriodStart: new Date(state.entitlement.periodStart!),
132+
currentPeriodEnd: new Date(state.entitlement.periodEnd!),
133+
});
134+
// A newer balance response alone must not turn old, uncovered capacity into
135+
// paid compute. Both authoritative reads must confirm the funded month.
136+
h.balance.mockResolvedValue({ ...state.balance, blocking: false, computeCovered: true });
137+
await expect(assertCloudCanSpend("org_1")).rejects.toMatchObject({ statusCode: 402, code: "CLOUD_BILLING_BLOCKED" });
128138
expect(h.limits).not.toHaveBeenCalled();
129139
expect(h.setQuota).not.toHaveBeenCalled();
130140
expect(h.resetQuota).not.toHaveBeenCalled();

‎apps/api/src/modules/billing/oblien-billing-api.test.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ describe("Oblien monthly capacity contract", () => {
659659
.toThrow(/does not match/);
660660
}
661661
});
662-
it.each(["past_due", "unpaid", "paused", "canceled"] as const)("keeps billing readable for an expired paid month and a %s renewal", async status => {
662+
it.each(["active", "past_due", "unpaid", "paused", "canceled"] as const)("keeps billing readable for an expired paid month and a %s renewal", async status => {
663663
const state = unpaidMonthlyCloudBilling("org-one", "os-one");
664664
state.subscription.status = status;
665665
const entitlement = await setup(state.entitlement).api.getEntitlement("os-one");
@@ -668,9 +668,8 @@ describe("Oblien monthly capacity contract", () => {
668668
expect(entitlement).toMatchObject({ status: "canceled", computeCovered: false, capacity: { status: "expired", computeCovered: false } });
669669
expect(entitlement.periodEnd).not.toBe(subscription!.periodEnd);
670670
});
671-
it("still rejects funded, overlapping, foreign or inconsistent unpaid renewal records", () => {
671+
it.each(["active", "past_due"] as const)("still rejects funded, overlapping, foreign or inconsistent %s renewal records", status => {
672672
for (const mutate of [
673-
(state: ReturnType<typeof unpaidMonthlyCloudBilling>) => { state.subscription.status = "active"; },
674673
(state: ReturnType<typeof unpaidMonthlyCloudBilling>) => { state.subscription.status = "trialing"; },
675674
(state: ReturnType<typeof unpaidMonthlyCloudBilling>) => { state.entitlement.status = "active"; },
676675
(state: ReturnType<typeof unpaidMonthlyCloudBilling>) => { state.entitlement.computeCovered = state.entitlement.capacity!.computeCovered = true; },
@@ -681,9 +680,12 @@ describe("Oblien monthly capacity contract", () => {
681680
(state: ReturnType<typeof unpaidMonthlyCloudBilling>) => { state.subscription.periodStart = "2026-10-15T00:00:00Z"; },
682681
(state: ReturnType<typeof unpaidMonthlyCloudBilling>) => { state.subscription.periodStart = null; },
683682
(state: ReturnType<typeof unpaidMonthlyCloudBilling>) => { state.subscription.periodEnd = state.subscription.periodStart; },
683+
(state: ReturnType<typeof unpaidMonthlyCloudBilling>) => { state.entitlement.periodStart = state.entitlement.capacity!.periodStart = state.entitlement.periodEnd!; },
684684
(state: ReturnType<typeof unpaidMonthlyCloudBilling>) => { state.entitlement.capacity!.periodEnd = "2026-10-31T00:00:00Z"; },
685685
]) {
686-
const state = unpaidMonthlyCloudBilling(); mutate(state);
686+
const state = unpaidMonthlyCloudBilling();
687+
state.subscription.status = status;
688+
mutate(state);
687689
expect(() => assertOblienEntitlementMatchesSubscription(state.entitlement, state.subscription)).toThrow(/does not match/);
688690
}
689691
});

‎packages/platform/src/engine/lib/oblien-billing-api.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,13 @@ export function assertOblienEntitlementMatchesSubscription(entitlement: OblienEn
231231
const monthly = subscription?.offer?.billingMode === "monthly";
232232
const expectedTier = monthly ? "capacity" : subscription?.tierId ?? "free";
233233
const capacity = entitlement.capacity;
234-
// An unpaid renewal advances Stripe's invoice period without buying another
235-
// month of compute. The retained, expired capacity still describes the last
236-
// funded period. Keep billing readable so the customer can pay, while the
237-
// provider's uncovered entitlement continues to block new billable work.
234+
// Stripe can advance an active subscription's period while its renewal invoice
235+
// is still a draft, before attempting payment. An overdue renewal has the same
236+
// period split: expired capacity describes the last funded month. Accept this
237+
// uncovered state for billing/management reads without granting compute or
238+
// inventing a grace period; billable work still requires provider coverage.
238239
const unpaidRenewal = monthly && subscription &&
239-
["past_due", "unpaid", "paused", "canceled"].includes(subscription.status) &&
240+
["active", "past_due", "unpaid", "paused", "canceled"].includes(subscription.status) &&
240241
entitlement.status !== "active" && entitlement.computeCovered === false &&
241242
capacity?.computeCovered === false && capacity.status === "expired" &&
242243
entitlement.periodStart !== null && entitlement.periodEnd !== null &&

0 commit comments

Comments
 (0)