Files
calendar/packages/lib/payment/handlePayment.ts
T
d6fb0df64f perf: tRPC procedures and middleware refactor (#8419)
* trpc procedures an middleware refactor

* allow use sessionMiddleware without a req object

* sync with the new tRPC structure

* tRPC refactor on routing form app

* import Prisma from @prisma/client

* Lazy load apps from appstore

* remove unrelated changes

* Add types for PaymentService

* type fixes

* Merge branch 'main' into roae85/cal-1514-set-the-user-session-only-on-the

* fix typo

* remove console.log

* remove explicit types from apstore object

* linter fixes

---------

Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
2023-05-09 19:27:05 +00:00

77 lines
2.4 KiB
TypeScript

import type { AppCategories, Prisma } from "@prisma/client";
import appStore from "@calcom/app-store";
import type { EventTypeAppsList } from "@calcom/app-store/utils";
import type { EventTypeModel } from "@calcom/prisma/zod";
import type { CalendarEvent } from "@calcom/types/Calendar";
import type { IAbstractPaymentService } from "@calcom/types/PaymentService";
const handlePayment = async (
evt: CalendarEvent,
selectedEventType: Pick<Zod.infer<typeof EventTypeModel>, "metadata">,
paymentAppCredentials: {
key: Prisma.JsonValue;
appId: EventTypeAppsList;
app: {
dirName: string;
categories: AppCategories[];
} | null;
},
booking: {
user: { email: string | null; name: string | null; timeZone: string } | null;
id: number;
startTime: { toISOString: () => string };
uid: string;
},
bookerEmail: string
) => {
const paymentApp = await appStore[paymentAppCredentials?.app?.dirName as keyof typeof appStore]();
if (!(paymentApp && "lib" in paymentApp && "PaymentService" in paymentApp.lib)) {
console.warn(`payment App service of type ${paymentApp} is not implemented`);
return null;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const PaymentService = paymentApp.lib.PaymentService as any;
const paymentInstance = new PaymentService(paymentAppCredentials) as IAbstractPaymentService;
const paymentOption =
selectedEventType?.metadata?.apps?.[paymentAppCredentials.appId].paymentOption || "ON_BOOKING";
let paymentData;
if (paymentOption === "HOLD") {
paymentData = await paymentInstance.collectCard(
{
amount: selectedEventType?.metadata?.apps?.[paymentAppCredentials.appId].price,
currency: selectedEventType?.metadata?.apps?.[paymentAppCredentials.appId].currency,
},
booking.id,
bookerEmail,
paymentOption
);
} else {
paymentData = await paymentInstance.create(
{
amount: selectedEventType?.metadata?.apps?.[paymentAppCredentials.appId].price,
currency: selectedEventType?.metadata?.apps?.[paymentAppCredentials.appId].currency,
},
booking.id,
bookerEmail,
paymentOption
);
}
if (!paymentData) {
console.error("Payment data is null");
throw new Error("Payment data is null");
}
try {
await paymentInstance.afterPayment(evt, booking, paymentData);
} catch (e) {
console.error(e);
}
return paymentData;
};
export { handlePayment };