feat: payment card atom (#17203)

* init stripe payment card atom

* cleanup

* update platform libraries

* fix import paths

* add function to get user payment info

* fixup

* resolve merge conflicts

* fixup fixup

* resolve merge conflicts

* fixup

* update payments page

* add props for event payment success and failure

* update props and fixup

* bump platform libraries

* fixup! bump platform libraries

* fixup! fixup! bump platform libraries

* resolve merge conflicts

* fix: add isActive to paymentsTab

---------

Co-authored-by: Morgan <[email protected]>
Co-authored-by: Morgan Vernay <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-10-31 11:21:13 +02:00
1 parent a5806cb100
commit fee3b73bda
15 files changed
+602 -129

No files matched your search

@@ -0,0 +1,37 @@
import { useQuery } from "@tanstack/react-query";
import type { PaymentPageProps } from "@calcom/features/ee/payments/pages/payment";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { ApiResponse } from "@calcom/platform-types";
import { useAtomsContext } from "../../hooks/useAtomsContext";
import http from "../../lib/http";
export const QUERY_KEY = "use-event-app-integration";
export const useAtomsEventTypePaymentInfo = ({
uid,
onEventTypePaymentInfoSuccess,
onEventTypePaymentInfoFailure,
}: {
uid: string;
onEventTypePaymentInfoSuccess?: () => void;
onEventTypePaymentInfoFailure?: () => void;
}) => {
const pathname = `/atoms/payment/${uid}`;
const { isInit, accessToken } = useAtomsContext();
return useQuery({
queryKey: [QUERY_KEY, uid],
queryFn: () => {
return http?.get<ApiResponse<Omit<PaymentPageProps, "trpcState">>>(pathname).then((res) => {
if (res.data.status === SUCCESS_STATUS) {
onEventTypePaymentInfoSuccess?.();
return res.data.data;
}
onEventTypePaymentInfoFailure?.();
throw new Error(res.data.error.message);
});
},
enabled: !!uid && isInit && !!accessToken,
});
};
@@ -175,6 +175,7 @@ function getNavigation({ length, multipleDuration, t, tabs, url, onClick, curren
tabsNavigation.push({
name: "event_payments_tab_title",
onClick: () => onClick("payments"),
isActive: currentTab === "payments",
href: `${url}?tabName=payments`,
icon: "credit-card",
info: `event_payments_tab_description`,
@@ -0,0 +1,151 @@
import React, { Suspense } from "react";
import { getSuccessPageLocationMessage } from "@calcom/app-store/locations";
import dayjs from "@calcom/dayjs";
import { PayIcon } from "@calcom/features/bookings/components/event-meta/PayIcon";
import { Price } from "@calcom/features/bookings/components/event-meta/Price";
import type { PaymentPageProps } from "@calcom/features/ee/payments/pages/payment";
import { APP_NAME, WEBSITE_URL } from "@calcom/lib/constants";
import getPaymentAppData from "@calcom/lib/getPaymentAppData";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { isBrowserLocale24h } from "@calcom/lib/timeFormat";
import { localStorage } from "@calcom/lib/webstorage";
import { AtomsWrapper } from "../../src/components/atoms-wrapper";
import { cn } from "../../src/lib/utils";
import { useAtomsEventTypePaymentInfo } from "../hooks/useAtomEventTypePaymentInfo";
const StripePaymentForm = React.lazy(() => import("./StripePaymentForm"));
export const PaymentForm = ({
paymentUid,
onPaymentSuccess,
onPaymentCancellation,
onEventTypePaymentInfoSuccess,
onEventTypePaymentInfoFailure,
}: {
paymentUid: string;
onPaymentSuccess?: (input: Omit<PaymentPageProps, "trpcState">) => void;
onPaymentCancellation?: (input: Omit<PaymentPageProps, "trpcState">) => void;
onEventTypePaymentInfoSuccess?: () => void;
onEventTypePaymentInfoFailure?: () => void;
}) => {
const { t } = useLocale();
const { data: paymentInfo, isLoading } = useAtomsEventTypePaymentInfo({
uid: paymentUid,
onEventTypePaymentInfoSuccess,
onEventTypePaymentInfoFailure,
});
const eventName = paymentInfo?.booking.title;
const is24h = isBrowserLocale24h();
const timezone =
localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess() || "Europe/London";
const date = dayjs.utc(paymentInfo?.booking.startTime).tz(timezone);
if (isLoading) return <h1 className="p-4 pt-4 text-xl">Loading...</h1>;
if (!paymentInfo) return <h1 className="p-4 text-xl">No payment found with UID - {paymentUid}</h1>;
const paymentAppData = getPaymentAppData(paymentInfo?.eventType);
return (
<AtomsWrapper>
<div>
<Suspense fallback={<></>}>
<main className="mx-auto">
<div>
<div className="flex min-h-screen items-end justify-center px-4 pb-20 pt-4 text-center sm:block sm:p-0">
<div className="inset-0 my-4 transition-opacity sm:my-0" aria-hidden="true">
<span className="hidden sm:inline-block sm:h-screen sm:align-middle" aria-hidden="true">
&#8203;
</span>
<div
className={cn(
"main bg-default border-subtle inline-block transform overflow-hidden rounded-lg border px-8 pb-4 pt-5 text-left align-bottom transition-all sm:my-8 sm:w-full sm:max-w-lg sm:py-6 sm:align-middle"
)}
role="dialog"
aria-modal="true"
aria-labelledby="modal-headline">
<div>
<div className="bg-success mx-auto flex h-12 w-12 items-center justify-center rounded-full">
<PayIcon currency={paymentAppData.currency} className="h-8 w-8 text-green-600" />
</div>
<div className="mt-3 text-center sm:mt-5">
<h3 className="text-emphasis text-2xl font-semibold leading-6" id="modal-headline">
{paymentAppData.paymentOption === "HOLD"
? t("complete_your_booking")
: t("payment")}
</h3>
<div className="text-default mt-4 grid grid-cols-3 border-b border-t py-4 text-left dark:border-gray-900 dark:text-gray-300">
<div className="font-medium">{t("what")}</div>
<div className="col-span-2 mb-6">{eventName}</div>
<div className="font-medium">{t("when")}</div>
<div className="col-span-2 mb-6">
{date.format("dddd, DD MMMM YYYY")}
<br />
{date.format(is24h ? "H:mm" : "h:mma")} - {paymentInfo.eventType.length} mins{" "}
<span className="text-subtle">({timezone})</span>
</div>
{paymentInfo.booking.location && (
<>
<div className="font-medium">{t("where")}</div>
<div className="col-span-2 mb-6">
{getSuccessPageLocationMessage(paymentInfo.booking.location, t)}
</div>
</>
)}
<div className="font-medium">
{paymentInfo.payment.paymentOption === "HOLD" ? t("no_show_fee") : t("price")}
</div>
<div className="col-span-2 mb-6 font-semibold">
<Price
currency={paymentAppData.currency}
price={paymentAppData.price}
displayAlternateSymbol={false}
/>
</div>
</div>
</div>
</div>
<div>
{paymentInfo.payment.success && !paymentInfo.payment.refunded && (
<div className="text-default mt-4 text-center dark:text-gray-300">{t("paid")}</div>
)}
{paymentInfo.payment.appId === "stripe" && !paymentInfo.payment.success && (
<StripePaymentForm
clientSecret={paymentInfo.clientSecret}
payment={paymentInfo.payment}
eventType={paymentInfo.eventType}
user={paymentInfo.user}
location={paymentInfo.booking.location}
booking={paymentInfo.booking}
uid={paymentUid}
onPaymentSuccess={onPaymentSuccess}
onPaymentCancellation={onPaymentCancellation}
/>
)}
{paymentInfo.payment.refunded && (
<div className="text-default mt-4 text-center dark:text-gray-300">
{t("refunded")}
</div>
)}
</div>
{!paymentInfo.profile.hideBranding && (
<div className="text-muted dark:text-inverted mt-4 border-t pt-4 text-center text-xs dark:border-gray-900">
<a href={`${WEBSITE_URL}/signup`}>
{t("create_booking_link_with_calcom", { appName: APP_NAME })}
</a>
</div>
)}
</div>
</div>
</div>
</div>
</main>
</Suspense>
</div>
</AtomsWrapper>
);
};
@@ -0,0 +1,131 @@
import { Elements, useElements, useStripe } from "@stripe/react-stripe-js";
import { useEffect, useState } from "react";
import type { SyntheticEvent } from "react";
import getStripe from "@calcom/app-store/stripepayment/lib/client";
import type { Props, States } from "@calcom/features/ee/payments/components/Payment";
import { PaymentFormComponent } from "@calcom/features/ee/payments/components/Payment";
import type { PaymentPageProps } from "@calcom/features/ee/payments/pages/payment";
import { useLocale } from "@calcom/lib/hooks/useLocale";
const StripePaymentComponent = (
props: Props & {
onPaymentSuccess?: (input: Omit<PaymentPageProps, "trpcState">) => void;
onPaymentCancellation?: (input: Omit<PaymentPageProps, "trpcState">) => void;
}
) => {
const { t } = useLocale();
const elements = useElements();
const paymentOption = props.payment.paymentOption;
const attendeeEmail = props.booking.attendees[0].email;
const stripe = useStripe();
const [state, setState] = useState<States>({ status: "idle" });
return (
<PaymentFormComponent
{...props}
elements={elements}
paymentOption={paymentOption}
state={state}
onSubmit={async (ev: SyntheticEvent) => {
ev.preventDefault();
if (!stripe || !elements) {
return;
}
setState({ status: "processing" });
let payload;
const params: {
uid: string;
email: string | null;
location?: string;
payment_intent?: string;
payment_intent_client_secret?: string;
redirect_status?: string;
} = {
uid: props.booking.uid,
email: attendeeEmail,
};
if (paymentOption === "HOLD" && "setupIntent" in props.payment.data) {
payload = await stripe.confirmSetup({
elements,
redirect: "if_required",
});
if (payload.setupIntent) {
params.payment_intent = payload.setupIntent.id;
params.payment_intent_client_secret = payload.setupIntent.client_secret || undefined;
params.redirect_status = payload.setupIntent.status;
}
} else if (paymentOption === "ON_BOOKING") {
payload = await stripe.confirmPayment({
elements,
redirect: "if_required",
});
if (payload.paymentIntent) {
params.payment_intent = payload.paymentIntent.id;
params.payment_intent_client_secret = payload.paymentIntent.client_secret || undefined;
params.redirect_status = payload.paymentIntent.status;
}
}
if (payload?.error) {
setState({
status: "error",
error: new Error(`Payment failed: ${payload.error.message}`),
});
} else {
setState({ status: "idle" });
props.onPaymentSuccess?.(props as unknown as Omit<PaymentPageProps, "trpcState">);
if (props.location) {
if (props.location.includes("integration")) {
params.location = t("web_conferencing_details_to_follow");
} else {
params.location = props.location;
}
}
}
}}
onCancel={() => {
props.onPaymentCancellation?.(props as unknown as Omit<PaymentPageProps, "trpcState">);
}}
onPaymentElementChange={() => {
setState({ status: "idle" });
}}
/>
);
};
const StripePaymentForm = (
props: Props & {
uid: string;
onPaymentSuccess?: (input: Omit<PaymentPageProps, "trpcState">) => void;
onPaymentCancellation?: (input: Omit<PaymentPageProps, "trpcState">) => void;
}
) => {
const stripePromise = getStripe(props.payment.data.stripe_publishable_key as any);
const [theme, setTheme] = useState<"stripe" | "night">("stripe");
useEffect(() => {
if (document.documentElement.classList.contains("dark")) {
setTheme("night");
}
}, []);
return (
<Elements
stripe={stripePromise}
options={{
clientSecret: props.clientSecret,
appearance: {
theme,
},
}}>
<StripePaymentComponent {...props} />
</Elements>
);
};
export default StripePaymentForm;
+1
View File
@@ -27,3 +27,4 @@ export type { UpdateScheduleInput_2024_06_11 as UpdateScheduleBody } from "@calc
export { EventTypePlatformWrapper as EventTypeSettings } from "./event-types/wrappers/EventTypePlatformWrapper";
export { StripeConnect } from "./connect/stripe/StripeConnect";
export { CreateEventTypePlatformWrapper as CreateEventType } from "./event-types/wrappers/CreateEventTypePlatformWrapper";
export { PaymentForm } from "./event-types/payments/PaymentForm";
@@ -93,7 +93,11 @@ export default function Bookings(props: { calUsername: string; calEmail: string
eventSlug={eventTypeSlug}
onCreateBookingSuccess={(data) => {
setBookingTitle(data.data.title ?? "");
router.push(`/${data.data.uid}`);
if (data.data.paymentRequired) {
router.push(`/payment/${data.data.paymentUid}`);
} else {
router.push(`/${data.data.uid}`);
}
}}
metadata={{ CustomKey: "CustomValue" }}
duration={eventTypeDuration}
@@ -0,0 +1,30 @@
import { Navbar } from "@/components/Navbar";
import { Inter } from "next/font/google";
import { usePathname } from "next/navigation";
import { useRouter } from "next/navigation";
import { PaymentForm } from "@calcom/atoms";
const inter = Inter({ subsets: ["latin"] });
export default function Payment(props: { calUsername: string; calEmail: string }) {
const pathname = usePathname();
const router = useRouter();
const uid = pathname.split("/").pop();
return (
<main className={`flex min-h-screen flex-col ${inter.className}`}>
<Navbar username={props.calUsername} />
<PaymentForm
paymentUid={uid ?? ""}
onPaymentSuccess={(data) => {
router.push("/bookings");
}}
onPaymentCancellation={(data) => {
router.back();
}}
/>
</main>
);
}
+5
View File
@@ -9,6 +9,7 @@ import { getBookingForReschedule } from "@calcom/features/bookings/lib/get-booki
import getBookingInfo from "@calcom/features/bookings/lib/getBookingInfo";
import handleCancelBooking from "@calcom/features/bookings/lib/handleCancelBooking";
import * as newBookingMethods from "@calcom/features/bookings/lib/handleNewBooking";
import { getClientSecretFromPayment } from "@calcom/features/ee/payments/pages/getClientSecretFromPayment";
import { getPublicEvent } from "@calcom/features/eventtypes/lib/getPublicEvent";
import { handleCreatePhoneCall } from "@calcom/features/handleCreatePhoneCall";
import handleMarkNoShow from "@calcom/features/handleMarkNoShow";
@@ -19,6 +20,7 @@ import { symmetricEncrypt, symmetricDecrypt } from "@calcom/lib/crypto";
import { getTranslation } from "@calcom/lib/server/i18n";
import { MembershipRole } from "@calcom/prisma/enums";
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
import { paymentDataSelect } from "@calcom/prisma/selects/payment";
import type { TeamQuery } from "@calcom/trpc/server/routers/loggedInViewer/integrations.handler";
import { updateHandler as updateScheduleHandler } from "@calcom/trpc/server/routers/viewer/availability/schedule/update.handler";
import { getAvailableSlots } from "@calcom/trpc/server/routers/viewer/slots/util";
@@ -175,3 +177,6 @@ export type { CredentialPayload };
export { getAppFromSlug };
export { credentialForCalendarServiceSelect };
export { MembershipRole };
export { paymentDataSelect };
export { getClientSecretFromPayment };