From 018837c4dbf63ddf647455674c46676d8f96d80c Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Wed, 18 Nov 2020 14:33:40 +0530 Subject: [PATCH] :recycle: Add billing portal session link --- .../stripe/stripe-billing.controller.ts | 9 +++++++++ src/modules/stripe/stripe.service.ts | 20 +++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/modules/stripe/stripe-billing.controller.ts b/src/modules/stripe/stripe-billing.controller.ts index 7299a04f5..d1d0f3f1f 100644 --- a/src/modules/stripe/stripe-billing.controller.ts +++ b/src/modules/stripe/stripe-billing.controller.ts @@ -69,4 +69,13 @@ export class StripeBillingController { ): Promise { return this.stripeService.deleteCustomer(groupId); } + + @Post() + @AuditLog('billing-portal') + @Scopes('group-{groupId}:write-billing') + async getSession( + @Param('groupId', ParseIntPipe) groupId: number, + ): Promise> { + return this.stripeService.getBillingPortalLink(groupId); + } } diff --git a/src/modules/stripe/stripe.service.ts b/src/modules/stripe/stripe.service.ts index 1e0eea35d..a768b25d2 100644 --- a/src/modules/stripe/stripe.service.ts +++ b/src/modules/stripe/stripe.service.ts @@ -77,6 +77,13 @@ export class StripeService { return result; } + async getBillingPortalLink( + groupId: number, + ): Promise> { + const stripeId = await this.stripeId(groupId); + return this.stripe.billingPortal.sessions.create({ customer: stripeId }); + } + async getInvoices( groupId: number, params: { @@ -165,7 +172,7 @@ export class StripeService { async createSession( groupId: number, mode: Stripe.Checkout.SessionCreateParams.Mode, - price?: string, + planId?: string, ): Promise { const stripeId = await this.stripeId(groupId); const data: Stripe.Checkout.SessionCreateParams = { @@ -176,12 +183,17 @@ export class StripeService { >('payments.paymentMethodTypes') ?? ['card'], success_url: `${this.configService.get( 'frontendUrl', - )}/groups/${groupId}/subscription`, + )}/groups/${groupId}/billing/${ + mode === 'setup' ? 'sources' : 'subscription' + }`, cancel_url: `${this.configService.get( 'frontendUrl', - )}/groups/${groupId}/subscription`, + )}/groups/${groupId}/billing/${ + mode === 'setup' ? 'sources' : 'subscription' + }`, }; - if (mode === 'subscription') data.line_items = [{ quantity: 1, price }]; + if (mode === 'subscription') + data.line_items = [{ quantity: 1, price: planId }]; const result = await this.stripe.checkout.sessions.create(data); return result; }