2023-04-11 21:44:14 +00:00
|
|
|
import type { Payment } from "@prisma/client";
|
2023-04-14 21:56:16 +00:00
|
|
|
import { useElements, useStripe, PaymentElement, Elements } from "@stripe/react-stripe-js";
|
2023-04-05 18:14:46 +00:00
|
|
|
import type stripejs from "@stripe/stripe-js";
|
2023-04-14 21:56:16 +00:00
|
|
|
import type { StripeElementLocale } from "@stripe/stripe-js";
|
2021-09-22 18:36:13 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-04-05 18:14:46 +00:00
|
|
|
import type { SyntheticEvent } from "react";
|
|
|
|
import { useEffect, useState } from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2023-04-14 21:56:16 +00:00
|
|
|
import getStripe from "@calcom/app-store/stripepayment/lib/client";
|
2023-04-11 21:44:14 +00:00
|
|
|
import type { StripePaymentData, StripeSetupIntentData } from "@calcom/app-store/stripepayment/lib/server";
|
2023-04-11 02:20:15 +00:00
|
|
|
import { bookingSuccessRedirect } from "@calcom/lib/bookingSuccessRedirect";
|
2023-04-14 21:56:16 +00:00
|
|
|
import { CAL_URL } from "@calcom/lib/constants";
|
2022-11-22 16:38:25 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-04-11 21:44:14 +00:00
|
|
|
import { Button, Checkbox } from "@calcom/ui";
|
2021-09-22 18:36:13 +00:00
|
|
|
|
2023-04-11 02:20:15 +00:00
|
|
|
import type { EventType } from ".prisma/client";
|
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
type Props = {
|
2023-04-11 21:44:14 +00:00
|
|
|
payment: Omit<Payment, "id" | "fee" | "success" | "refunded" | "externalId" | "data"> & {
|
|
|
|
data: StripePaymentData | StripeSetupIntentData;
|
2021-09-22 18:36:13 +00:00
|
|
|
};
|
2023-04-11 02:20:15 +00:00
|
|
|
eventType: { id: number; successRedirectUrl: EventType["successRedirectUrl"] };
|
2021-09-22 18:36:13 +00:00
|
|
|
user: { username: string | null };
|
2022-02-08 15:18:10 +00:00
|
|
|
location?: string | null;
|
2022-05-11 20:36:38 +00:00
|
|
|
bookingId: number;
|
2022-11-15 19:00:02 +00:00
|
|
|
bookingUid: string;
|
2021-09-22 18:36:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type States =
|
|
|
|
| { status: "idle" }
|
|
|
|
| { status: "processing" }
|
|
|
|
| { status: "error"; error: Error }
|
|
|
|
| { status: "ok" };
|
|
|
|
|
2023-04-14 21:56:16 +00:00
|
|
|
const PaymentForm = (props: Props) => {
|
2021-12-17 16:58:23 +00:00
|
|
|
const { t, i18n } = useLocale();
|
2021-09-22 18:36:13 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const [state, setState] = useState<States>({ status: "idle" });
|
|
|
|
const stripe = useStripe();
|
|
|
|
const elements = useElements();
|
2023-04-11 21:44:14 +00:00
|
|
|
const paymentOption = props.payment.paymentOption;
|
|
|
|
const [holdAcknowledged, setHoldAcknowledged] = useState<boolean>(paymentOption === "HOLD" ? false : true);
|
2021-12-17 16:58:23 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
elements?.update({ locale: i18n.language as StripeElementLocale });
|
|
|
|
}, [elements, i18n.language]);
|
|
|
|
|
2021-10-28 22:58:26 +00:00
|
|
|
const handleSubmit = async (ev: SyntheticEvent) => {
|
2021-09-22 18:36:13 +00:00
|
|
|
ev.preventDefault();
|
2021-10-28 22:58:26 +00:00
|
|
|
|
2022-12-02 00:12:06 +00:00
|
|
|
if (!stripe || !elements || !router.isReady) return;
|
2021-09-22 18:36:13 +00:00
|
|
|
setState({ status: "processing" });
|
2023-04-11 21:44:14 +00:00
|
|
|
|
|
|
|
let payload;
|
|
|
|
const params: { [k: string]: any } = {
|
|
|
|
uid: props.bookingUid,
|
|
|
|
email: router.query.email,
|
|
|
|
};
|
|
|
|
if (paymentOption === "HOLD" && "setupIntent" in props.payment.data) {
|
2023-04-14 21:56:16 +00:00
|
|
|
payload = await stripe.confirmSetup({
|
|
|
|
elements,
|
|
|
|
confirmParams: {
|
2023-04-19 20:46:59 +00:00
|
|
|
return_url: props.eventType.successRedirectUrl || `${CAL_URL}/booking/${props.bookingUid}`,
|
2023-04-11 21:44:14 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (paymentOption === "ON_BOOKING") {
|
2023-04-14 21:56:16 +00:00
|
|
|
payload = await stripe.confirmPayment({
|
|
|
|
elements,
|
|
|
|
confirmParams: {
|
2023-04-19 20:46:59 +00:00
|
|
|
return_url: props.eventType.successRedirectUrl || `${CAL_URL}/booking/${props.bookingUid}`,
|
2023-04-11 21:44:14 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (payload?.error) {
|
2021-09-22 18:36:13 +00:00
|
|
|
setState({
|
|
|
|
status: "error",
|
|
|
|
error: new Error(`Payment failed: ${payload.error.message}`),
|
|
|
|
});
|
|
|
|
} else {
|
2021-10-28 22:58:26 +00:00
|
|
|
if (props.location) {
|
|
|
|
if (props.location.includes("integration")) {
|
|
|
|
params.location = t("web_conferencing_details_to_follow");
|
2021-09-22 18:36:13 +00:00
|
|
|
} else {
|
2021-10-28 22:58:26 +00:00
|
|
|
params.location = props.location;
|
2021-09-22 18:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 02:20:15 +00:00
|
|
|
return bookingSuccessRedirect({
|
|
|
|
router,
|
|
|
|
successRedirectUrl: props.eventType.successRedirectUrl,
|
|
|
|
query: params,
|
|
|
|
bookingUid: props.bookingUid,
|
|
|
|
});
|
2021-09-22 18:36:13 +00:00
|
|
|
}
|
|
|
|
};
|
2023-04-14 21:56:16 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
return (
|
2023-04-11 21:44:14 +00:00
|
|
|
<form id="payment-form" className="bg-subtle mt-4 rounded-md p-6" onSubmit={handleSubmit}>
|
2023-04-14 21:56:16 +00:00
|
|
|
<div>
|
|
|
|
<PaymentElement onChange={() => setState({ status: "idle" })} />
|
|
|
|
</div>
|
2023-04-11 21:44:14 +00:00
|
|
|
{paymentOption === "HOLD" && (
|
2023-06-22 22:25:37 +00:00
|
|
|
<div className="bg-info mb-5 mt-2 rounded-md p-3">
|
2023-04-11 21:44:14 +00:00
|
|
|
<Checkbox
|
|
|
|
description={t("acknowledge_booking_no_show_fee", {
|
|
|
|
amount: props.payment.amount / 100,
|
|
|
|
formatParams: { amount: { currency: props.payment.currency } },
|
|
|
|
})}
|
|
|
|
onChange={(e) => setHoldAcknowledged(e.target.checked)}
|
|
|
|
descriptionClassName="text-blue-900 font-semibold"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div className="mt-2 flex justify-end space-x-2">
|
|
|
|
<Button
|
|
|
|
color="minimal"
|
|
|
|
disabled={!holdAcknowledged || ["processing", "error"].includes(state.status)}
|
2023-07-10 13:58:38 +00:00
|
|
|
id="cancel"
|
2023-07-18 20:27:54 +00:00
|
|
|
onClick={() => router.back()}>
|
2023-04-11 21:44:14 +00:00
|
|
|
<span id="button-text">{t("cancel")}</span>
|
|
|
|
</Button>
|
2021-09-22 18:36:13 +00:00
|
|
|
<Button
|
|
|
|
type="submit"
|
2023-04-11 21:44:14 +00:00
|
|
|
disabled={!holdAcknowledged || ["processing", "error"].includes(state.status)}
|
2021-09-22 18:36:13 +00:00
|
|
|
loading={state.status === "processing"}
|
2023-04-11 21:44:14 +00:00
|
|
|
id="submit"
|
2023-04-14 21:56:16 +00:00
|
|
|
color="secondary">
|
2021-09-22 18:36:13 +00:00
|
|
|
<span id="button-text">
|
2023-04-11 21:44:14 +00:00
|
|
|
{state.status === "processing" ? (
|
|
|
|
<div className="spinner" id="spinner" />
|
|
|
|
) : paymentOption === "HOLD" ? (
|
|
|
|
t("submit_card")
|
|
|
|
) : (
|
|
|
|
t("pay_now")
|
|
|
|
)}
|
2021-09-22 18:36:13 +00:00
|
|
|
</span>
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
{state.status === "error" && (
|
2023-04-11 21:44:14 +00:00
|
|
|
<div className="mt-4 text-center text-red-900 dark:text-gray-300" role="alert">
|
2021-09-22 18:36:13 +00:00
|
|
|
{state.error.message}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</form>
|
|
|
|
);
|
2023-04-14 21:56:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const ELEMENT_STYLES: stripejs.Appearance = {
|
|
|
|
theme: "none",
|
|
|
|
};
|
|
|
|
|
|
|
|
const ELEMENT_STYLES_DARK: stripejs.Appearance = {
|
|
|
|
theme: "night",
|
|
|
|
variables: {
|
|
|
|
colorText: "#d6d6d6",
|
|
|
|
fontWeightNormal: "600",
|
|
|
|
borderRadius: "6px",
|
|
|
|
colorBackground: "#101010",
|
|
|
|
colorPrimary: "#d6d6d6",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function PaymentComponent(props: Props) {
|
|
|
|
const stripePromise = getStripe((props.payment.data as StripePaymentData).stripe_publishable_key);
|
|
|
|
const paymentOption = props.payment.paymentOption;
|
|
|
|
const [darkMode, setDarkMode] = useState<boolean>(false);
|
|
|
|
let clientSecret: string | null;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setDarkMode(window.matchMedia("(prefers-color-scheme: dark)").matches);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (paymentOption === "HOLD" && "setupIntent" in props.payment.data) {
|
|
|
|
clientSecret = props.payment.data.setupIntent.client_secret;
|
|
|
|
} else if (!("setupIntent" in props.payment.data)) {
|
|
|
|
clientSecret = props.payment.data.client_secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Elements
|
|
|
|
stripe={stripePromise}
|
|
|
|
options={{
|
|
|
|
clientSecret: clientSecret!,
|
|
|
|
appearance: darkMode ? ELEMENT_STYLES_DARK : ELEMENT_STYLES,
|
|
|
|
}}>
|
|
|
|
<PaymentForm {...props} />
|
|
|
|
</Elements>
|
|
|
|
);
|
2021-09-22 18:36:13 +00:00
|
|
|
}
|