finish payment strategies
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
||||
} from '@nestjs/common'
|
||||
import { PayosService } from './payos.service'
|
||||
import { ApiTags, ApiOperation } from '@nestjs/swagger'
|
||||
import { WebhookType } from '@payos/node/lib/type'
|
||||
|
||||
@ApiTags('Payos')
|
||||
@Controller('payos')
|
||||
@@ -19,11 +20,8 @@ export class PayosController {
|
||||
// webhook
|
||||
@Post('webhook')
|
||||
@ApiOperation({ summary: 'Webhook for Payos' })
|
||||
async webhook(
|
||||
@Body() body: any,
|
||||
@Headers('x-payos-signature') signature: string,
|
||||
) {
|
||||
return this.payosService.webhook(body, signature)
|
||||
async webhook(@Body() body: WebhookType) {
|
||||
return this.payosService.webhook(body)
|
||||
}
|
||||
|
||||
// ping webhook
|
||||
|
||||
@@ -5,7 +5,12 @@ import PayOS from '@payos/node'
|
||||
import type {
|
||||
CheckoutRequestType,
|
||||
CheckoutResponseDataType,
|
||||
WebhookType,
|
||||
WebhookDataType,
|
||||
CancelPaymentLinkRequestType,
|
||||
DataType,
|
||||
} from '@payos/node/lib/type'
|
||||
import { OrderStatus, PaymentStatus, ScheduleStatus } from '@prisma/client'
|
||||
export type CreatePaymentBody = CheckoutRequestType
|
||||
export type CreatePaymentResponse = CheckoutResponseDataType
|
||||
@Injectable()
|
||||
@@ -19,9 +24,51 @@ export class PayosService {
|
||||
return 'pong'
|
||||
}
|
||||
|
||||
async webhook(body: any, signature: string) {
|
||||
Logger.log('Webhook received', body)
|
||||
return body
|
||||
async webhook(data: WebhookType) {
|
||||
Logger.log(`Webhook received: ${JSON.stringify(data)}`)
|
||||
// verify checksum
|
||||
const paymentData = this.payos.verifyPaymentWebhookData(data)
|
||||
if (!paymentData) {
|
||||
Logger.error(`Invalid checksum: ${JSON.stringify(data)}`)
|
||||
throw new Error('Invalid checksum')
|
||||
}
|
||||
const paymentStatus =
|
||||
paymentData.code === '00' ? PaymentStatus.PAID : PaymentStatus.CANCELLED
|
||||
// update payment status
|
||||
const payment = await this.prisma.payment.update({
|
||||
where: { paymentCode: paymentData.paymentLinkId },
|
||||
data: {
|
||||
status: paymentStatus,
|
||||
},
|
||||
})
|
||||
const orderStatus =
|
||||
paymentStatus === PaymentStatus.PAID
|
||||
? OrderStatus.PAID
|
||||
: OrderStatus.FAILED
|
||||
// update order status
|
||||
await this.prisma.order.update({
|
||||
where: { id: payment.orderId },
|
||||
data: {
|
||||
status: orderStatus,
|
||||
},
|
||||
})
|
||||
const order = await this.prisma.order.findUnique({
|
||||
where: { id: payment.orderId },
|
||||
})
|
||||
const schedule = await this.prisma.schedule.findUnique({
|
||||
where: { id: order?.scheduleId },
|
||||
})
|
||||
// update schedule order id
|
||||
await this.prisma.schedule.update({
|
||||
where: { id: schedule?.id },
|
||||
data: {
|
||||
orderId: order?.id,
|
||||
status: ScheduleStatus.IN_PROGRESS,
|
||||
},
|
||||
})
|
||||
return {
|
||||
message: 'Payment received',
|
||||
}
|
||||
}
|
||||
|
||||
async createPaymentURL(body: any) {
|
||||
@@ -36,8 +83,11 @@ export class PayosService {
|
||||
return await this.payos.getPaymentLinkInformation(orderId)
|
||||
}
|
||||
|
||||
async cancelPaymentURL(body: any) {
|
||||
return body
|
||||
async cancelPaymentURL(
|
||||
orderId: string | number,
|
||||
cancellationReason?: string,
|
||||
) {
|
||||
return await this.payos.cancelPaymentLink(orderId, cancellationReason)
|
||||
}
|
||||
|
||||
async refundPayment(body: any) {
|
||||
|
||||
Reference in New Issue
Block a user