universo-platform-3d

Форк
0
209 строк · 6.3 Кб
1
import {
2
  Controller,
3
  Get,
4
  Post,
5
  Body,
6
  Param,
7
  Delete,
8
  UsePipes,
9
  ValidationPipe,
10
  RawBodyRequest,
11
  Req,
12
  BadRequestException,
13
  Patch
14
} from '@nestjs/common'
15
import { StripeService } from './stripe.service'
16
import { Stripe } from 'stripe'
17
import { FirebaseTokenAuthGuard } from '../auth/auth.guard'
18
import { ApiParam } from '@nestjs/swagger'
19
import { AddBank, AddCard, CardToken } from './dto/token.dto'
20
import { UserToken } from '../auth/get-user.decorator'
21
import { UserId } from '../util/mongo-object-id-helpers'
22
import { PaymentIntentDto } from './dto/paymentIntent.dto'
23
import { TransfersDto } from './dto/transfers.dto'
24
import { ProductDto, SubscriptionDto } from './dto/subscription.dto'
25
import { PublicFirebaseAuthNotRequired } from '../auth/public.decorator'
26
@Controller('stripe')
27
@FirebaseTokenAuthGuard()
28
@UsePipes(new ValidationPipe({ whitelist: true }))
29
export class StripeController {
30
  constructor(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()
37
  public async setupIntent(
38
    @UserToken('user_id') userId: UserId,
39
    @Body() data: { payment_method: string }
40
  ) {
41
    return await this.stripeService.setupIntent(userId, data)
42
  }
43

44
  @Post('/customer')
45
  @FirebaseTokenAuthGuard()
46
  public async createCustomerAccount(@UserToken('user_id') userId: UserId) {
47
    return await this.stripeService.createCustomerAccount(userId)
48
  }
49

50
  @Post('/connect')
51
  @FirebaseTokenAuthGuard()
52
  public async createConnectAccount(@UserToken('user_id') userId: UserId) {
53
    return await this.stripeService.createConnectAccount(userId)
54
  }
55

56
  @Delete('/connect')
57
  @FirebaseTokenAuthGuard()
58
  public async deleteConnectAccount(@UserToken('user_id') userId: UserId) {
59
    return await this.stripeService.deleteConnectAccount(userId)
60
  }
61

62
  @Post('/card')
63
  @FirebaseTokenAuthGuard()
64
  public async createCard(
65
    @Body() { token }: AddCard,
66
    @UserToken('user_id') userId: UserId
67
  ): Promise<Stripe.CustomerSource[]> {
68
    return await this.stripeService.createCard(userId, token)
69
  }
70

71
  @Get('/cards')
72
  @FirebaseTokenAuthGuard()
73
  public async getCardsList(@UserToken('user_id') userId: UserId) {
74
    return await this.stripeService.getCardsList(userId)
75
  }
76

77
  @Get('/account-info')
78
  @FirebaseTokenAuthGuard()
79
  public async getStripeAccountInfo(@UserToken('user_id') userId: UserId) {
80
    return await this.stripeService.getStripeAccountInfo(userId)
81
  }
82

83
  @Post('/bank-account')
84
  @FirebaseTokenAuthGuard()
85
  public async addBankAccount(
86
    @Body() tokenData: AddBank,
87
    @UserToken('user_id') userId: UserId
88
  ) {
89
    return await this.stripeService.addBankToken(userId, tokenData)
90
  }
91

92
  @Delete('/card/:idCard')
93
  @FirebaseTokenAuthGuard()
94
  @ApiParam({ name: 'idCard', type: 'string', required: true })
95
  public async deleteCard(
96
    @UserToken('user_id') userId: UserId,
97
    @Param('idCard') cardId: string
98
  ) {
99
    return await this.stripeService.deleteCard(userId, cardId)
100
  }
101

102
  @Post('/card/:paymentMethodId')
103
  @FirebaseTokenAuthGuard()
104
  @ApiParam({ name: 'paymentMethodId', type: 'string', required: true })
105
  public async setDefaultPaymentMethod(
106
    @UserToken('user_id') userId: UserId,
107
    @Param('paymentMethodId') paymentMethodId: string
108
  ) {
109
    return await this.stripeService.setDefaultPaymentMethod(
110
      userId,
111
      paymentMethodId
112
    )
113
  }
114

115
  @Post('/payment-intent')
116
  @FirebaseTokenAuthGuard()
117
  public async createPaymentIntent(
118
    @UserToken('user_id') userId: UserId,
119
    @Body() data: PaymentIntentDto
120
  ) {
121
    return await this.stripeService.createPaymentIntent(userId, data)
122
  }
123

124
  @Get('/payment-methods')
125
  @FirebaseTokenAuthGuard()
126
  public async getPaymentMethods(@UserToken('user_id') userId: UserId) {
127
    return await this.stripeService.getPaymentMethods(userId)
128
  }
129

130
  @Post('/transfers')
131
  @FirebaseTokenAuthGuard()
132
  @ApiParam({ name: 'destinationUserId', type: 'string', required: true })
133
  public async transfersAmount(@Body() data: TransfersDto) {
134
    return await this.stripeService.transfersAmount(data)
135
  }
136

137
  // Create product for Subscription
138

139
  @Post('/product')
140
  @FirebaseTokenAuthGuard()
141
  public async createProduct(
142
    @UserToken('user_id') userId: UserId,
143
    @Body() data: ProductDto
144
  ) {
145
    return await this.stripeService.createProduct(userId, data)
146
  }
147

148
  // Get all products.
149

150
  @Get('/products')
151
  @FirebaseTokenAuthGuard()
152
  public async getAllProductsWithPrice(@UserToken('user_id') userId: UserId) {
153
    return await this.stripeService.getAllProductsWithPrice(userId)
154
  }
155

156
  //Creating Subscription.
157

158
  @Post('/subscription')
159
  @FirebaseTokenAuthGuard()
160
  public async createSubscription(
161
    @UserToken('user_id') userId: UserId,
162
    @Body() data: SubscriptionDto
163
  ) {
164
    return await this.stripeService.createSubscription(userId, data)
165
  }
166

167
  @Patch('/subscription/pause')
168
  @FirebaseTokenAuthGuard()
169
  public async pauseSubscription(@UserToken('user_id') userId: UserId) {
170
    return await this.stripeService.pauseSubscription(userId)
171
  }
172

173
  @Patch('/subscription/resume')
174
  @FirebaseTokenAuthGuard()
175
  public async resumeSubscription(@UserToken('user_id') userId: UserId) {
176
    return await this.stripeService.resumeSubscription(userId)
177
  }
178

179
  @Delete('/subscription')
180
  @FirebaseTokenAuthGuard()
181
  public async deleteSubscription(@UserToken('user_id') userId: UserId) {
182
    return await this.stripeService.deleteSubscription(userId)
183
  }
184

185
  @Get('/dashboard-link')
186
  @FirebaseTokenAuthGuard()
187
  public async createDashboardLink(@UserToken('user_id') userId: UserId) {
188
    return await this.stripeService.createDashboardLink(userId)
189
  }
190

191
  @Get('/customer-portal-link')
192
  @FirebaseTokenAuthGuard()
193
  public async createCustomerPortalLink(@UserToken('user_id') userId: UserId) {
194
    return await this.stripeService.createCustomerPortalLink(userId)
195
  }
196

197
  @Post('/webhook')
198
  @PublicFirebaseAuthNotRequired()
199
  public async handleStripeWebhook(
200
    @Req() req: RawBodyRequest<Request>
201
  ): Promise<any> {
202
    if (!req.rawBody) {
203
      throw new BadRequestException('Invalid payload')
204
    }
205
    const raw = req.rawBody.toString('utf8')
206
    const json = JSON.parse(raw)
207
    return await this.stripeService.handleStripeWebhook(json)
208
  }
209
}
210

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.