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

Commit

Permalink
♻️ Use config in Stripe module, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jan 9, 2021
1 parent 0cbe65c commit 62ac626
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/modules/stripe/stripe-billing.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { StripeService } from './stripe.service';
export class StripeBillingController {
constructor(private stripeService: StripeService) {}

/** Create a billing account for a group */
@Post()
@AuditLog('create-billing')
@Scopes('group-{groupId}:write-billing')
Expand All @@ -33,6 +34,7 @@ export class StripeBillingController {
return this.stripeService.createCustomer(groupId, data);
}

/** Read billing for a group */
@Get()
@Scopes('group-{groupId}:read-billing')
async getBillingAccount(
Expand All @@ -41,6 +43,7 @@ export class StripeBillingController {
return this.stripeService.getCustomer(groupId);
}

/** Update billing for a group */
@Patch()
@AuditLog('update-billing')
@Scopes('group-{groupId}:write-billing')
Expand All @@ -51,6 +54,7 @@ export class StripeBillingController {
return this.stripeService.updateCustomer(groupId, data);
}

/** Replace billing for a group */
@Put()
@AuditLog('update-billing')
@Scopes('group-{groupId}:write-billing')
Expand All @@ -61,6 +65,7 @@ export class StripeBillingController {
return this.stripeService.updateCustomer(groupId, data);
}

/** Delete billing for a group */
@Delete()
@AuditLog('delete-billing')
@Scopes('group-{groupId}:delete-billing')
Expand All @@ -70,6 +75,7 @@ export class StripeBillingController {
return this.stripeService.deleteCustomer(groupId);
}

/** Get the billing portal link for a group */
@Get('link')
@AuditLog('billing-portal')
@Scopes('group-{groupId}:write-billing')
Expand Down
2 changes: 2 additions & 0 deletions src/modules/stripe/stripe-invoices.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { StripeService } from './stripe.service';
export class StripeInvoicesController {
constructor(private stripeService: StripeService) {}

/** Read invoices for a group */
@Get()
@Scopes('group-{groupId}:read-invoice-*')
async getAll(
Expand All @@ -19,6 +20,7 @@ export class StripeInvoicesController {
return this.stripeService.getInvoices(groupId, { take, cursor });
}

/** Read an invoice for a group */
@Get(':id')
@Scopes('group-{groupId}:read-invoice-{id}')
async get(
Expand Down
9 changes: 7 additions & 2 deletions src/modules/stripe/stripe-sources.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { StripeService } from './stripe.service';
export class StripeSourcesController {
constructor(private stripeService: StripeService) {}

/** Create a source for a group */
@Post()
@AuditLog('write-source')
@Scopes('group-{groupId}:write-source-*')
Expand All @@ -27,6 +28,7 @@ export class StripeSourcesController {
return this.stripeService.createSession(groupId, 'setup');
}

/** Read sources for a group */
@Get()
@Scopes('group-{groupId}:read-source-*')
async getAll(
Expand All @@ -37,6 +39,7 @@ export class StripeSourcesController {
return this.stripeService.getSources(groupId, { take, cursor });
}

/** Read a source for a group */
@Get(':id')
@Scopes('group-{groupId}:read-source-{id}')
async get(
Expand All @@ -46,13 +49,15 @@ export class StripeSourcesController {
return this.stripeService.getSource(groupId, id);
}

/** Delete a source for a group */
@Delete(':id')
@AuditLog('delete-source')
@Scopes('group-{groupId}:delete-source-{id}')
async remove(
@Param('groupId', ParseIntPipe) groupId: number,
@Param('id') id: string,
): Promise<void> {
return this.stripeService.deleteSource(groupId, id);
): Promise<{ success: true }> {
await this.stripeService.deleteSource(groupId, id);
return { success: true };
}
}
5 changes: 5 additions & 0 deletions src/modules/stripe/stripe-subscription.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { StripeService } from './stripe.service';
export class StripeSubscriptionController {
constructor(private stripeService: StripeService) {}

/** Create a subscription for a group */
@Post(':plan')
@AuditLog('create-subscription')
@Scopes('group-{groupId}:write-subscription-*')
Expand All @@ -28,6 +29,7 @@ export class StripeSubscriptionController {
return this.stripeService.createSession(groupId, 'subscription', plan);
}

/** Get subscriptions for a group */
@Get()
@Scopes('group-{groupId}:read-subscription-*')
async getAll(
Expand All @@ -38,6 +40,7 @@ export class StripeSubscriptionController {
return this.stripeService.getSubscriptions(groupId, { take, cursor });
}

/** Get a subscription for a group */
@Get(':id')
@Scopes('group-{groupId}:read-subscription-{id}')
async get(
Expand All @@ -47,6 +50,7 @@ export class StripeSubscriptionController {
return this.stripeService.getSubscription(groupId, id);
}

/** Cancel a subscription for a group */
@Delete(':id')
@AuditLog('delete-subscription')
@Scopes('group-{groupId}:delete-subscription-{id}')
Expand All @@ -57,6 +61,7 @@ export class StripeSubscriptionController {
return this.stripeService.cancelSubscription(groupId, id);
}

/** Get subscription plans for a group */
@Get('plans')
@Scopes('group-{groupId}:write-subscription-*')
async getPlans(
Expand Down
1 change: 1 addition & 0 deletions src/modules/stripe/stripe-webhook.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { StripeService } from './stripe.service';
export class StripeWebhookController {
constructor(private stripeService: StripeService) {}

/** Handle a Stripe webhook */
@Post()
async handleWebhook(
@Headers('stripe-signature') signature: string,
Expand Down
29 changes: 17 additions & 12 deletions src/modules/stripe/stripe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import Stripe from 'stripe';
import { Configuration } from '../../config/configuration.interface';
import {
BILLING_ACCOUNT_CREATED_CONFLICT,
BILLING_NOT_FOUND,
Expand All @@ -23,18 +22,16 @@ import { PrismaService } from '../../providers/prisma/prisma.service';
export class StripeService {
stripe: Stripe;
logger = new Logger(StripeService.name);
private metaConfig = this.configService.get<Configuration['meta']>('meta');
private paymentsConfig = this.configService.get<Configuration['payments']>(
'payments',
);

constructor(
private configService: ConfigService,
private prisma: PrismaService,
) {
this.stripe = new Stripe(this.paymentsConfig.stripeApiKey, {
const stripeApiKey = this.configService.get<string>(
'payments.stripeApiKey',
);
this.stripe = new Stripe(stripeApiKey, {
apiVersion: '2020-08-27',
typescript: true,
});
}

Expand Down Expand Up @@ -86,7 +83,9 @@ export class StripeService {
const stripeId = await this.stripeId(groupId);
return this.stripe.billingPortal.sessions.create({
customer: stripeId,
return_url: `${this.metaConfig.frontendUrl}/groups/${groupId}`,
return_url: `${this.configService.get<string>(
'frontendUrl',
)}/groups/${groupId}`,
});
}

Expand Down Expand Up @@ -184,9 +183,15 @@ export class StripeService {
const data: Stripe.Checkout.SessionCreateParams = {
customer: stripeId,
mode,
payment_method_types: this.paymentsConfig.paymentMethodTypes ?? ['card'],
success_url: `${this.metaConfig.frontendUrl}/groups/${groupId}`,
cancel_url: `${this.metaConfig.frontendUrl}/groups/${groupId}`,
payment_method_types: this.configService.get<
Array<Stripe.Checkout.SessionCreateParams.PaymentMethodType>
>('payments.paymentMethodTypes') ?? ['card'],
success_url: `${this.configService.get<string>(
'frontendUrl',
)}/groups/${groupId}`,
cancel_url: `${this.configService.get<string>(
'frontendUrl',
)}/groups/${groupId}`,
};
if (mode === 'subscription')
data.line_items = [{ quantity: 1, price: planId }];
Expand Down Expand Up @@ -234,7 +239,7 @@ export class StripeService {
const event = this.stripe.webhooks.constructEvent(
payload,
signature,
this.paymentsConfig.stripeEndpointSecret,
this.configService.get<string>('payments.stripeEndpointSecret') ?? '',
);
switch (event.type) {
default:
Expand Down

0 comments on commit 62ac626

Please sign in to comment.