Files
calendar/packages/app-store/hitpay/api/callback.ts
T
Muhammad Aiman SulaimanandGitHub 58d7e59b38 feat: Updates to HitPay Payment App (#19737)
* feat: add hitpay embed feature for cal embed

feat: update hitpay app description

fix: fixed customers being able to exceed bookings in the same timeslot when using HitPay

fix: fixed callback issue in cal.com using hitpay app when not receiving payment responses

* fix: fixed type-check error
2025-03-05 11:29:38 +00:00

85 lines
2.1 KiB
TypeScript

import type { NextApiRequest, NextApiResponse } from "next";
import qs from "qs";
import { z } from "zod";
import { HttpError as HttpCode } from "@calcom/lib/http-error";
import prisma from "@calcom/prisma";
const PaymentDataSchema = z.object({
id: z.string(),
url: z.string(),
defaultLink: z.string(),
email: z.string(),
});
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { reference, status } = req.query;
if (!reference) {
throw new HttpCode({ statusCode: 204, message: "Reference not found" });
}
const payment = await prisma.payment.findFirst({
where: {
externalId: reference as string,
},
select: {
id: true,
amount: true,
bookingId: true,
data: true,
booking: {
select: {
uid: true,
user: {
select: {
email: true,
username: true,
credentials: {
where: {
type: "hitpay_payment",
},
},
},
},
responses: true,
eventType: {
select: {
slug: true,
},
},
},
},
},
});
if (!payment) {
throw new HttpCode({ statusCode: 204, message: "Payment not found" });
}
const key = payment.booking?.user?.credentials?.[0].key;
if (!key) {
throw new HttpCode({ statusCode: 204, message: "Credential not found" });
}
if (!payment.booking || !payment.booking.user || !payment.booking.eventType) {
throw new HttpCode({ statusCode: 204, message: "Booking not correct" });
}
if (status !== "completed") {
const url = `/${payment.booking.user.username}/${payment.booking.eventType.slug}`;
return res.redirect(url);
}
const parsedData = PaymentDataSchema.parse(payment.data);
const queryParams = {
"flag.coep": false,
isSuccessBookingPage: true,
email: parsedData.email,
eventTypeSlug: payment.booking.eventType.slug,
};
const query = qs.stringify(queryParams);
const url = `/booking/${payment.booking.uid}?${query}`;
return res.redirect(url);
}