🪢 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
feat(billing): Allow past subscribers see their invoices (#9232)
Michael Fröhlich45215cb4ffa9
diff --git a/web/src/ee/features/billing/components/BillingDiscountView.tsx b/web/src/ee/features/billing/components/BillingDiscountView.tsxindex 474db658c2..958bd212b9 100644--- a/web/src/ee/features/billing/components/BillingDiscountView.tsx+++ b/web/src/ee/features/billing/components/BillingDiscountView.tsx@@ -1,14 +1,16 @@-import { useRouter } from "next/router";import { api } from "@/src/utils/api";import { Badge } from "@/src/components/ui/badge";+import { useBillingInformation } from "@/src/ee/features/billing/components/useBillingInformation";export const BillingDiscountView = () => {- const router = useRouter();- const orgId = router.query.organizationId as string | undefined;+ const { organization } = useBillingInformation();+ const shouldRenderComponent = Boolean(+ organization?.cloudConfig?.stripe?.customerId,+ );const { data } = api.cloudBilling.getSubscriptionInfo.useQuery(- { orgId: orgId ?? "" },- { enabled: Boolean(orgId) },+ { orgId: organization?.id ?? "" },+ { enabled: Boolean(organization?.id && shouldRenderComponent) },);const discounts = data?.discounts ?? [];@@ -28,6 +30,8 @@ export const BillingDiscountView = () => {}};+ if (!shouldRenderComponent) return null;+if (!discounts.length) return null;return (diff --git a/web/src/ee/features/billing/components/BillingInvoiceTable.tsx b/web/src/ee/features/billing/components/BillingInvoiceTable.tsxindex 30cd7ca3dd..7b41c77c72 100644--- a/web/src/ee/features/billing/components/BillingInvoiceTable.tsx+++ b/web/src/ee/features/billing/components/BillingInvoiceTable.tsx@@ -9,6 +9,8 @@ import { Download, ExternalLink } from "lucide-react";import { formatLocalIsoDate } from "@/src/components/LocalIsoDate";import { useEffect, useMemo, useState } from "react";+import { useBillingInformation } from "./useBillingInformation";+type InvoiceRow = {id: string;number: string | null;@@ -26,7 +28,12 @@ type InvoiceRow = {};};-export function BillingInvoiceTable({ orgId }: { orgId: string }) {+export function BillingInvoiceTable() {+ const { organization } = useBillingInformation();+ const shouldShowTable = Boolean(+ organization?.cloudConfig?.stripe?.customerId,+ );+const [virtualTotal, setVirtualTotal] = useState(9999);const [paginationState, setPaginationState] = useState<{pageIndex: number;@@ -35,12 +42,18 @@ export function BillingInvoiceTable({ orgId }: { orgId: string }) {endingBefore?: string;}>({ pageIndex: 0, pageSize: 10 });- const invoicesQuery = api.cloudBilling.getInvoices.useQuery({- orgId,- limit: paginationState.pageSize,- startingAfter: paginationState.startingAfter,- endingBefore: paginationState.endingBefore,- });+ const invoicesQuery = api.cloudBilling.getInvoices.useQuery(+ {+ orgId: organization?.id ?? "",+ limit: paginationState.pageSize,+ startingAfter: paginationState.startingAfter,+ endingBefore: paginationState.endingBefore,+ },+ {+ enabled: shouldShowTable,+ retry: false,+ },+ );const isFirstPage =!paginationState.startingAfter && !paginationState.endingBefore;@@ -65,23 +78,20 @@ export function BillingInvoiceTable({ orgId }: { orgId: string }) {return { isLoading: true, isError: false } as const;}if (invoicesQuery.isError) {+ // setting the error causes the table to remaining in loading state+ // instead we just return an empty arrayreturn {isLoading: false,- isError: true,- error: invoicesQuery.error.message,+ isError: false,+ data: [] as InvoiceRow[],} as const;}return { isLoading: false, isError: false, data: rows } as const;- }, [- rows,- invoicesQuery.isPending,- invoicesQuery.isError,- invoicesQuery.error,- ]);+ }, [rows, invoicesQuery.isPending, invoicesQuery.isError]);useEffect(() => {if (isFirstPage) setVirtualTotal(9999);- }, [orgId, paginationState.pageSize, isFirstPage]);+ }, [organization?.id, paginationState.pageSize, isFirstPage]);// When we fetch a page that reports hasMore === false, lock in the exact sizeuseEffect(() => {@@ -260,6 +270,11 @@ export function BillingInvoiceTable({ orgId }: { orgId: string }) {}};+ if (!shouldShowTable) {+ // users on hobby plan who never had a subscription+ return null;+ }+return (<div className="space-y-0"><div className="flex items-center justify-between pt-4">diff --git a/web/src/ee/features/billing/components/BillingSettings.tsx b/web/src/ee/features/billing/components/BillingSettings.tsxindex 651a784df4..747950b065 100644--- a/web/src/ee/features/billing/components/BillingSettings.tsx+++ b/web/src/ee/features/billing/components/BillingSettings.tsx@@ -11,8 +11,8 @@ import { BillingUsageChart } from "./BillingUsageChart";import { BillingActionButtons } from "./BillingActionButtons";import { BillingScheduleNotification } from "./BillingScheduleNotification";import { BillingInvoiceTable } from "./BillingInvoiceTable";-import { useBillingInformation } from "./useBillingInformation";import { BillingDiscountView } from "./BillingDiscountView";+import { BillingPlanPeriodView } from "@/src/ee/features/billing/components/BillingPlanPeriodView";export const BillingSettings = () => {const router = useRouter();@@ -24,7 +24,7 @@ export const BillingSettings = () => {const entitled = useHasEntitlement("cloud-billing");const isUsageAlertEntitled = useHasEntitlement("cloud-usage-alerts");- const billingInfo = useBillingInformation();+if (!entitled) return null;if (!hasAccess)@@ -45,12 +45,11 @@ export const BillingSettings = () => {<Header title="Usage & Billing" /><div className="space-y-6"><BillingUsageChart />- {orgId && billingInfo.hasActiveSubscription && <BillingDiscountView />}+ <BillingPlanPeriodView />+ <BillingDiscountView /><BillingActionButtons />{isUsageAlertEntitled && orgId && <UsageAlerts orgId={orgId} />}- {orgId && billingInfo.hasActiveSubscription && (- <BillingInvoiceTable orgId={orgId} />- )}+ <BillingInvoiceTable /></div></div>);diff --git a/web/src/ee/features/billing/components/BillingUsageChart.tsx b/web/src/ee/features/billing/components/BillingUsageChart.tsxindex ae3e89d6ef..c34e723d87 100644--- a/web/src/ee/features/billing/components/BillingUsageChart.tsx+++ b/web/src/ee/features/billing/components/BillingUsageChart.tsx@@ -7,7 +7,6 @@ import { Card } from "@/src/components/ui/card";import { numberFormatter, compactNumberFormatter } from "@/src/utils/numbers";import { type Plan } from "@langfuse/shared";import { MAX_EVENTS_FREE_PLAN } from "@/src/ee/features/billing/constants";-import { BillingPlanPeriodView } from "@/src/ee/features/billing/components/BillingPlanPeriodView";export const BillingUsageChart = () => {const organization = useQueryOrganization();@@ -71,9 +70,6 @@ export const BillingUsageChart = () => {</span>)}</Card>- <div className="mt-2">- <BillingPlanPeriodView />- </div></div>);};diff --git a/web/src/ee/features/billing/server/stripeBillingService.ts b/web/src/ee/features/billing/server/stripeBillingService.tsindex 18c0884e86..7c79721b6b 100644--- a/web/src/ee/features/billing/server/stripeBillingService.ts+++ b/web/src/ee/features/billing/server/stripeBillingService.ts@@ -156,16 +156,17 @@ class BillingService {private async retrieveInvoiceList(stripeCustomerId: string,- subscriptionId: string,limit: number,startingAfter?: string,endingBefore?: string,) {const client = this.stripe;+ // Note: We assume each stripe Customer has only one subscription+ // if this changes, we need to update the code to not leak other subscriptions+const result = await client.invoices.list({customer: stripeCustomerId,- subscription: subscriptionId, // one customer may have multiple subscriptions (one per org)limit: limit,starting_after: startingAfter,ending_before: endingBefore,@@ -1107,10 +1108,11 @@ class BillingService {return { status: "noop" as const };}- const subscription = await this.retrieveSubscriptionWithSchedule(- client,- subscriptionId,- );+ const subscription =+ await this.retrieveSubscriptionWithScheduleAndDiscounts(+ client,+ subscriptionId,+ );// Release any pending schedule before immediate cancelawait this.releaseExistingSubscriptionScheduleIfAny(subscription, opId);@@ -1215,27 +1217,28 @@ class BillingService {const stripeCustomerId = parsedOrg.cloudConfig?.stripe?.customerId;const stripeSubscriptionId =parsedOrg.cloudConfig?.stripe?.activeSubscriptionId;-- if (!stripeCustomerId || !stripeSubscriptionId) {+ if (!stripeCustomerId) {throw new TRPCError({code: "INTERNAL_SERVER_ERROR",message: "No stripe customer or subscription found",});}+ // retrieve all invoices for the customer (also past subscriptions when cancelled)const list = await this.retrieveInvoiceList(stripeCustomerId,- stripeSubscriptionId,pagination.limit,pagination.startingAfter,pagination.endingBefore,);- const preview = await this.createInvoicePreview(- client,- stripeCustomerId,- stripeSubscriptionId,- );+ const preview = stripeSubscriptionId+ ? await this.createInvoicePreview(+ client,+ stripeCustomerId,+ stripeSubscriptionId,+ )+ : null;const priceCache = new Map<string, Stripe.Price>();