universo-platform-3d
209 строк · 6.3 Кб
1import {2Controller,3Get,4Post,5Body,6Param,7Delete,8UsePipes,9ValidationPipe,10RawBodyRequest,11Req,12BadRequestException,13Patch
14} from '@nestjs/common'15import { StripeService } from './stripe.service'16import { Stripe } from 'stripe'17import { FirebaseTokenAuthGuard } from '../auth/auth.guard'18import { ApiParam } from '@nestjs/swagger'19import { AddBank, AddCard, CardToken } from './dto/token.dto'20import { UserToken } from '../auth/get-user.decorator'21import { UserId } from '../util/mongo-object-id-helpers'22import { PaymentIntentDto } from './dto/paymentIntent.dto'23import { TransfersDto } from './dto/transfers.dto'24import { ProductDto, SubscriptionDto } from './dto/subscription.dto'25import { PublicFirebaseAuthNotRequired } from '../auth/public.decorator'26@Controller('stripe')27@FirebaseTokenAuthGuard()28@UsePipes(new ValidationPipe({ whitelist: true }))29export class StripeController {30constructor(private readonly stripeService: StripeService) {}31
32/**33* 2023-07-24 11:12:23 Note: I'm commenting these out because it was old code and I don't think we need to expose all these. The implementation also wasn't secure with allowing an userId to be specified.
34*/
35@Post('/setup-intent')36@FirebaseTokenAuthGuard()37public async setupIntent(38@UserToken('user_id') userId: UserId,39@Body() data: { payment_method: string }40) {41return await this.stripeService.setupIntent(userId, data)42}43
44@Post('/customer')45@FirebaseTokenAuthGuard()46public async createCustomerAccount(@UserToken('user_id') userId: UserId) {47return await this.stripeService.createCustomerAccount(userId)48}49
50@Post('/connect')51@FirebaseTokenAuthGuard()52public async createConnectAccount(@UserToken('user_id') userId: UserId) {53return await this.stripeService.createConnectAccount(userId)54}55
56@Delete('/connect')57@FirebaseTokenAuthGuard()58public async deleteConnectAccount(@UserToken('user_id') userId: UserId) {59return await this.stripeService.deleteConnectAccount(userId)60}61
62@Post('/card')63@FirebaseTokenAuthGuard()64public async createCard(65@Body() { token }: AddCard,66@UserToken('user_id') userId: UserId67): Promise<Stripe.CustomerSource[]> {68return await this.stripeService.createCard(userId, token)69}70
71@Get('/cards')72@FirebaseTokenAuthGuard()73public async getCardsList(@UserToken('user_id') userId: UserId) {74return await this.stripeService.getCardsList(userId)75}76
77@Get('/account-info')78@FirebaseTokenAuthGuard()79public async getStripeAccountInfo(@UserToken('user_id') userId: UserId) {80return await this.stripeService.getStripeAccountInfo(userId)81}82
83@Post('/bank-account')84@FirebaseTokenAuthGuard()85public async addBankAccount(86@Body() tokenData: AddBank,87@UserToken('user_id') userId: UserId88) {89return await this.stripeService.addBankToken(userId, tokenData)90}91
92@Delete('/card/:idCard')93@FirebaseTokenAuthGuard()94@ApiParam({ name: 'idCard', type: 'string', required: true })95public async deleteCard(96@UserToken('user_id') userId: UserId,97@Param('idCard') cardId: string98) {99return await this.stripeService.deleteCard(userId, cardId)100}101
102@Post('/card/:paymentMethodId')103@FirebaseTokenAuthGuard()104@ApiParam({ name: 'paymentMethodId', type: 'string', required: true })105public async setDefaultPaymentMethod(106@UserToken('user_id') userId: UserId,107@Param('paymentMethodId') paymentMethodId: string108) {109return await this.stripeService.setDefaultPaymentMethod(110userId,111paymentMethodId
112)113}114
115@Post('/payment-intent')116@FirebaseTokenAuthGuard()117public async createPaymentIntent(118@UserToken('user_id') userId: UserId,119@Body() data: PaymentIntentDto120) {121return await this.stripeService.createPaymentIntent(userId, data)122}123
124@Get('/payment-methods')125@FirebaseTokenAuthGuard()126public async getPaymentMethods(@UserToken('user_id') userId: UserId) {127return await this.stripeService.getPaymentMethods(userId)128}129
130@Post('/transfers')131@FirebaseTokenAuthGuard()132@ApiParam({ name: 'destinationUserId', type: 'string', required: true })133public async transfersAmount(@Body() data: TransfersDto) {134return await this.stripeService.transfersAmount(data)135}136
137// Create product for Subscription138
139@Post('/product')140@FirebaseTokenAuthGuard()141public async createProduct(142@UserToken('user_id') userId: UserId,143@Body() data: ProductDto144) {145return await this.stripeService.createProduct(userId, data)146}147
148// Get all products.149
150@Get('/products')151@FirebaseTokenAuthGuard()152public async getAllProductsWithPrice(@UserToken('user_id') userId: UserId) {153return await this.stripeService.getAllProductsWithPrice(userId)154}155
156//Creating Subscription.157
158@Post('/subscription')159@FirebaseTokenAuthGuard()160public async createSubscription(161@UserToken('user_id') userId: UserId,162@Body() data: SubscriptionDto163) {164return await this.stripeService.createSubscription(userId, data)165}166
167@Patch('/subscription/pause')168@FirebaseTokenAuthGuard()169public async pauseSubscription(@UserToken('user_id') userId: UserId) {170return await this.stripeService.pauseSubscription(userId)171}172
173@Patch('/subscription/resume')174@FirebaseTokenAuthGuard()175public async resumeSubscription(@UserToken('user_id') userId: UserId) {176return await this.stripeService.resumeSubscription(userId)177}178
179@Delete('/subscription')180@FirebaseTokenAuthGuard()181public async deleteSubscription(@UserToken('user_id') userId: UserId) {182return await this.stripeService.deleteSubscription(userId)183}184
185@Get('/dashboard-link')186@FirebaseTokenAuthGuard()187public async createDashboardLink(@UserToken('user_id') userId: UserId) {188return await this.stripeService.createDashboardLink(userId)189}190
191@Get('/customer-portal-link')192@FirebaseTokenAuthGuard()193public async createCustomerPortalLink(@UserToken('user_id') userId: UserId) {194return await this.stripeService.createCustomerPortalLink(userId)195}196
197@Post('/webhook')198@PublicFirebaseAuthNotRequired()199public async handleStripeWebhook(200@Req() req: RawBodyRequest<Request>201): Promise<any> {202if (!req.rawBody) {203throw new BadRequestException('Invalid payload')204}205const raw = req.rawBody.toString('utf8')206const json = JSON.parse(raw)207return await this.stripeService.handleStripeWebhook(json)208}209}
210