From 3895ef002e64680c98087b96879240418b958809 Mon Sep 17 00:00:00 2001 From: Muhammad Aiman Sulaiman <101640454+MuhammadAimanSulaiman@users.noreply.github.com> Date: Fri, 13 Jun 2025 14:40:28 +0800 Subject: [PATCH] fix: Updates to HitPay app (#21175) * reapplied my changes after syncing with upstream * fix: changed raw error to sanitized error * fix: added try/catch to create catch --- packages/app-store/hitpay/api/callback.ts | 8 +++++++ packages/app-store/hitpay/api/webhook.ts | 1 + .../app-store/hitpay/lib/PaymentService.ts | 23 +++++++++++++++++-- packages/app-store/hitpay/lib/types.d.ts | 1 + 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/app-store/hitpay/api/callback.ts b/packages/app-store/hitpay/api/callback.ts index cdf2b1a9e6..9c9c9e0328 100644 --- a/packages/app-store/hitpay/api/callback.ts +++ b/packages/app-store/hitpay/api/callback.ts @@ -65,6 +65,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) } if (status !== "completed") { + await prisma.booking.update({ + where: { + id: payment.bookingId, + }, + data: { + status: "CANCELLED", + }, + }); const url = `/${payment.booking.user.username}/${payment.booking.eventType.slug}`; return res.redirect(url); } diff --git a/packages/app-store/hitpay/api/webhook.ts b/packages/app-store/hitpay/api/webhook.ts index 6cc7f896ab..eb920c18fc 100644 --- a/packages/app-store/hitpay/api/webhook.ts +++ b/packages/app-store/hitpay/api/webhook.ts @@ -47,6 +47,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) if (req.method !== "POST") { throw new HttpCode({ statusCode: 405, message: "Method Not Allowed" }); } + const obj: WebhookReturn = req.body as WebhookReturn; const excluded = { ...obj } as Partial; delete excluded.hmac; diff --git a/packages/app-store/hitpay/lib/PaymentService.ts b/packages/app-store/hitpay/lib/PaymentService.ts index cd3cfe3ed5..6618f14a08 100644 --- a/packages/app-store/hitpay/lib/PaymentService.ts +++ b/packages/app-store/hitpay/lib/PaymentService.ts @@ -50,6 +50,7 @@ export class PaymentService implements IAbstractPaymentService { title: true, startTime: true, endTime: true, + eventTypeId: true, eventType: { select: { slug: true, @@ -64,11 +65,13 @@ export class PaymentService implements IAbstractPaymentService { throw new Error("Booking or API key not found"); } - const { startTime, endTime } = booking; + const { startTime, endTime, eventTypeId } = booking; const bookingsWithSameTimeSlot = await prisma.booking.findMany({ where: { + eventTypeId, startTime, endTime, + OR: [{ status: "PENDING" }, { status: "AWAITING_HOST" }], }, select: { uid: true, @@ -169,8 +172,24 @@ export class PaymentService implements IAbstractPaymentService { throw new Error("Failed to store Payment data"); } return paymentData; - } catch (error) { + } catch (error: any) { log.error("Payment could not be created", bookingId, safeStringify(error)); + try { + await prisma.booking.update({ + where: { + id: bookingId, + }, + data: { + status: "CANCELLED", + }, + }); + } catch (error) { + throw new Error(ErrorCode.PaymentCreationFailure); + } + + if (error.message === ErrorCode.BookingSeatsFull || error.message === ErrorCode.NoAvailableUsersFound) { + throw error; + } throw new Error(ErrorCode.PaymentCreationFailure); } } diff --git a/packages/app-store/hitpay/lib/types.d.ts b/packages/app-store/hitpay/lib/types.d.ts index 8f0a9a7267..443b5af52b 100644 --- a/packages/app-store/hitpay/lib/types.d.ts +++ b/packages/app-store/hitpay/lib/types.d.ts @@ -6,6 +6,7 @@ export type PaidBooking = Prisma.BookingGetPayload<{ title: true; startTime: true; endTime: true; + eventTypeId: true; eventType: { select: { slug: true;