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
@@ -6,7 +6,7 @@ import z from "zod";
|
||||
import app_RoutingForms from "@calcom/app-store/ee/routing-forms/trpc-router";
|
||||
import ethRouter from "@calcom/app-store/rainbow/trpc/router";
|
||||
import { deleteStripeCustomer } from "@calcom/app-store/stripepayment/lib/customer";
|
||||
import stripe, { closePayments } from "@calcom/app-store/stripepayment/lib/server";
|
||||
import stripe from "@calcom/app-store/stripepayment/lib/server";
|
||||
import { getPremiumPlanProductId } from "@calcom/app-store/stripepayment/lib/utils";
|
||||
import getApps, { getLocationGroupedOptions } from "@calcom/app-store/utils";
|
||||
import { cancelScheduledJobs } from "@calcom/app-store/zapier/lib/nodeScheduler";
|
||||
@@ -20,8 +20,9 @@ import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib";
|
||||
import getEnabledApps from "@calcom/lib/apps/getEnabledApps";
|
||||
import { ErrorCode, verifyPassword } from "@calcom/lib/auth";
|
||||
import { symmetricDecrypt } from "@calcom/lib/crypto";
|
||||
import getStripeAppData from "@calcom/lib/getStripeAppData";
|
||||
import getPaymentAppData from "@calcom/lib/getPaymentAppData";
|
||||
import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
|
||||
import { deletePayment } from "@calcom/lib/payment/deletePayment";
|
||||
import { checkUsername } from "@calcom/lib/server/checkUsername";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { resizeBase64Image } from "@calcom/lib/server/resizeBase64Image";
|
||||
@@ -817,11 +818,14 @@ const loggedInViewerRouter = router({
|
||||
id: id,
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
include: {
|
||||
select: {
|
||||
key: true,
|
||||
appId: true,
|
||||
app: {
|
||||
select: {
|
||||
slug: true,
|
||||
categories: true,
|
||||
dirName: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -838,7 +842,11 @@ const loggedInViewerRouter = router({
|
||||
select: {
|
||||
id: true,
|
||||
locations: true,
|
||||
destinationCalendar: true,
|
||||
destinationCalendar: {
|
||||
include: {
|
||||
credential: true,
|
||||
},
|
||||
},
|
||||
price: true,
|
||||
currency: true,
|
||||
metadata: true,
|
||||
@@ -882,7 +890,7 @@ const loggedInViewerRouter = router({
|
||||
|
||||
// If it's a calendar, remove the destination calendar from the event type
|
||||
if (credential.app?.categories.includes(AppCategories.calendar)) {
|
||||
if (eventType.destinationCalendar?.integration === credential.type) {
|
||||
if (eventType.destinationCalendar?.credential?.appId === credential.appId) {
|
||||
const destinationCalendar = await prisma.destinationCalendar.findFirst({
|
||||
where: {
|
||||
id: eventType.destinationCalendar?.id,
|
||||
@@ -920,7 +928,7 @@ const loggedInViewerRouter = router({
|
||||
|
||||
const metadata = EventTypeMetaDataSchema.parse(eventType.metadata);
|
||||
|
||||
const stripeAppData = getStripeAppData({ ...eventType, metadata });
|
||||
const stripeAppData = getPaymentAppData({ ...eventType, metadata });
|
||||
|
||||
// If it's a payment, hide the event type and set the price to 0. Also cancel all pending bookings
|
||||
if (credential.app?.categories.includes(AppCategories.payment)) {
|
||||
@@ -1007,12 +1015,11 @@ const loggedInViewerRouter = router({
|
||||
});
|
||||
|
||||
for (const payment of booking.payment) {
|
||||
// Right now we only close payments on Stripe
|
||||
const stripeKeysSchema = z.object({
|
||||
stripe_user_id: z.string(),
|
||||
});
|
||||
const { stripe_user_id } = stripeKeysSchema.parse(credential.key);
|
||||
await closePayments(payment.externalId, stripe_user_id);
|
||||
try {
|
||||
await deletePayment(payment.id, credential);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
await prisma.payment.delete({
|
||||
where: {
|
||||
id: payment.id,
|
||||
|
||||
@@ -279,4 +279,45 @@ export const appsRouter = router({
|
||||
});
|
||||
return !!gCalPresent;
|
||||
}),
|
||||
updateAppCredentials: authedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
credentialId: z.number(),
|
||||
key: z.object({}).passthrough(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { user } = ctx;
|
||||
|
||||
const { key } = input;
|
||||
|
||||
// Find user credential
|
||||
const credential = await ctx.prisma.credential.findFirst({
|
||||
where: {
|
||||
id: input.credentialId,
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
// Check if credential exists
|
||||
if (!credential) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Could not find credential ${input.credentialId}`,
|
||||
});
|
||||
}
|
||||
|
||||
const updated = await ctx.prisma.credential.update({
|
||||
where: {
|
||||
id: credential.id,
|
||||
},
|
||||
data: {
|
||||
key: {
|
||||
...(credential.key as Prisma.JsonObject),
|
||||
...(key as Prisma.JsonObject),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return !!updated;
|
||||
}),
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
BookingReference,
|
||||
BookingStatus,
|
||||
EventType,
|
||||
MembershipRole,
|
||||
Prisma,
|
||||
SchedulingType,
|
||||
User,
|
||||
@@ -13,9 +14,9 @@ import {
|
||||
import type { TFunction } from "next-i18next";
|
||||
import { z } from "zod";
|
||||
|
||||
import appStore from "@calcom/app-store";
|
||||
import { getCalendar } from "@calcom/app-store/_utils/getCalendar";
|
||||
import { DailyLocationType } from "@calcom/app-store/locations";
|
||||
import { refund } from "@calcom/app-store/stripepayment/lib/server";
|
||||
import { scheduleTrigger } from "@calcom/app-store/zapier/lib/nodeScheduler";
|
||||
import EventManager from "@calcom/core/EventManager";
|
||||
import { CalendarEventBuilder } from "@calcom/core/builders/CalendarEvent/builder";
|
||||
@@ -319,7 +320,7 @@ export const bookingsRouter = router({
|
||||
|
||||
const recurringInfo = recurringInfoBasic.map(
|
||||
(
|
||||
info: typeof recurringInfoBasic[number]
|
||||
info: (typeof recurringInfoBasic)[number]
|
||||
): {
|
||||
recurringEventId: string | null;
|
||||
count: number;
|
||||
@@ -709,6 +710,8 @@ export const bookingsRouter = router({
|
||||
eventType: {
|
||||
select: {
|
||||
id: true,
|
||||
owner: true,
|
||||
teamId: true,
|
||||
recurringEvent: true,
|
||||
title: true,
|
||||
requiresConfirmation: true,
|
||||
@@ -1074,7 +1077,70 @@ export const bookingsRouter = router({
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await refund(booking, evt); // No payment integration for recurring events for v1
|
||||
const successPayment = booking.payment.find((payment) => payment.success);
|
||||
if (!successPayment) {
|
||||
throw new Error("Cannot reject a booking without a successful payment");
|
||||
}
|
||||
|
||||
let eventTypeOwnerId;
|
||||
if (booking.eventType?.owner) {
|
||||
eventTypeOwnerId = booking.eventType.owner.id;
|
||||
} else if (booking.eventType?.teamId) {
|
||||
const teamOwner = await prisma.membership.findFirst({
|
||||
where: {
|
||||
teamId: booking.eventType.teamId,
|
||||
role: MembershipRole.OWNER,
|
||||
},
|
||||
select: {
|
||||
userId: true,
|
||||
},
|
||||
});
|
||||
eventTypeOwnerId = teamOwner?.userId;
|
||||
}
|
||||
|
||||
if (!eventTypeOwnerId) {
|
||||
throw new Error("Event Type owner not found for obtaining payment app credentials");
|
||||
}
|
||||
|
||||
const paymentAppCredentials = await prisma.credential.findMany({
|
||||
where: {
|
||||
userId: eventTypeOwnerId,
|
||||
appId: successPayment.appId,
|
||||
},
|
||||
select: {
|
||||
key: true,
|
||||
appId: true,
|
||||
app: {
|
||||
select: {
|
||||
categories: true,
|
||||
dirName: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const paymentAppCredential = paymentAppCredentials.find((credential) => {
|
||||
return credential.appId === successPayment.appId;
|
||||
});
|
||||
|
||||
if (!paymentAppCredential) {
|
||||
throw new Error("Payment app credentials not found");
|
||||
}
|
||||
|
||||
// Posible to refactor TODO:
|
||||
const paymentApp = appStore[paymentAppCredential?.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(paymentAppCredential);
|
||||
const paymentData = await paymentInstance.refund(successPayment.id);
|
||||
if (!paymentData.refunded) {
|
||||
throw new Error("Payment could not be refunded");
|
||||
}
|
||||
|
||||
await prisma.booking.update({
|
||||
where: {
|
||||
id: bookingId,
|
||||
|
||||
Reference in new issue
Block a user