Skip to content

Commit 68d5b87

Browse files
authored
Merge pull request #42 from teacoder-team/dev
feat(payment): add cryptocurrency support
2 parents 931ac98 + a5930b2 commit 68d5b87

4 files changed

Lines changed: 32 additions & 32 deletions

File tree

src/api/payment/payment.service.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { InitPaymentRequest } from './dto'
2222
@Injectable()
2323
export class PaymentService {
2424
private readonly HOSTS_APP: string
25+
private readonly HOSTS_REST: string
26+
2527
private readonly SUBSCRIPTION_PRICE = 349
2628

2729
public constructor(
@@ -31,6 +33,7 @@ export class PaymentService {
3133
private readonly heleketService: HeleketService
3234
) {
3335
this.HOSTS_APP = this.configService.getOrThrow<string>('HOSTS_APP')
36+
this.HOSTS_REST = this.configService.getOrThrow<string>('HOSTS_REST')
3437
}
3538

3639
public async create(dto: InitPaymentRequest, user: User) {
@@ -80,19 +83,15 @@ export class PaymentService {
8083
)
8184
break
8285
case PaymentMethod.CRYPTO:
83-
// providerResponse = await this.heleketService.createPayment({
84-
// // amount: String(this.SUBSCRIPTION_PRICE),
85-
// amount: '40',
86-
// currency: 'RUB',
87-
// order_id: payment.id,
88-
// url_return: 'http://localhost:14701/premium',
89-
// url_success: 'http://localhost:14701/payment/success',
90-
// url_callback:
91-
// 'https://72263b5f4671.ngrok-free.app/webhook/crypto'
92-
// })
93-
throw new BadRequestException(
94-
'This payment method is not available yet'
95-
)
86+
providerResponse = await this.heleketService.createPayment({
87+
amount: String(this.SUBSCRIPTION_PRICE),
88+
currency: 'RUB',
89+
order_id: payment.id,
90+
url_return: `${this.HOSTS_APP}/payment/success`,
91+
url_success: `${this.HOSTS_APP}/premium`,
92+
url_callback: `${this.HOSTS_REST}/webhook/crypto`
93+
})
94+
break
9695
default:
9796
throw new BadRequestException('Unsupported payment provider')
9897
}

src/api/payment/scheduler/scheduler.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable, Logger } from '@nestjs/common'
22
import { Cron, CronExpression } from '@nestjs/schedule'
3-
import { PaymentStatus } from '@prisma/generated'
3+
import { PaymentMethod, PaymentStatus } from '@prisma/generated'
44
import { CurrencyEnum, YookassaService } from 'nestjs-yookassa'
55
import { VatCodesEnum } from 'nestjs-yookassa/dist/interfaces/receipt-details.interface'
66

@@ -65,7 +65,10 @@ export class SchedulerService {
6565
const lastSuccess = await this.prismaService.payment.findFirst({
6666
where: {
6767
userId: user.id,
68-
status: PaymentStatus.SUCCESS
68+
status: PaymentStatus.SUCCESS,
69+
method: {
70+
not: PaymentMethod.CRYPTO
71+
}
6972
},
7073
orderBy: {
7174
createdAt: 'desc'

src/api/payment/webhook/webhook.controller.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import {
88
} from '@nestjs/common'
99
import { ApiOkResponse, ApiOperation } from '@nestjs/swagger'
1010

11-
import { ClientIp } from '@/common/decorators'
12-
1311
import { WebhookService } from './webhook.service'
1412

1513
@Controller('webhook')
@@ -29,20 +27,20 @@ export class WebhookController {
2927
@Post('yookassa')
3028
@HttpCode(HttpStatus.OK)
3129
public async yookassaWebhook(@Body() payload: any, @Ip() ip: string) {
32-
console.log('YOOKASSA WEBHOOK IP: ', ip)
33-
3430
this.webhookService.verifyWebhook(ip)
3531

3632
await this.webhookService.handleYookassa(payload)
3733

3834
return { ok: true }
3935
}
4036

41-
// @Post('crypto')
42-
// @HttpCode(HttpStatus.OK)
43-
// public async cryptoWebhook(@Body() payload: any) {
44-
// await this.webhookService.handleCrypto(payload)
37+
@Post('crypto')
38+
@HttpCode(HttpStatus.OK)
39+
public async cryptoWebhook(@Body() payload: any) {
40+
console.log(payload)
41+
42+
await this.webhookService.handleCrypto(payload)
4543

46-
// return { ok: true }
47-
// }
44+
return { ok: true }
45+
}
4846
}

src/api/payment/webhook/webhook.service.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ export class WebhookService {
4444
}
4545

4646
public async handleYookassa(payload: any) {
47-
console.log('YOOKASSA WEBHOOK: ', JSON.stringify(payload))
48-
4947
if (payload.event === 'payment.waiting_for_capture') {
5048
return await this.yookassaService.capturePayment(payload.object.id)
5149
} else if (payload.event === 'payment.succeeded') {
@@ -120,6 +118,13 @@ export class WebhookService {
120118
}
121119
})
122120

121+
if (provider === 'yookassa') {
122+
await this.savePaymentMethod(
123+
payment.user.id,
124+
paymentData.payment_method
125+
)
126+
}
127+
123128
const now = new Date()
124129

125130
let expiresAt = payment.user.subscription?.expiresAt ?? now
@@ -147,11 +152,6 @@ export class WebhookService {
147152
}
148153
}
149154
})
150-
151-
await this.savePaymentMethod(
152-
payment.user.id,
153-
paymentData.payment_method
154-
)
155155
}
156156

157157
private async savePaymentMethod(userId: string, metadata: any) {

0 commit comments

Comments
 (0)