update transaction
This commit is contained in:
@@ -2,20 +2,8 @@ import { Inject, Injectable, Logger } from '@nestjs/common'
|
||||
|
||||
import { PrismaService } from '../Prisma/prisma.service'
|
||||
import PayOS from '@payos/node'
|
||||
import type {
|
||||
CheckoutRequestType,
|
||||
CheckoutResponseDataType,
|
||||
WebhookType,
|
||||
WebhookDataType,
|
||||
CancelPaymentLinkRequestType,
|
||||
DataType,
|
||||
} from '@payos/node/lib/type'
|
||||
import {
|
||||
ChatRoomType,
|
||||
OrderStatus,
|
||||
PaymentStatus,
|
||||
ScheduleStatus,
|
||||
} from '@prisma/client'
|
||||
import type { CheckoutRequestType, CheckoutResponseDataType, WebhookType, WebhookDataType, CancelPaymentLinkRequestType, DataType } from '@payos/node/lib/type'
|
||||
import { ChatRoomType, OrderStatus, PaymentStatus, ScheduleStatus } from '@prisma/client'
|
||||
export type CreatePaymentBody = CheckoutRequestType
|
||||
export type CreatePaymentResponse = CheckoutResponseDataType
|
||||
@Injectable()
|
||||
@@ -31,74 +19,87 @@ export class PayosService {
|
||||
|
||||
async webhook(data: WebhookType) {
|
||||
Logger.log(`Webhook received: ${JSON.stringify(data)}`)
|
||||
// check if orderCode = 123 mean it's a test payment and auto response success
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Test payment */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
if (data.data.orderCode === 123) {
|
||||
return {
|
||||
message: 'Payment received',
|
||||
}
|
||||
}
|
||||
// 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.findUniqueOrThrow({
|
||||
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: {
|
||||
customerId: order?.userId,
|
||||
orderId: order?.id,
|
||||
status: ScheduleStatus.IN_PROGRESS,
|
||||
},
|
||||
})
|
||||
// get mentor id from managed service
|
||||
const managedService = await this.prisma.managedService.findUniqueOrThrow({
|
||||
where: { id: schedule?.managedServiceId },
|
||||
})
|
||||
const mentorId = managedService.mentorId
|
||||
// get center id from order service
|
||||
const orderService = await this.prisma.service.findUniqueOrThrow({
|
||||
where: { id: order?.serviceId },
|
||||
})
|
||||
const centerId = orderService.centerId
|
||||
// create chatroom for service meeting room
|
||||
await this.prisma.chatRoom.create({
|
||||
data: {
|
||||
type: ChatRoomType.SUPPORT,
|
||||
customerId: order.userId,
|
||||
centerId: centerId,
|
||||
mentorId: mentorId,
|
||||
},
|
||||
})
|
||||
return {
|
||||
message: 'Payment received',
|
||||
const paymentStatus = paymentData.code === '00' ? PaymentStatus.PAID : PaymentStatus.CANCELLED
|
||||
/* ---------------------------- begin transaction --------------------------- */
|
||||
try {
|
||||
await this.prisma.$transaction(async (tx) => {
|
||||
// update payment status
|
||||
const payment = await tx.payment.update({
|
||||
where: { paymentCode: paymentData.paymentLinkId },
|
||||
data: {
|
||||
status: paymentStatus,
|
||||
},
|
||||
})
|
||||
const orderStatus = paymentStatus === PaymentStatus.PAID ? OrderStatus.PAID : OrderStatus.FAILED
|
||||
// update order status
|
||||
await tx.order.update({
|
||||
where: { id: payment.orderId },
|
||||
data: {
|
||||
status: orderStatus,
|
||||
},
|
||||
})
|
||||
const order = await tx.order.findUniqueOrThrow({
|
||||
where: { id: payment.orderId },
|
||||
})
|
||||
const schedule = await tx.schedule.findUnique({
|
||||
where: { id: order?.scheduleId },
|
||||
})
|
||||
// update schedule order id
|
||||
await tx.schedule.update({
|
||||
where: { id: schedule?.id },
|
||||
data: {
|
||||
customerId: order?.userId,
|
||||
orderId: order?.id,
|
||||
status: ScheduleStatus.IN_PROGRESS,
|
||||
},
|
||||
})
|
||||
// get mentor id from managed service
|
||||
const managedService = await tx.managedService.findUniqueOrThrow({
|
||||
where: { id: schedule?.managedServiceId },
|
||||
})
|
||||
const mentorId = managedService.mentorId
|
||||
// get center id from order service
|
||||
const orderService = await tx.service.findUniqueOrThrow({
|
||||
where: { id: order?.serviceId },
|
||||
})
|
||||
const centerId = orderService.centerId
|
||||
// create chatroom for support
|
||||
await tx.chatRoom.create({
|
||||
data: {
|
||||
type: ChatRoomType.SUPPORT,
|
||||
customerId: order.userId,
|
||||
centerId: centerId,
|
||||
mentorId: mentorId,
|
||||
},
|
||||
})
|
||||
// update orderId for schedule dates
|
||||
await tx.scheduleDate.updateMany({
|
||||
where: { scheduleId: schedule?.id },
|
||||
data: {
|
||||
orderId: order.id,
|
||||
},
|
||||
})
|
||||
return {
|
||||
message: 'Payment received',
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
Logger.error(`Transaction failed: ${error}`)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,10 +115,7 @@ export class PayosService {
|
||||
return await this.payos.getPaymentLinkInformation(orderId)
|
||||
}
|
||||
|
||||
async cancelPaymentURL(
|
||||
orderId: string | number,
|
||||
cancellationReason?: string,
|
||||
) {
|
||||
async cancelPaymentURL(orderId: string | number, cancellationReason?: string) {
|
||||
return await this.payos.cancelPaymentLink(orderId, cancellationReason)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user