🪢 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: clear free-tier suspension state on Stripe subscription upgrade (#13689)
Valery Meleshkina53be09edf8a
diff --git a/web/src/__tests__/server/stripe-webhook-handler.servertest.ts b/web/src/__tests__/server/stripe-webhook-handler.servertest.tsnew file mode 100644index 0000000000..41f566ae85--- /dev/null+++ b/web/src/__tests__/server/stripe-webhook-handler.servertest.ts@@ -0,0 +1,105 @@+import { describe, it, expect, beforeEach, afterEach } from "vitest";+import { v4 } from "uuid";+import type Stripe from "stripe";+import { prisma } from "@langfuse/shared/src/db";+import { handleSubscriptionChanged } from "@/src/ee/features/billing/server/stripeWebhookHandler";+import { env } from "@/src/env.mjs";++const buildSubscription = (args: {+ orgId: string;+ status?: Stripe.Subscription.Status;+}): Stripe.Subscription =>+ ({+ id: `sub_test_${v4()}`,+ status: args.status ?? "active",+ customer: `cus_test_${v4()}`,+ metadata: {+ orgId: args.orgId,+ cloudRegion: env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION,+ },+ items: {+ data: [+ {+ id: `si_test_${v4()}`,+ price: {+ id: `price_test_${v4()}`,+ product: `prod_test_${v4()}`,+ recurring: { usage_type: "licensed" },+ },+ },+ ],+ },+ }) as unknown as Stripe.Subscription;++describe("stripeWebhookHandler.handleSubscriptionChanged", () => {+ let orgId: string;++ beforeEach(async () => {+ orgId = v4();+ await prisma.organization.create({+ data: {+ id: orgId,+ name: `test-org-${orgId}`,+ cloudFreeTierUsageThresholdState: "BLOCKED",+ },+ });+ });++ afterEach(async () => {+ await prisma.organization.delete({ where: { id: orgId } }).catch(() => {});+ });++ it.each([["active"], ["trialing"]] as const)(+ "clears cloudFreeTierUsageThresholdState when subscription.updated arrives with status=%s",+ async (status) => {+ const subscription = buildSubscription({ orgId, status });++ await handleSubscriptionChanged(subscription, "updated");++ const updated = await prisma.organization.findUniqueOrThrow({+ where: { id: orgId },+ });+ expect(updated.cloudFreeTierUsageThresholdState).toBeNull();+ },+ );++ it("clears cloudFreeTierUsageThresholdState when a paid subscription is created", async () => {+ const subscription = buildSubscription({ orgId, status: "active" });++ await handleSubscriptionChanged(subscription, "created");++ const updated = await prisma.organization.findUniqueOrThrow({+ where: { id: orgId },+ });+ expect(updated.cloudFreeTierUsageThresholdState).toBeNull();+ });++ it.each([+ ["created", "incomplete"],+ ["created", "incomplete_expired"],+ ["updated", "past_due"],+ ["updated", "unpaid"],+ ] as const)(+ "does NOT clear cloudFreeTierUsageThresholdState on subscription.%s with status=%s",+ async (action, status) => {+ const subscription = buildSubscription({ orgId, status });++ await handleSubscriptionChanged(subscription, action);++ const updated = await prisma.organization.findUniqueOrThrow({+ where: { id: orgId },+ });+ // Side-effect assertion: subscriptionStatus is written unconditionally+ // inside the same prisma update that contains the isPaidAndCurrent+ // gate. Asserting it proves the gated branch actually ran, so the+ // "still BLOCKED" check below cannot pass by an early return.+ const stripe = (updated.cloudConfig as { stripe?: unknown } | null)+ ?.stripe as+ | { subscriptionStatus?: string; activeSubscriptionId?: string }+ | undefined;+ expect(stripe?.subscriptionStatus).toBe(status);+ expect(stripe?.activeSubscriptionId).toBe(subscription.id);+ expect(updated.cloudFreeTierUsageThresholdState).toBe("BLOCKED");+ },+ );+});diff --git a/web/src/ee/features/billing/server/stripeWebhookHandler.ts b/web/src/ee/features/billing/server/stripeWebhookHandler.tsindex 769c546d84..1cd32b5ec0 100644--- a/web/src/ee/features/billing/server/stripeWebhookHandler.ts+++ b/web/src/ee/features/billing/server/stripeWebhookHandler.ts@@ -464,7 +464,7 @@ export async function createDefaultSpendAlerts({}}-async function handleSubscriptionChanged(+export async function handleSubscriptionChanged(subscription: Stripe.Subscription,action: "created" | "deleted" | "updated",) {@@ -632,12 +632,21 @@ async function handleSubscriptionChanged(},};+ // Only clear free-tier suspension when payment is credibly current.+ // Stripe fires subscription.created/updated for non-paying states too+ // (incomplete, past_due, unpaid, ...); unblocking on those would let a+ // failed-payment org resume ingestion. The worker's hourly threshold job+ // remains the fallback that reconciles this column from usage + plan.+ const isPaidAndCurrent =+ subscription.status === "active" || subscription.status === "trialing";+await prisma.organization.update({where: {id: parsedOrg.id,},data: {cloudConfig: updatedCloudConfig,+ ...(isPaidAndCurrent ? { cloudFreeTierUsageThresholdState: null } : {}),},});