feat: Added more query params to successRedirect page (#8271)

Co-authored-by: Hariom Balhara <[email protected]>
This commit is contained in:
Aswath SandHariom Balhara authored and GitHub committed 2023-08-22 12:18:36 +00:00
1 parent c55d97368e
commit 16bb5c3384
5 files changed
+93 -25

No files matched your search

@@ -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,
});
},
});
@@ -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<Payment, "id" | "fee" | "success" | "refunded" | "externalId" | "data"> & {
data: Record<string, unknown>;
};
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,
});
}
};
@@ -139,8 +139,7 @@ const PaymentPage: FC<PaymentPageProps> = (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 && (
@@ -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 };
+35 -5
View File
@@ -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<string, string | null | undefined | boolean>;
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<string, string | null | undefined | boolean>;
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;