Skip to content

Scheduling infrastructure for absolutely everyone.

chore: refactor billing service v2 (#16805)

Rajiv Sahal26a1c749f7c4
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 7e899b967d7..40d3e287b32 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<ApiResponse<SubscribeTeamToBillingResponseDto | undefined>> {
- 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<ApiResponse<SubscribeTeamToBillingResponseDto | undefined>> {
- 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 cb6bd4cbc63..bf25bc0c9e4 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) {