From 26a1c749f7c4b06d4c1321ed8745814b83183e23 Mon Sep 17 00:00:00 2001 From: Rajiv Sahal Date: Mon, 30 Sep 2024 16:26:21 +0530 Subject: [PATCH] chore: refactor billing service v2 (#16805) --- .../billing/controllers/billing.controller.ts | 31 ++++++------------- .../billing/services/billing.service.ts | 10 ++++-- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/apps/api/v2/src/modules/billing/controllers/billing.controller.ts b/apps/api/v2/src/modules/billing/controllers/billing.controller.ts index 7e899b967d..40d3e287b3 100644 --- a/apps/api/v2/src/modules/billing/controllers/billing.controller.ts +++ b/apps/api/v2/src/modules/billing/controllers/billing.controller.ts @@ -68,20 +68,14 @@ export class BillingController { @Param("teamId") teamId: number, @Body() input: SubscribeToPlanInput ): Promise> { - const { action, url } = await this.billingService.createSubscriptionForTeam(teamId, input.plan); - - if (action === "redirect") { - return { - status: "success", - data: { - action: "redirect", - url, - }, - }; - } + const customerId = await this.billingService.createTeamBilling(teamId); + const url = await this.billingService.redirectToSubscribeCheckout(teamId, input.plan, customerId); return { status: "success", + data: { + url, + }, }; } @@ -92,20 +86,13 @@ export class BillingController { @Param("teamId") teamId: number, @Body() input: SubscribeToPlanInput ): Promise> { - const { action, url } = await this.billingService.updateSubscriptionForTeam(teamId, input.plan); - - if (action === "redirect") { - return { - status: "success", - data: { - action: "redirect", - url, - }, - }; - } + const url = await this.billingService.updateSubscriptionForTeam(teamId, input.plan); return { status: "success", + data: { + url, + }, }; } diff --git a/apps/api/v2/src/modules/billing/services/billing.service.ts b/apps/api/v2/src/modules/billing/services/billing.service.ts index cb6bd4cbc6..bf25bc0c9e 100644 --- a/apps/api/v2/src/modules/billing/services/billing.service.ts +++ b/apps/api/v2/src/modules/billing/services/billing.service.ts @@ -47,7 +47,7 @@ export class BillingService implements OnModuleDestroy { } } - async createSubscriptionForTeam(teamId: number, plan: PlatformPlan) { + async createTeamBilling(teamId: number) { const teamWithBilling = await this.teamsRepository.findByIdIncludeBilling(teamId); let customerId = teamWithBilling?.platformBilling?.customerId; @@ -60,6 +60,10 @@ export class BillingService implements OnModuleDestroy { }); } + return customerId; + } + + async redirectToSubscribeCheckout(teamId: number, plan: PlatformPlan, customerId?: string) { const { url } = await this.stripeService.stripe.checkout.sessions.create({ customer: customerId, line_items: [ @@ -90,7 +94,7 @@ export class BillingService implements OnModuleDestroy { if (!url) throw new InternalServerErrorException("Failed to create Stripe session."); - return { action: "redirect", url }; + return url; } async updateSubscriptionForTeam(teamId: number, plan: PlatformPlan) { @@ -111,7 +115,7 @@ export class BillingService implements OnModuleDestroy { if (!url) throw new InternalServerErrorException("Failed to create Stripe session."); - return { action: "redirect", url }; + return url; } async setSubscriptionForTeam(teamId: number, subscriptionId: string, plan: PlatformPlan) {