import type { Prisma } from "@prisma/client"; import EventManager from "@calcom/core/EventManager"; import { sendScheduledEmails } from "@calcom/emails"; import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses"; import { handleConfirmation } from "@calcom/features/bookings/lib/handleConfirmation"; import { isPrismaObjOrUndefined, parseRecurringEvent } from "@calcom/lib"; import { HttpError as HttpCode } from "@calcom/lib/http-error"; import { getTranslation } from "@calcom/lib/server/i18n"; import prisma, { bookingMinimalSelect } from "@calcom/prisma"; import { BookingStatus } from "@calcom/prisma/enums"; import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential"; import type { CalendarEvent } from "@calcom/types/Calendar"; import { getTimeFormatStringFromUserTimeFormat } from "../timeFormat"; export async function handlePaymentSuccess(paymentId: number, bookingId: number) { const booking = await prisma.booking.findUnique({ where: { id: bookingId, }, select: { ...bookingMinimalSelect, eventType: true, smsReminderNumber: true, location: true, eventTypeId: true, userId: true, uid: true, paid: true, destinationCalendar: true, status: true, responses: true, user: { select: { id: true, username: true, credentials: { select: credentialForCalendarServiceSelect }, timeZone: true, timeFormat: true, email: true, name: true, locale: true, destinationCalendar: true, }, }, payment: { select: { amount: true, currency: true, paymentOption: true, }, }, }, }); if (!booking) throw new HttpCode({ statusCode: 204, message: "No booking found" }); type EventTypeRaw = Awaited>; let eventTypeRaw: EventTypeRaw | null = null; if (booking.eventTypeId) { eventTypeRaw = await getEventType(booking.eventTypeId); } const { user: userWithCredentials } = booking; if (!userWithCredentials) throw new HttpCode({ statusCode: 204, message: "No user found" }); const { credentials, ...user } = userWithCredentials; const t = await getTranslation(user.locale ?? "en", "common"); const attendeesListPromises = booking.attendees.map(async (attendee) => { return { name: attendee.name, email: attendee.email, timeZone: attendee.timeZone, language: { translate: await getTranslation(attendee.locale ?? "en", "common"), locale: attendee.locale ?? "en", }, }; }); const attendeesList = await Promise.all(attendeesListPromises); const selectedDestinationCalendar = booking.destinationCalendar || user.destinationCalendar; const evt: CalendarEvent = { type: booking.title, title: booking.title, description: booking.description || undefined, startTime: booking.startTime.toISOString(), endTime: booking.endTime.toISOString(), customInputs: isPrismaObjOrUndefined(booking.customInputs), ...getCalEventResponses({ booking: booking, bookingFields: booking.eventType?.bookingFields || null, }), organizer: { email: user.email, name: user.name!, timeZone: user.timeZone, timeFormat: getTimeFormatStringFromUserTimeFormat(user.timeFormat), language: { translate: t, locale: user.locale ?? "en" }, }, attendees: attendeesList, location: booking.location, uid: booking.uid, destinationCalendar: selectedDestinationCalendar ? [selectedDestinationCalendar] : [], recurringEvent: parseRecurringEvent(eventTypeRaw?.recurringEvent), paymentInfo: booking.payment?.[0] && { amount: booking.payment[0].amount, currency: booking.payment[0].currency, paymentOption: booking.payment[0].paymentOption, }, }; if (booking.location) evt.location = booking.location; const bookingData: Prisma.BookingUpdateInput = { paid: true, status: BookingStatus.ACCEPTED, }; const isConfirmed = booking.status === BookingStatus.ACCEPTED; if (isConfirmed) { const eventManager = new EventManager(userWithCredentials); const scheduleResult = await eventManager.create(evt); bookingData.references = { create: scheduleResult.referencesToCreate }; } if (eventTypeRaw?.requiresConfirmation) { delete bookingData.status; } const paymentUpdate = prisma.payment.update({ where: { id: paymentId, }, data: { success: true, }, }); const bookingUpdate = prisma.booking.update({ where: { id: booking.id, }, data: bookingData, }); await prisma.$transaction([paymentUpdate, bookingUpdate]); if (!isConfirmed && !eventTypeRaw?.requiresConfirmation) { await handleConfirmation({ user: userWithCredentials, evt, prisma, bookingId: booking.id, booking, paid: true, }); } else { await sendScheduledEmails({ ...evt }); } throw new HttpCode({ statusCode: 200, message: `Booking with id '${booking.id}' was paid and confirmed.`, }); } async function getEventType(id: number) { return prisma.eventType.findUnique({ where: { id, }, select: { recurringEvent: true, requiresConfirmation: true, }, }); }