2023-08-11 23:29:48 +00:00
|
|
|
import type { Payment } from "@prisma/client";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { GetServerSidePropsContext } from "next";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { z } from "zod";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { BookingStatus } from "@calcom/prisma/enums";
|
2022-10-14 16:24:43 +00:00
|
|
|
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
|
2022-07-28 19:58:26 +00:00
|
|
|
import type { inferSSRProps } from "@calcom/types/inferSSRProps";
|
2021-09-22 18:36:13 +00:00
|
|
|
|
2023-08-11 23:29:48 +00:00
|
|
|
import { ssrInit } from "../../../../../apps/web/server/lib/ssr";
|
2022-10-10 08:11:47 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
export type PaymentPageProps = inferSSRProps<typeof getServerSideProps>;
|
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
const querySchema = z.object({
|
|
|
|
uid: z.string(),
|
|
|
|
});
|
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
|
2022-10-10 08:11:47 +00:00
|
|
|
const ssr = await ssrInit(context);
|
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
const { uid } = querySchema.parse(context.query);
|
2021-09-22 18:36:13 +00:00
|
|
|
const rawPayment = await prisma.payment.findFirst({
|
|
|
|
where: {
|
2022-07-28 19:58:26 +00:00
|
|
|
uid,
|
2021-09-22 18:36:13 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
data: true,
|
|
|
|
success: true,
|
|
|
|
uid: true,
|
|
|
|
refunded: true,
|
|
|
|
bookingId: true,
|
2023-02-08 20:36:22 +00:00
|
|
|
appId: true,
|
2023-04-11 21:44:14 +00:00
|
|
|
amount: true,
|
|
|
|
currency: true,
|
|
|
|
paymentOption: true,
|
2021-09-22 18:36:13 +00:00
|
|
|
booking: {
|
|
|
|
select: {
|
2022-05-11 20:36:38 +00:00
|
|
|
id: true,
|
2022-11-15 19:00:02 +00:00
|
|
|
uid: true,
|
2021-09-22 18:36:13 +00:00
|
|
|
description: true,
|
|
|
|
title: true,
|
|
|
|
startTime: true,
|
|
|
|
attendees: {
|
|
|
|
select: {
|
|
|
|
email: true,
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
eventTypeId: true,
|
|
|
|
location: true,
|
2023-02-13 12:26:28 +00:00
|
|
|
status: true,
|
|
|
|
rejectionReason: true,
|
|
|
|
cancellationReason: true,
|
2021-09-22 18:36:13 +00:00
|
|
|
eventType: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
title: true,
|
|
|
|
description: true,
|
|
|
|
length: true,
|
|
|
|
eventName: true,
|
|
|
|
requiresConfirmation: true,
|
|
|
|
userId: true,
|
2022-10-14 16:24:43 +00:00
|
|
|
metadata: true,
|
2021-09-22 18:36:13 +00:00
|
|
|
users: {
|
|
|
|
select: {
|
|
|
|
name: true,
|
|
|
|
username: true,
|
|
|
|
hideBranding: true,
|
|
|
|
theme: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
name: true,
|
|
|
|
hideBranding: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
price: true,
|
|
|
|
currency: true,
|
2023-04-11 02:20:15 +00:00
|
|
|
successRedirectUrl: true,
|
2021-09-22 18:36:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-05-31 13:40:12 +00:00
|
|
|
if (!rawPayment) return { notFound: true };
|
2021-09-22 18:36:13 +00:00
|
|
|
|
|
|
|
const { data, booking: _booking, ...restPayment } = rawPayment;
|
2023-08-11 23:29:48 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
const payment = {
|
|
|
|
...restPayment,
|
2023-08-11 23:29:48 +00:00
|
|
|
data: data as Record<string, unknown>,
|
2021-09-22 18:36:13 +00:00
|
|
|
};
|
|
|
|
|
2022-05-31 13:40:12 +00:00
|
|
|
if (!_booking) return { notFound: true };
|
2021-09-22 18:36:13 +00:00
|
|
|
|
|
|
|
const { startTime, eventType, ...restBooking } = _booking;
|
|
|
|
const booking = {
|
|
|
|
...restBooking,
|
|
|
|
startTime: startTime.toString(),
|
|
|
|
};
|
|
|
|
|
2022-05-31 13:40:12 +00:00
|
|
|
if (!eventType) return { notFound: true };
|
2021-09-22 18:36:13 +00:00
|
|
|
|
|
|
|
const [user] = eventType.users;
|
|
|
|
if (!user) return { notFound: true };
|
|
|
|
|
|
|
|
const profile = {
|
|
|
|
name: eventType.team?.name || user?.name || null,
|
|
|
|
theme: (!eventType.team?.name && user?.theme) || null,
|
|
|
|
hideBranding: eventType.team?.hideBranding || user?.hideBranding || null,
|
|
|
|
};
|
|
|
|
|
2023-02-13 12:26:28 +00:00
|
|
|
if (
|
|
|
|
([BookingStatus.CANCELLED, BookingStatus.REJECTED] as BookingStatus[]).includes(
|
|
|
|
booking.status as BookingStatus
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
destination: `/booking/${booking.uid}`,
|
|
|
|
permanent: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
user,
|
2022-10-14 16:24:43 +00:00
|
|
|
eventType: {
|
|
|
|
...eventType,
|
|
|
|
metadata: EventTypeMetaDataSchema.parse(eventType.metadata),
|
|
|
|
},
|
2021-09-22 18:36:13 +00:00
|
|
|
booking,
|
2022-10-10 08:11:47 +00:00
|
|
|
trpcState: ssr.dehydrate(),
|
2021-09-22 18:36:13 +00:00
|
|
|
payment,
|
2023-08-11 23:29:48 +00:00
|
|
|
clientSecret: getClientSecretFromPayment(payment),
|
2021-09-22 18:36:13 +00:00
|
|
|
profile,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
2023-08-11 23:29:48 +00:00
|
|
|
|
|
|
|
function hasStringProp<T extends string>(x: unknown, key: T): x is { [key in T]: string } {
|
|
|
|
return !!x && typeof x === "object" && key in x;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getClientSecretFromPayment(
|
|
|
|
payment: Omit<Partial<Payment>, "data"> & { data: Record<string, unknown> }
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
payment.paymentOption === "HOLD" &&
|
|
|
|
hasStringProp(payment.data, "setupIntent") &&
|
|
|
|
hasStringProp(payment.data.setupIntent, "client_secret")
|
|
|
|
) {
|
|
|
|
return payment.data.setupIntent.client_secret;
|
|
|
|
}
|
|
|
|
if (hasStringProp(payment.data, "client_secret")) {
|
|
|
|
return payment.data.client_secret;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|