refactor: Move handlePayment to Features Package (#23670)
* mv * update imports
This commit is contained in:
1 parent
95f8046db9
commit
c4142ab4e7
3 files changed
+2
-2
No files matched your search
@@ -18,6 +18,7 @@ import { CalendarEventBuilder } from "@calcom/features/CalendarEventBuilder";
|
||||
import type { BookingDataSchemaGetter } from "@calcom/features/bookings/lib/dto/types";
|
||||
import type { CreateRegularBookingData, CreateBookingMeta } from "@calcom/features/bookings/lib/dto/types";
|
||||
import type { CheckBookingAndDurationLimitsService } from "@calcom/features/bookings/lib/handleNewBooking/checkBookingAndDurationLimits";
|
||||
import { handlePayment } from "@calcom/features/bookings/lib/handlePayment";
|
||||
import { handleWebhookTrigger } from "@calcom/features/bookings/lib/handleWebhookTrigger";
|
||||
import { isEventTypeLoggingEnabled } from "@calcom/features/bookings/lib/isEventTypeLoggingEnabled";
|
||||
import type { CacheService } from "@calcom/features/calendar-cache/lib/getShouldServeCache";
|
||||
@@ -57,7 +58,6 @@ import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import type { CheckBookingLimitsService } from "@calcom/lib/intervalLimits/server/checkBookingLimits";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { handlePayment } from "@calcom/lib/payment/handlePayment";
|
||||
import { getPiiFreeCalendarEvent, getPiiFreeEventType } from "@calcom/lib/piiFreeData";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
import type { AppCategories, Prisma } from "@prisma/client";
|
||||
|
||||
import { PaymentServiceMap } from "@calcom/app-store/payment.services.generated";
|
||||
import type { EventTypeAppsList } from "@calcom/app-store/utils";
|
||||
import type { CompleteEventType } from "@calcom/prisma/zod";
|
||||
import { eventTypeAppMetadataOptionalSchema } from "@calcom/prisma/zod-utils";
|
||||
import type { CalendarEvent } from "@calcom/types/Calendar";
|
||||
import type { IAbstractPaymentService } from "@calcom/types/PaymentService";
|
||||
|
||||
const isPaymentService = (x: unknown): x is { PaymentService: any } =>
|
||||
!!x && typeof x === "object" && "PaymentService" in x && typeof x.PaymentService === "function";
|
||||
|
||||
const isKeyOf = <T extends object>(obj: T, key: unknown): key is keyof T =>
|
||||
typeof key === "string" && key in obj;
|
||||
|
||||
const handlePayment = async ({
|
||||
evt,
|
||||
selectedEventType,
|
||||
paymentAppCredentials,
|
||||
booking,
|
||||
bookerName,
|
||||
bookerEmail,
|
||||
bookerPhoneNumber,
|
||||
isDryRun = false,
|
||||
}: {
|
||||
evt: CalendarEvent;
|
||||
selectedEventType: Pick<CompleteEventType, "metadata" | "title">;
|
||||
paymentAppCredentials: {
|
||||
key: Prisma.JsonValue;
|
||||
appId: EventTypeAppsList;
|
||||
app: {
|
||||
dirName: string;
|
||||
categories: AppCategories[];
|
||||
} | null;
|
||||
};
|
||||
booking: {
|
||||
user: { email: string | null; name: string | null; timeZone: string; username: string | null } | null;
|
||||
id: number;
|
||||
userId: number | null;
|
||||
startTime: { toISOString: () => string };
|
||||
uid: string;
|
||||
};
|
||||
bookerName: string;
|
||||
bookerEmail: string;
|
||||
bookerPhoneNumber?: string | null;
|
||||
isDryRun?: boolean;
|
||||
}) => {
|
||||
if (isDryRun) return null;
|
||||
const key = paymentAppCredentials?.app?.dirName;
|
||||
|
||||
const paymentAppImportFn = PaymentServiceMap[key as keyof typeof PaymentServiceMap];
|
||||
if (!paymentAppImportFn) {
|
||||
console.warn(`payment app not implemented for key: ${key}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const paymentAppModule = await paymentAppImportFn;
|
||||
if (!isPaymentService(paymentAppModule)) {
|
||||
console.warn(`payment App service not found for key: ${key}`);
|
||||
return null;
|
||||
}
|
||||
const PaymentService = paymentAppModule.PaymentService;
|
||||
const paymentInstance = new PaymentService(paymentAppCredentials) as IAbstractPaymentService;
|
||||
|
||||
const apps = eventTypeAppMetadataOptionalSchema.parse(selectedEventType?.metadata?.apps);
|
||||
const paymentOption = apps?.[paymentAppCredentials.appId].paymentOption || "ON_BOOKING";
|
||||
|
||||
let paymentData;
|
||||
if (paymentOption === "HOLD") {
|
||||
paymentData = await paymentInstance.collectCard(
|
||||
{
|
||||
amount: apps?.[paymentAppCredentials.appId].price,
|
||||
currency: apps?.[paymentAppCredentials.appId].currency,
|
||||
},
|
||||
booking.id,
|
||||
paymentOption,
|
||||
bookerEmail,
|
||||
bookerPhoneNumber
|
||||
);
|
||||
} else {
|
||||
paymentData = await paymentInstance.create(
|
||||
{
|
||||
amount: apps?.[paymentAppCredentials.appId].price,
|
||||
currency: apps?.[paymentAppCredentials.appId].currency,
|
||||
},
|
||||
booking.id,
|
||||
booking.userId,
|
||||
booking.user?.username ?? null,
|
||||
bookerName,
|
||||
paymentOption,
|
||||
bookerEmail,
|
||||
bookerPhoneNumber,
|
||||
selectedEventType.title,
|
||||
evt.title
|
||||
);
|
||||
}
|
||||
|
||||
if (!paymentData) {
|
||||
console.error("Payment data is null");
|
||||
throw new Error("Payment data is null");
|
||||
}
|
||||
try {
|
||||
await paymentInstance.afterPayment(evt, booking, paymentData, selectedEventType?.metadata);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
return paymentData;
|
||||
};
|
||||
|
||||
export { handlePayment };
|
||||
@@ -4,6 +4,7 @@ import { uuid } from "short-uuid";
|
||||
|
||||
import { sendScheduledSeatsEmailsAndSMS } from "@calcom/emails";
|
||||
import { refreshCredentials } from "@calcom/features/bookings/lib/getAllCredentialsForUsersOnEvent/refreshCredentials";
|
||||
import { handlePayment } from "@calcom/features/bookings/lib/handlePayment";
|
||||
import {
|
||||
allowDisablingAttendeeConfirmationEmails,
|
||||
allowDisablingHostConfirmationEmails,
|
||||
@@ -11,7 +12,6 @@ import {
|
||||
import EventManager from "@calcom/lib/EventManager";
|
||||
import { ErrorCode } from "@calcom/lib/errorCodes";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { handlePayment } from "@calcom/lib/payment/handlePayment";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { BookingStatus } from "@calcom/prisma/enums";
|
||||
import { eventTypeAppMetadataOptionalSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
Reference in new issue
Block a user