finish payment strategies

This commit is contained in:
2024-11-03 22:09:59 +07:00
parent 6b7430c5cf
commit 0f68b51d75
3 changed files with 157 additions and 58 deletions

View File

@@ -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) {