From eb43059fda70c44b37e28991ae38b4b64d087910 Mon Sep 17 00:00:00 2001 From: giancarlo Date: Tue, 2 Apr 2024 14:31:53 +0800 Subject: [PATCH] Update billing provider and related adjustments This commit switches the billing provider from Stripe to Lemon Squeezy. It also includes necessary modifications in the web app, billing gateway, and database to accommodate this change. Additionally, updates have been made to the checkout process, billing strategy, and relevant services and components. --- apps/web/config/billing.config.ts | 2 +- .../lemon-squeezy-webhook-handler.service.ts | 13 +++++++++---- supabase/migrations/20221215192558_schema.sql | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/apps/web/config/billing.config.ts b/apps/web/config/billing.config.ts index c3009ca5c..10d35be9f 100644 --- a/apps/web/config/billing.config.ts +++ b/apps/web/config/billing.config.ts @@ -25,7 +25,7 @@ export default createBillingSchema({ paymentType: 'one-time', lineItems: [ { - id: 'price_1P0jgcI1i3VnbZTqXVXaZkMP', + id: '324643', name: 'Base', description: 'Base plan', cost: 999.99, diff --git a/packages/billing/lemon-squeezy/src/services/lemon-squeezy-webhook-handler.service.ts b/packages/billing/lemon-squeezy/src/services/lemon-squeezy-webhook-handler.service.ts index fd3b0e3e4..7f5f94548 100644 --- a/packages/billing/lemon-squeezy/src/services/lemon-squeezy-webhook-handler.service.ts +++ b/packages/billing/lemon-squeezy/src/services/lemon-squeezy-webhook-handler.service.ts @@ -1,4 +1,4 @@ -import { getOrder } from '@lemonsqueezy/lemonsqueezy.js'; +import { getOrder, getVariant } from '@lemonsqueezy/lemonsqueezy.js'; import { createHmac, timingSafeEqual } from 'crypto'; import { BillingWebhookHandlerService } from '@kit/billing'; @@ -139,10 +139,15 @@ export class LemonSqueezyWebhookHandlerService ) { await initializeLemonSqueezyClient(); - const subscription = event.data.relationships.subscriptions.links.self; + // we fetch the variant to check if the order is a subscription + // if Lemon Squeezy was able to discriminate between orders and subscriptions + // it would be better to use that information. But for now, we need to fetch the variant + const variantId = event.data.attributes.first_order_item.variant_id; + const { data } = await getVariant(variantId); - if (subscription) { - // we handle the subscription created event instead + // if the order is a subscription + // we handle it in the subscription created event + if (data?.data.attributes.is_subscription) { return; } diff --git a/supabase/migrations/20221215192558_schema.sql b/supabase/migrations/20221215192558_schema.sql index cdfc96d93..140278c37 100644 --- a/supabase/migrations/20221215192558_schema.sql +++ b/supabase/migrations/20221215192558_schema.sql @@ -1676,7 +1676,7 @@ create policy create_org_account on public.accounts for insert to authenticated with check ( public.is_set( - 'enable_organization_accounts') + 'enable_team_accounts') and public.accounts.is_personal_account = false); create or replace function public.create_invitation(account_id uuid,