From 16bb5c33843c169a3b29cb6032a942b45b9a8e92 Mon Sep 17 00:00:00 2001 From: Aswath S Date: Tue, 22 Aug 2023 17:48:36 +0530 Subject: [PATCH] feat: Added more query params to successRedirect page (#8271) Co-authored-by: Hariom Balhara --- .../BookEventForm/BookEventForm.tsx | 7 +- .../ee/payments/components/Payment.tsx | 64 +++++++++++++++---- .../ee/payments/components/PaymentPage.tsx | 3 +- .../features/ee/payments/pages/payment.tsx | 4 +- packages/lib/bookingSuccessRedirect.ts | 40 ++++++++++-- 5 files changed, 93 insertions(+), 25 deletions(-) diff --git a/packages/features/bookings/Booker/components/BookEventForm/BookEventForm.tsx b/packages/features/bookings/Booker/components/BookEventForm/BookEventForm.tsx index a162d31b0f..e40cf4725f 100644 --- a/packages/features/bookings/Booker/components/BookEventForm/BookEventForm.tsx +++ b/packages/features/bookings/Booker/components/BookEventForm/BookEventForm.tsx @@ -204,7 +204,7 @@ export const BookEventFormChild = ({ return bookingSuccessRedirect({ successRedirectUrl: eventType?.successRedirectUrl || "", query, - bookingUid: uid, + booking: responseData, }); }, onError: () => { @@ -214,7 +214,8 @@ export const BookEventFormChild = ({ const createRecurringBookingMutation = useMutation(createRecurringBooking, { onSuccess: async (responseData) => { - const { uid } = responseData[0] || {}; + const booking = responseData[0] || {}; + const { uid } = booking; if (!uid) { console.error("No uid returned from createRecurringBookingMutation"); @@ -233,7 +234,7 @@ export const BookEventFormChild = ({ return bookingSuccessRedirect({ successRedirectUrl: eventType?.successRedirectUrl || "", query, - bookingUid: uid, + booking, }); }, }); diff --git a/packages/features/ee/payments/components/Payment.tsx b/packages/features/ee/payments/components/Payment.tsx index b98008a939..7d418a97d5 100644 --- a/packages/features/ee/payments/components/Payment.tsx +++ b/packages/features/ee/payments/components/Payment.tsx @@ -8,28 +8,61 @@ import type { SyntheticEvent } from "react"; import { useEffect, useState } from "react"; import getStripe from "@calcom/app-store/stripepayment/lib/client"; -import { useBookingSuccessRedirect } from "@calcom/lib/bookingSuccessRedirect"; +import { getBookingRedirectExtraParams, useBookingSuccessRedirect } from "@calcom/lib/bookingSuccessRedirect"; import { CAL_URL } from "@calcom/lib/constants"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { Button, CheckboxField } from "@calcom/ui"; +import type { PaymentPageProps } from "../pages/payment"; + type Props = { payment: Omit & { data: Record; }; - eventType: { id: number; successRedirectUrl: EventType["successRedirectUrl"] }; - user: { username: string | null }; + eventType: { + id: number; + successRedirectUrl: EventType["successRedirectUrl"]; + }; + user: { + username: string | null; + }; location?: string | null; - bookingId: number; - bookingUid: string; clientSecret: string; + booking: PaymentPageProps["booking"]; }; type States = - | { status: "idle" } - | { status: "processing" } - | { status: "error"; error: Error } - | { status: "ok" }; + | { + status: "idle"; + } + | { + status: "processing"; + } + | { + status: "error"; + error: Error; + } + | { + status: "ok"; + }; + +const getReturnUrl = (props: Props) => { + if (!props.eventType.successRedirectUrl) { + return `${CAL_URL}/booking/${props.booking.uid}`; + } + + const returnUrl = new URL(props.eventType.successRedirectUrl); + const queryParams = getBookingRedirectExtraParams(props.booking); + + Object.entries(queryParams).forEach(([key, value]) => { + if (value === null || value === undefined) { + return; + } + returnUrl.searchParams.append(key, String(value)); + }); + + return returnUrl.toString(); +}; const PaymentForm = (props: Props) => { const { t, i18n } = useLocale(); @@ -53,25 +86,28 @@ const PaymentForm = (props: Props) => { setState({ status: "processing" }); let payload; - const params: { [k: string]: any } = { - uid: props.bookingUid, + const params: { + [k: string]: any; + } = { + uid: props.booking.uid, email: searchParams.get("email"), }; if (paymentOption === "HOLD" && "setupIntent" in props.payment.data) { payload = await stripe.confirmSetup({ elements, confirmParams: { - return_url: props.eventType.successRedirectUrl || `${CAL_URL}/booking/${props.bookingUid}`, + return_url: getReturnUrl(props), }, }); } else if (paymentOption === "ON_BOOKING") { payload = await stripe.confirmPayment({ elements, confirmParams: { - return_url: props.eventType.successRedirectUrl || `${CAL_URL}/booking/${props.bookingUid}`, + return_url: getReturnUrl(props), }, }); } + if (payload?.error) { setState({ status: "error", @@ -89,7 +125,7 @@ const PaymentForm = (props: Props) => { return bookingSuccessRedirect({ successRedirectUrl: props.eventType.successRedirectUrl, query: params, - bookingUid: props.bookingUid, + booking: props.booking, }); } }; diff --git a/packages/features/ee/payments/components/PaymentPage.tsx b/packages/features/ee/payments/components/PaymentPage.tsx index e49bd8a68d..5022485fcd 100644 --- a/packages/features/ee/payments/components/PaymentPage.tsx +++ b/packages/features/ee/payments/components/PaymentPage.tsx @@ -139,8 +139,7 @@ const PaymentPage: FC = (props) => { eventType={props.eventType} user={props.user} location={props.booking.location} - bookingId={props.booking.id} - bookingUid={props.booking.uid} + booking={props.booking} /> )} {props.payment.appId === "paypal" && !props.payment.success && ( diff --git a/packages/features/ee/payments/pages/payment.tsx b/packages/features/ee/payments/pages/payment.tsx index 2dd032035a..f9dc8a0dbc 100644 --- a/packages/features/ee/payments/pages/payment.tsx +++ b/packages/features/ee/payments/pages/payment.tsx @@ -40,6 +40,7 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => description: true, title: true, startTime: true, + endTime: true, attendees: { select: { email: true, @@ -96,10 +97,11 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => if (!_booking) return { notFound: true }; - const { startTime, eventType, ...restBooking } = _booking; + const { startTime, endTime, eventType, ...restBooking } = _booking; const booking = { ...restBooking, startTime: startTime.toString(), + endTime: endTime.toString(), }; if (!eventType) return { notFound: true }; diff --git a/packages/lib/bookingSuccessRedirect.ts b/packages/lib/bookingSuccessRedirect.ts index 5e93c3e1f9..303f4fca4e 100644 --- a/packages/lib/bookingSuccessRedirect.ts +++ b/packages/lib/bookingSuccessRedirect.ts @@ -1,6 +1,9 @@ -import type { Booking, EventType } from "@prisma/client"; +import type { EventType } from "@prisma/client"; import { useRouter, useSearchParams } from "next/navigation"; +import type { PaymentPageProps } from "@calcom/ee/payments/pages/payment"; +import type { BookingResponse } from "@calcom/features/bookings/types"; + function getNewSeachParams(args: { query: Record; searchParams?: URLSearchParams; @@ -16,27 +19,54 @@ function getNewSeachParams(args: { return newSearchParams; } +type SuccessRedirectBookingType = Pick< + BookingResponse | PaymentPageProps["booking"], + "uid" | "title" | "description" | "startTime" | "endTime" | "location" +>; + +export const getBookingRedirectExtraParams = (booking: SuccessRedirectBookingType) => { + type BookingResponseKey = keyof SuccessRedirectBookingType; + const redirectQueryParamKeys: BookingResponseKey[] = [ + "title", + "description", + "startTime", + "endTime", + "location", + ]; + + return (Object.keys(booking) as BookingResponseKey[]) + .filter((key) => redirectQueryParamKeys.includes(key)) + .reduce((obj, key) => ({ ...obj, [key]: booking[key] }), {}); +}; + export const useBookingSuccessRedirect = () => { const router = useRouter(); const searchParams = useSearchParams(); const bookingSuccessRedirect = ({ successRedirectUrl, - bookingUid, query, + booking, }: { successRedirectUrl: EventType["successRedirectUrl"]; - bookingUid: Booking["uid"] | undefined; query: Record; + booking: SuccessRedirectBookingType; }) => { if (successRedirectUrl) { const url = new URL(successRedirectUrl); // Using parent ensures, Embed iframe would redirect outside of the iframe. - const newSearchParams = getNewSeachParams({ query, searchParams }); + const bookingExtraParams = getBookingRedirectExtraParams(booking); + const newSearchParams = getNewSeachParams({ + query: { + ...query, + ...bookingExtraParams, + }, + searchParams, + }); window.parent.location.href = `${url.toString()}?${newSearchParams.toString()}`; return; } const newSearchParams = getNewSeachParams({ query }); - return router.push(`/booking/${bookingUid}?${newSearchParams.toString()}`); + return router.push(`/booking/${booking.uid}?${newSearchParams.toString()}`); }; return bookingSuccessRedirect;