🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(billing): treat missing subscription reactivation as a client error (#17675)
Max Deichmann652651a01451
diff --git a/web/src/__tests__/server/unit/chbBillingService.servertest.ts b/web/src/__tests__/server/unit/chbBillingService.servertest.tsindex 8a10fa2e0e..5b86bddc8f 100644--- a/web/src/__tests__/server/unit/chbBillingService.servertest.ts+++ b/web/src/__tests__/server/unit/chbBillingService.servertest.ts@@ -680,6 +680,24 @@ describe("chbBillingService", () => {expect(clientMock.getAttachedPlan).not.toHaveBeenCalled();expect(clientMock.clearScheduledChange).not.toHaveBeenCalled();});++ it.each([+ ["cancel", (s: ChbBillingService) => s.cancel(ORG_ID)],+ ["reactivate", (s: ChbBillingService) => s.reactivate(ORG_ID)],+ [+ "clearPlanSwitchSchedule",+ (s: ChbBillingService) => s.clearPlanSwitchSchedule(ORG_ID),+ ],+ ])(+ "maps %s without an attached plan onto PRECONDITION_FAILED",+ async (_label, call) => {+ withOrg({ clickhouse: { organizationId: CH_ORG_ID } });++ expect(await trpcCode(call(service()))).toBe("PRECONDITION_FAILED");+ expect(clientMock.setScheduledChange).not.toHaveBeenCalled();+ expect(clientMock.clearScheduledChange).not.toHaveBeenCalled();+ },+ );});describe("getInvoices", () => {diff --git a/web/src/__tests__/server/unit/stripeBillingService.servertest.ts b/web/src/__tests__/server/unit/stripeBillingService.servertest.tsnew file mode 100644index 0000000000..6a6b7f457b--- /dev/null+++ b/web/src/__tests__/server/unit/stripeBillingService.servertest.ts@@ -0,0 +1,96 @@+import type { CloudConfigSchema } from "@langfuse/shared";+import type * as SharedServer from "@langfuse/shared/src/server";+import type Stripe from "stripe";+import { TRPCError } from "@trpc/server";+import { beforeEach, describe, expect, it, vi } from "vitest";++const mocks = vi.hoisted(() => ({+ env: { NEXTAUTH_URL: "https://cloud.langfuse.com" },+ logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },+ auditLog: vi.fn(),+}));++vi.mock("@/src/env.mjs", () => ({ env: mocks.env }));++vi.mock("@/src/ee/features/billing/utils/stripe", () => ({+ stripeClient: undefined,+}));++vi.mock("@/src/features/audit-logs/server", () => ({+ auditLog: mocks.auditLog,+}));++vi.mock("@langfuse/shared/src/server", async (importOriginal) => {+ const actual = await importOriginal<typeof SharedServer>();+ return { ...actual, logger: mocks.logger };+});++import { BillingService } from "@/src/ee/features/billing/server/stripe/stripeBillingService";+import { type OrgAuthedContext } from "@/src/server/api/trpc";++const ORG_ID = "org-1";++const findUnique = vi.fn();+const subscriptions = {+ retrieve: vi.fn(),+ update: vi.fn(),+};++const stripe = { subscriptions };++const ctx = {+ prisma: { organization: { findUnique } },+ session: {+ orgId: ORG_ID,+ orgRole: "OWNER",+ user: { id: "user-1", email: "[email protected]" },+ },+} as unknown as OrgAuthedContext;++const service = () => new BillingService(stripe as unknown as Stripe, ctx);++const stubOrg = (cloudConfig: CloudConfigSchema | null) => ({+ id: ORG_ID,+ name: "Org",+ createdAt: new Date("2026-01-15T00:00:00Z"),+ updatedAt: new Date("2026-01-15T00:00:00Z"),+ cloudConfig,+ cloudBillingCycleAnchor: new Date("2026-01-15T00:00:00Z"),+ cloudCurrentCycleUsage: 0,+});++const trpcCode = async (promise: Promise<unknown>) => {+ const error = await promise.catch((e) => e);+ expect(error).toBeInstanceOf(TRPCError);+ return (error as TRPCError).code;+};++describe("stripeBillingService", () => {+ beforeEach(() => {+ vi.resetAllMocks();+ });++ describe("cancel, reactivate and clear without an active subscription", () => {+ it.each([+ ["cancel", (s: BillingService) => s.cancel(ORG_ID)],+ ["reactivate", (s: BillingService) => s.reactivate(ORG_ID)],+ [+ "clearPlanSwitchSchedule",+ (s: BillingService) => s.clearPlanSwitchSchedule(ORG_ID),+ ],+ ])(+ "maps %s onto PRECONDITION_FAILED and does not call Stripe",+ async (_label, call) => {+ findUnique.mockResolvedValue(+ stubOrg({+ stripe: { customerId: "cus_1", isLegacySubscription: false },+ }),+ );++ expect(await trpcCode(call(service()))).toBe("PRECONDITION_FAILED");+ expect(subscriptions.retrieve).not.toHaveBeenCalled();+ expect(subscriptions.update).not.toHaveBeenCalled();+ },+ );+ });+});diff --git a/web/src/ee/features/billing/server/chb/chbBillingService.ts b/web/src/ee/features/billing/server/chb/chbBillingService.tsindex cc57064000..324f77d42c 100644--- a/web/src/ee/features/billing/server/chb/chbBillingService.ts+++ b/web/src/ee/features/billing/server/chb/chbBillingService.ts@@ -702,7 +702,7 @@ export class ChbBillingService {const chb = this.requireChbState(parsedOrg);if (!chb.attachedPlanId) {throw new TRPCError({- code: "INTERNAL_SERVER_ERROR",+ code: "PRECONDITION_FAILED",message: "No active subscription to cancel",});}@@ -749,7 +749,7 @@ export class ChbBillingService {const chb = this.requireChbState(parsedOrg);if (!chb.attachedPlanId) {throw new TRPCError({- code: "INTERNAL_SERVER_ERROR",+ code: "PRECONDITION_FAILED",message: "No active subscription found",});}diff --git a/web/src/ee/features/billing/server/stripe/stripeBillingService.ts b/web/src/ee/features/billing/server/stripe/stripeBillingService.tsindex cb483678c4..27c983d84e 100644--- a/web/src/ee/features/billing/server/stripe/stripeBillingService.ts+++ b/web/src/ee/features/billing/server/stripe/stripeBillingService.ts@@ -64,7 +64,7 @@ export type BillingSubscriptionInfo = {hasValidPaymentMethod: boolean;};-class BillingService {+export class BillingService {constructor(private stripe: Stripe,private ctx: OrgAuthedContext,@@ -1187,7 +1187,7 @@ class BillingService {parsedOrg.cloudConfig?.stripe?.activeSubscriptionId;if (!subscriptionId)throw new TRPCError({- code: "INTERNAL_SERVER_ERROR",+ code: "PRECONDITION_FAILED",message: "No active subscription to cancel",});@@ -1285,7 +1285,7 @@ class BillingService {parsedOrg.cloudConfig?.stripe?.activeSubscriptionId;if (!subscriptionId)throw new TRPCError({- code: "INTERNAL_SERVER_ERROR",+ code: "PRECONDITION_FAILED",message: "No active subscription to reactivate",});@@ -1496,7 +1496,7 @@ class BillingService {parsedOrg.cloudConfig?.stripe?.activeSubscriptionId;if (!subscriptionId)throw new TRPCError({- code: "INTERNAL_SERVER_ERROR",+ code: "PRECONDITION_FAILED",message: "No active subscription found",});