From ced1b4f7e3f6a8355e2348414d92638887eccc3c Mon Sep 17 00:00:00 2001 From: giancarlo Date: Sat, 1 Jun 2024 17:03:20 +0700 Subject: [PATCH] Add refinement to ensure metered line items have zero cost The billing schema has been updated to include a refinement that ensures metered line items have a cost of zero. To clarify, a different line item type should be added for a flat fee in Stripe. Additionally, unnecessary verifications and logic regarding item costs have been removed from the PlanPicker and LineItemDetails components. --- .../billing/core/src/create-billing-schema.ts | 14 ++++++++++++++ .../gateway/src/components/line-item-details.tsx | 4 ---- .../gateway/src/components/plan-picker.tsx | 15 --------------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/packages/billing/core/src/create-billing-schema.ts b/packages/billing/core/src/create-billing-schema.ts index 5c58d3817..159f4616e 100644 --- a/packages/billing/core/src/create-billing-schema.ts +++ b/packages/billing/core/src/create-billing-schema.ts @@ -72,6 +72,20 @@ export const LineItemSchema = z message: 'Metered line items must have a unit and tiers', path: ['type', 'unit', 'tiers'], }, + ) + .refine( + (data) => { + if (data.type === LineItemType.Metered) { + return data.cost === 0; + } + + return true; + }, + { + message: + 'Metered line items must have a cost of 0. Please add a different line item type for a flat fee (Stripe)', + path: ['type', 'cost'], + }, ); export const PlanSchema = z diff --git a/packages/billing/gateway/src/components/line-item-details.tsx b/packages/billing/gateway/src/components/line-item-details.tsx index 26ed4c4b9..aa3f8d157 100644 --- a/packages/billing/gateway/src/components/line-item-details.tsx +++ b/packages/billing/gateway/src/components/line-item-details.tsx @@ -187,10 +187,6 @@ export function LineItemDetails( return ; case 'metered': { - if (item.cost > 0) { - return ; - } - return ; } } diff --git a/packages/billing/gateway/src/components/plan-picker.tsx b/packages/billing/gateway/src/components/plan-picker.tsx index 797748f98..c996fe716 100644 --- a/packages/billing/gateway/src/components/plan-picker.tsx +++ b/packages/billing/gateway/src/components/plan-picker.tsx @@ -79,21 +79,6 @@ export function PlanPicker( } }, { message: t('noPlanChosen'), path: ['planId'] }, - ) - .refine( - (data) => { - try { - const { plan } = getProductPlanPair(props.config, data.planId); - - return !(plan.paymentType === 'recurring' && !data.interval); - } catch { - return false; - } - }, - { - message: t('noIntervalPlanChosen'), - path: ['interval'], - }, ), ), defaultValues: {