update payment
This commit is contained in:
@@ -8,11 +8,14 @@ import {
|
||||
import { Builder } from '../Graphql/graphql.builder'
|
||||
import { PrismaService } from '../Prisma/prisma.service'
|
||||
import { OrderStatus } from '@prisma/client'
|
||||
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
|
||||
import { PayosService } from 'src/Payos/payos.service'
|
||||
@Injectable()
|
||||
export class OrderSchema extends PothosSchema {
|
||||
constructor(
|
||||
@Inject(SchemaBuilderToken) private readonly builder: Builder,
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly payosService: PayosService,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
@@ -121,11 +124,24 @@ export class OrderSchema extends PothosSchema {
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
resolve: async (query, _root, args, _ctx, _info) => {
|
||||
resolve: async (query, _root, args, ctx, _info) => {
|
||||
return this.prisma.$transaction(async (prisma) => {
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Subscription is not allowed')
|
||||
}
|
||||
if (!args.data.service.connect?.id) {
|
||||
throw new Error('Service not found')
|
||||
}
|
||||
const order = await prisma.order.create({
|
||||
...query,
|
||||
data: args.data,
|
||||
data: {
|
||||
status: OrderStatus.PENDING,
|
||||
total:
|
||||
(args.data.service.connect?.price as number | undefined) ?? 0,
|
||||
userId: ctx.http.me.id,
|
||||
serviceId: args.data.service.connect.id,
|
||||
scheduleId: args.data.scheduleId,
|
||||
},
|
||||
})
|
||||
// check if service is valid
|
||||
if (!args.data.service.connect) {
|
||||
@@ -135,15 +151,32 @@ export class OrderSchema extends PothosSchema {
|
||||
if (args.data.service.connect.price === 0) {
|
||||
return order
|
||||
}
|
||||
// generate payment code by prefix 'EPESS' + 6 hex digits
|
||||
const paymentCode = 'EPESS' + Math.random().toString(16).slice(2, 8)
|
||||
// random integer
|
||||
const paymentCode = Math.floor(Math.random() * 1000000)
|
||||
// create payment
|
||||
await prisma.payment.create({
|
||||
const payment = await prisma.payment.create({
|
||||
data: {
|
||||
orderId: order.id,
|
||||
amount: args.data.service.connect.price as number,
|
||||
paymentCode: paymentCode,
|
||||
expiredAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
|
||||
paymentCode: paymentCode.toString(),
|
||||
expiredAt: DateTimeUtils.now().plus({ minutes: 15 }).toJSDate(),
|
||||
},
|
||||
})
|
||||
// generate payment url
|
||||
const paymentData = await this.payosService.createPayment({
|
||||
orderCode: paymentCode,
|
||||
amount: args.data.service.connect.price as number,
|
||||
description: args.data.service.connect.name as string,
|
||||
buyerName: ctx.http.me.name as string,
|
||||
buyerEmail: ctx.http.me.email as string,
|
||||
returnUrl: `${process.env.PAYOS_WEBHOOK_URL}/return`,
|
||||
cancelUrl: `${process.env.PAYOS_WEBHOOK_URL}/cancel`,
|
||||
})
|
||||
// update payment url
|
||||
await prisma.payment.update({
|
||||
where: { id: payment.id },
|
||||
data: {
|
||||
paymentCode: paymentData.paymentLinkId,
|
||||
},
|
||||
})
|
||||
return order
|
||||
|
||||
Reference in New Issue
Block a user