File size: 1,510 Bytes
3722089 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import {
Controller,
HttpException,
Post,
RawBodyRequest,
Req,
} from '@nestjs/common';
import { StripeService } from '@gitroom/nestjs-libraries/services/stripe.service';
import { ApiTags } from '@nestjs/swagger';
@ApiTags('Stripe')
@Controller('/stripe')
export class StripeController {
constructor(
private readonly _stripeService: StripeService,
) {}
@Post('/')
stripe(@Req() req: RawBodyRequest<Request>) {
const event = this._stripeService.validateRequest(
req.rawBody,
// @ts-ignore
req.headers['stripe-signature'],
process.env.STRIPE_SIGNING_KEY
);
// Maybe it comes from another stripe webhook
if (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
event?.data?.object?.metadata?.service !== 'gitroom' &&
event.type !== 'invoice.payment_succeeded'
) {
return { ok: true };
}
try {
switch (event.type) {
case 'invoice.payment_succeeded':
return this._stripeService.paymentSucceeded(event);
case 'customer.subscription.created':
return this._stripeService.createSubscription(event);
case 'customer.subscription.updated':
return this._stripeService.updateSubscription(event);
case 'customer.subscription.deleted':
return this._stripeService.deleteSubscription(event);
default:
return { ok: true };
}
} catch (e) {
throw new HttpException(e, 500);
}
}
}
|