feat/payment-service-6438-cal-767 (#6677)
* WIP paymentService * Changes for payment Service * Fix for stripe payment flow * Remove logs/comments * Refactored refund for stripe app * Move stripe handlePayment to own lib * Move stripe delete payments to paymentService * lint fix * Change handleRefundError as generic function * remove log * remove logs * remove logs * Return stripe default export to lib/server * Fixing types * Fix types * Upgrades typescript * Update yarn lock * Typings * Hotfix: ping,riverside,whereby and around not showing up in list (#6712) * Hotfix: ping,riverside,whereby and around not showing up in list (#6712) (#6713) * Adds deployment settings to DB (#6706) * WIP * Adds DeploymentTheme * Add missing migrations * Adds client extensions for deployment * Cleanup * Revert "lint fix" This reverts commit e1a2e4a357e58e6673c47399888ae2e00d1351a6. * Add validation * Revert changes removed in force push * Removing abstract class and just leaving interface implementation * Fix types for handlePayments * Fix payment test appStore import * Fix stripe metadata in event type * Move migration to separate PR * Revert "Move migration to separate PR" This reverts commit 48aa64e0724a522d3cc2fefaaaee5792ee9cd9e6. * Update packages/prisma/migrations/20230125175109_remove_type_from_payment_and_add_app_relationship/migration.sql Co-authored-by: Omar López <[email protected]> --------- Co-authored-by: zomars <[email protected]> Co-authored-by: Hariom Balhara <[email protected]>
This commit is contained in:
30 files changed
+761
-273
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import { Payment, Prisma, Booking } from "@prisma/client";
|
||||
|
||||
import { CalendarEvent } from "@calcom/types/Calendar";
|
||||
|
||||
export interface IAbstractPaymentService {
|
||||
create(
|
||||
payment: Pick<Prisma.PaymentUncheckedCreateInput, "amount" | "currency">,
|
||||
bookingId: Booking["id"]
|
||||
): Promise<Payment>;
|
||||
update(paymentId: Payment["id"], data: Partial<Prisma.PaymentUncheckedCreateInput>): Promise<Payment>;
|
||||
refund(paymentId: Payment["id"]): Promise<Payment>;
|
||||
getPaymentPaidStatus(): Promise<string>;
|
||||
getPaymentDetails(): Promise<Payment>;
|
||||
afterPayment(
|
||||
event: CalendarEvent,
|
||||
booking: {
|
||||
user: { email: string | null; name: string | null; timeZone: string } | null;
|
||||
id: number;
|
||||
startTime: { toISOString: () => string };
|
||||
uid: string;
|
||||
},
|
||||
paymentData: Payment
|
||||
): Promise<void>;
|
||||
deletePayment(paymentId: Payment["id"]): Promise<boolean>;
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import { LocationObject } from "@calcom/core/location";
|
||||
import { parseBookingLimit, parseRecurringEvent } from "@calcom/lib";
|
||||
import getEnabledApps from "@calcom/lib/apps/getEnabledApps";
|
||||
import { CAL_URL } from "@calcom/lib/constants";
|
||||
import getStripeAppData from "@calcom/lib/getStripeAppData";
|
||||
import getPaymentAppData from "@calcom/lib/getPaymentAppData";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { customInputSchema, EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
@@ -201,7 +201,7 @@ export default async function getEventTypeById({
|
||||
newMetadata.apps = {
|
||||
...apps,
|
||||
stripe: {
|
||||
...getStripeAppData(eventTypeWithParsedMetadata, true),
|
||||
...getPaymentAppData(eventTypeWithParsedMetadata, true),
|
||||
currency:
|
||||
(
|
||||
credentials.find((integration) => integration.type === "stripe_payment")
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { appDataSchemas } from "@calcom/app-store/apps.schemas.generated";
|
||||
import { appDataSchema } from "@calcom/app-store/stripepayment/zod";
|
||||
import { EventTypeAppsList, getEventTypeAppData } from "@calcom/app-store/utils";
|
||||
|
||||
export default function getPaymentAppData(
|
||||
eventType: Parameters<typeof getEventTypeAppData>[0],
|
||||
forcedGet?: boolean
|
||||
) {
|
||||
const metadataApps = eventType?.metadata?.apps as unknown as EventTypeAppsList;
|
||||
if (!metadataApps) {
|
||||
return { enabled: false, price: 0, currency: "usd", appId: null };
|
||||
}
|
||||
type appId = keyof typeof metadataApps;
|
||||
// @TODO: a lot of unknowns types here can be improved later
|
||||
const paymentAppIds = (Object.keys(metadataApps) as Array<keyof typeof appDataSchemas>).filter(
|
||||
(app) =>
|
||||
(metadataApps[app as appId] as unknown as z.infer<typeof appDataSchema>)?.price &&
|
||||
(metadataApps[app as appId] as unknown as z.infer<typeof appDataSchema>)?.enabled
|
||||
);
|
||||
|
||||
// Event type should only have one payment app data
|
||||
let paymentAppData: {
|
||||
enabled: boolean;
|
||||
price: number;
|
||||
currency: string;
|
||||
appId: EventTypeAppsList | null;
|
||||
} | null = null;
|
||||
for (const appId of paymentAppIds) {
|
||||
const appData = getEventTypeAppData(eventType, appId, forcedGet);
|
||||
if (appData && paymentAppData === null) {
|
||||
paymentAppData = {
|
||||
...appData,
|
||||
appId,
|
||||
};
|
||||
}
|
||||
}
|
||||
// This is the current expectation of system to have price and currency set always(using DB Level defaults).
|
||||
// Newly added apps code should assume that their app data might not be set.
|
||||
return paymentAppData || { enabled: false, price: 0, currency: "usd", appId: null };
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { getEventTypeAppData } from "@calcom/app-store/utils";
|
||||
|
||||
export default function getStripeAppData(
|
||||
eventType: Parameters<typeof getEventTypeAppData>[0],
|
||||
forcedGet?: boolean
|
||||
) {
|
||||
const stripeAppData = getEventTypeAppData(eventType, "stripe", forcedGet);
|
||||
// This is the current expectation of system to have price and currency set always(using DB Level defaults).
|
||||
// Newly added apps code should assume that their app data might not be set.
|
||||
return stripeAppData || { enabled: false, price: 0, currency: "usd" };
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import appStore from "@calcom/app-store";
|
||||
import { EventTypeAppsList } from "@calcom/app-store/utils";
|
||||
|
||||
import { AppCategories, Payment, Prisma } from ".prisma/client";
|
||||
|
||||
const deletePayment = async (
|
||||
paymentId: Payment["id"],
|
||||
paymentAppCredentials: {
|
||||
key: Prisma.JsonValue;
|
||||
appId: string | null;
|
||||
app: {
|
||||
dirName: string;
|
||||
categories: AppCategories[];
|
||||
} | null;
|
||||
}
|
||||
): Promise<boolean> => {
|
||||
const paymentApp = 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 false;
|
||||
}
|
||||
const PaymentService = paymentApp.lib.PaymentService;
|
||||
const paymentInstance = new PaymentService(paymentAppCredentials);
|
||||
const deleted = await paymentInstance.deletePayment(paymentId);
|
||||
return deleted;
|
||||
};
|
||||
|
||||
export { deletePayment };
|
||||
@@ -0,0 +1,53 @@
|
||||
import appStore from "@calcom/app-store";
|
||||
import { EventTypeAppsList } from "@calcom/app-store/utils";
|
||||
import { EventTypeModel } from "@calcom/prisma/zod";
|
||||
import { CalendarEvent } from "@calcom/types/Calendar";
|
||||
|
||||
import { AppCategories, Prisma } from ".prisma/client";
|
||||
|
||||
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;
|
||||
}
|
||||
) => {
|
||||
const paymentApp = 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;
|
||||
}
|
||||
const PaymentService = paymentApp.lib.PaymentService;
|
||||
const paymentInstance = new PaymentService(paymentAppCredentials);
|
||||
const paymentData = await paymentInstance.create(
|
||||
{
|
||||
amount: selectedEventType?.metadata?.apps?.[paymentAppCredentials.appId].price,
|
||||
currency: selectedEventType?.metadata?.apps?.[paymentAppCredentials.appId].currency,
|
||||
},
|
||||
booking.id
|
||||
);
|
||||
|
||||
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 };
|
||||
@@ -0,0 +1,16 @@
|
||||
import { sendOrganizerPaymentRefundFailedEmail } from "@calcom/emails";
|
||||
import { CalendarEvent } from "@calcom/types/Calendar";
|
||||
|
||||
const handleRefundError = async (opts: { event: CalendarEvent; reason: string; paymentId: string }) => {
|
||||
console.error(`refund failed: ${opts.reason} for booking '${opts.event.uid}'`);
|
||||
try {
|
||||
await sendOrganizerPaymentRefundFailedEmail({
|
||||
...opts.event,
|
||||
paymentInfo: { reason: opts.reason, id: opts.paymentId },
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
};
|
||||
|
||||
export { handleRefundError };
|
||||
Reference in new issue
Block a user