Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Add billing portal session link
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 18, 2020
1 parent 76181f1 commit 018837c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/modules/stripe/stripe-billing.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@ export class StripeBillingController {
): Promise<Stripe.DeletedCustomer> {
return this.stripeService.deleteCustomer(groupId);
}

@Post()
@AuditLog('billing-portal')
@Scopes('group-{groupId}:write-billing')
async getSession(
@Param('groupId', ParseIntPipe) groupId: number,
): Promise<Stripe.Response<Stripe.BillingPortal.Session>> {
return this.stripeService.getBillingPortalLink(groupId);
}
}
20 changes: 16 additions & 4 deletions src/modules/stripe/stripe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ export class StripeService {
return result;
}

async getBillingPortalLink(
groupId: number,
): Promise<Stripe.Response<Stripe.BillingPortal.Session>> {
const stripeId = await this.stripeId(groupId);
return this.stripe.billingPortal.sessions.create({ customer: stripeId });
}

async getInvoices(
groupId: number,
params: {
Expand Down Expand Up @@ -165,7 +172,7 @@ export class StripeService {
async createSession(
groupId: number,
mode: Stripe.Checkout.SessionCreateParams.Mode,
price?: string,
planId?: string,
): Promise<Stripe.Checkout.Session> {
const stripeId = await this.stripeId(groupId);
const data: Stripe.Checkout.SessionCreateParams = {
Expand All @@ -176,12 +183,17 @@ export class StripeService {
>('payments.paymentMethodTypes') ?? ['card'],
success_url: `${this.configService.get<string>(
'frontendUrl',
)}/groups/${groupId}/subscription`,
)}/groups/${groupId}/billing/${
mode === 'setup' ? 'sources' : 'subscription'
}`,
cancel_url: `${this.configService.get<string>(
'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;
}
Expand Down

0 comments on commit 018837c

Please sign in to comment.