Improve billing plan lookup (#270)

Retrieve plan from Stripe/LS if not found in billing configuration. Useful for legacy plans.
This commit is contained in:
Giancarlo Buomprisco
2025-06-13 16:45:55 +07:00
committed by GitHub
parent 2b21b7bed4
commit 856e9612c4
11 changed files with 183 additions and 38 deletions

View File

@@ -1,6 +1,6 @@
import { BadgeCheck } from 'lucide-react';
import { BillingConfig, getProductPlanPairByVariantId } from '@kit/billing';
import { PlanSchema, type ProductSchema } from '@kit/billing';
import { Tables } from '@kit/supabase/database';
import {
Card,
@@ -22,12 +22,14 @@ interface Props {
items: LineItem[];
};
config: BillingConfig;
product: ProductSchema;
plan: ReturnType<(typeof PlanSchema)['parse']>;
}
export function CurrentLifetimeOrderCard({
order,
config,
product,
plan,
}: React.PropsWithChildren<Props>) {
const lineItems = order.items;
const firstLineItem = lineItems[0];
@@ -36,17 +38,6 @@ export function CurrentLifetimeOrderCard({
throw new Error('No line items found in subscription');
}
const { product, plan } = getProductPlanPairByVariantId(
config,
firstLineItem.variant_id,
);
if (!product || !plan) {
throw new Error(
'Product or plan not found. Did you forget to add it to the billing config?',
);
}
const productLineItems = plan.lineItems;
return (

View File

@@ -1,7 +1,7 @@
import { formatDate } from 'date-fns';
import { BadgeCheck } from 'lucide-react';
import { BillingConfig, getProductPlanPairByVariantId } from '@kit/billing';
import { PlanSchema, type ProductSchema } from '@kit/billing';
import { Tables } from '@kit/supabase/database';
import { Alert, AlertDescription, AlertTitle } from '@kit/ui/alert';
import {
@@ -26,12 +26,14 @@ interface Props {
items: LineItem[];
};
config: BillingConfig;
product: ProductSchema;
plan: ReturnType<(typeof PlanSchema)['parse']>;
}
export function CurrentSubscriptionCard({
subscription,
config,
product,
plan,
}: React.PropsWithChildren<Props>) {
const lineItems = subscription.items;
const firstLineItem = lineItems[0];
@@ -40,17 +42,6 @@ export function CurrentSubscriptionCard({
throw new Error('No line items found in subscription');
}
const { product, plan } = getProductPlanPairByVariantId(
config,
firstLineItem.variant_id,
);
if (!product || !plan) {
throw new Error(
'Product or plan not found. Did you forget to add it to the billing config?',
);
}
const productLineItems = plan.lineItems;
return (