finish payment strategies
This commit is contained in:
@@ -125,61 +125,112 @@ export class OrderSchema extends PothosSchema {
|
||||
}),
|
||||
},
|
||||
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: {
|
||||
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,
|
||||
},
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Subscription is not allowed')
|
||||
}
|
||||
if (!args.data.service.connect?.id) {
|
||||
throw new Error('Service not found')
|
||||
}
|
||||
// query service
|
||||
const service = await this.prisma.service.findUnique({
|
||||
where: { id: args.data.service.connect.id },
|
||||
})
|
||||
if (!service) {
|
||||
throw new Error('Service not found')
|
||||
}
|
||||
// check if input schedule has order id then throw error
|
||||
const schedule = await this.prisma.schedule.findUnique({
|
||||
where: { id: args.data.scheduleId },
|
||||
})
|
||||
if (schedule?.orderId) {
|
||||
// check if order status is PAID OR PENDING
|
||||
const order = await this.prisma.order.findUnique({
|
||||
where: { id: schedule.orderId },
|
||||
})
|
||||
// check if service is valid
|
||||
if (!args.data.service.connect) {
|
||||
throw new Error('Service not found')
|
||||
if (
|
||||
order?.status === OrderStatus.PAID ||
|
||||
order?.status === OrderStatus.PENDING
|
||||
) {
|
||||
throw new Error('Schedule already has an order')
|
||||
}
|
||||
// check if service price is free
|
||||
if (args.data.service.connect.price === 0) {
|
||||
return order
|
||||
}
|
||||
// random integer
|
||||
const paymentCode = Math.floor(Math.random() * 1000000)
|
||||
// create payment
|
||||
const payment = await prisma.payment.create({
|
||||
}
|
||||
const order = await this.prisma.order.create({
|
||||
...query,
|
||||
data: {
|
||||
status: OrderStatus.PENDING,
|
||||
total: service.price,
|
||||
userId: ctx.http.me.id,
|
||||
serviceId: service.id,
|
||||
scheduleId: args.data.scheduleId,
|
||||
},
|
||||
})
|
||||
// check if service is valid
|
||||
if (!args.data.service.connect) {
|
||||
throw new Error('Service not found')
|
||||
}
|
||||
|
||||
// check if order is free
|
||||
if (order.total === 0) {
|
||||
// assign schedule
|
||||
await this.prisma.schedule.update({
|
||||
where: { id: args.data.scheduleId },
|
||||
data: {
|
||||
orderId: order.id,
|
||||
amount: args.data.service.connect.price as number,
|
||||
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
|
||||
}
|
||||
|
||||
// random integer
|
||||
const paymentCode = Math.floor(Math.random() * 1000000)
|
||||
// create payment
|
||||
const payment = await this.prisma.payment.create({
|
||||
data: {
|
||||
orderId: order.id,
|
||||
amount: service.price,
|
||||
paymentCode: paymentCode.toString(),
|
||||
expiredAt: DateTimeUtils.now().plus({ minutes: 15 }).toJSDate(),
|
||||
},
|
||||
})
|
||||
// generate payment url
|
||||
const paymentData = await this.payosService.createPayment({
|
||||
orderCode: paymentCode,
|
||||
amount: service.price,
|
||||
description: service.name,
|
||||
buyerName: ctx.http.me.name,
|
||||
buyerEmail: ctx.http.me.email,
|
||||
returnUrl: `${process.env.PAYOS_RETURN_URL}`.replace(
|
||||
'<serviceId>',
|
||||
service.id,
|
||||
),
|
||||
cancelUrl: `${process.env.PAYOS_RETURN_URL}`.replace(
|
||||
'<serviceId>',
|
||||
service.id,
|
||||
),
|
||||
expiredAt: DateTimeUtils.now()
|
||||
.plus({ minutes: 15 })
|
||||
.toUnixInteger(),
|
||||
})
|
||||
// update order payment id
|
||||
await this.prisma.order.update({
|
||||
where: { id: order.id },
|
||||
data: {
|
||||
paymentId: payment.id,
|
||||
},
|
||||
})
|
||||
// update payment url
|
||||
await this.prisma.payment.update({
|
||||
where: { id: payment.id },
|
||||
data: {
|
||||
paymentCode: paymentData.paymentLinkId,
|
||||
},
|
||||
})
|
||||
// refetch order
|
||||
return await this.prisma.order.findUnique({
|
||||
where: { id: order.id },
|
||||
include: {
|
||||
payment: true,
|
||||
},
|
||||
})
|
||||
},
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user