2023-09-28 12:03:01 +00:00
|
|
|
import type { Prisma } from "@prisma/client";
|
|
|
|
|
|
|
|
import EventManager from "@calcom/core/EventManager";
|
|
|
|
import { sendScheduledEmails } from "@calcom/emails";
|
2023-09-30 04:52:32 +00:00
|
|
|
import { doesBookingRequireConfirmation } from "@calcom/features/bookings/lib/doesBookingRequireConfirmation";
|
|
|
|
import { handleBookingRequested } from "@calcom/features/bookings/lib/handleBookingRequested";
|
2023-09-28 12:03:01 +00:00
|
|
|
import { handleConfirmation } from "@calcom/features/bookings/lib/handleConfirmation";
|
|
|
|
import { HttpError as HttpCode } from "@calcom/lib/http-error";
|
2023-09-30 04:52:32 +00:00
|
|
|
import { getBooking } from "@calcom/lib/payment/getBooking";
|
|
|
|
import prisma from "@calcom/prisma";
|
2023-09-28 12:03:01 +00:00
|
|
|
import { BookingStatus } from "@calcom/prisma/enums";
|
|
|
|
|
2023-09-30 04:52:32 +00:00
|
|
|
import logger from "../logger";
|
2023-09-28 12:03:01 +00:00
|
|
|
|
2023-10-17 19:00:48 +00:00
|
|
|
const log = logger.getSubLogger({ prefix: ["[handlePaymentSuccess]"] });
|
2023-09-28 12:03:01 +00:00
|
|
|
export async function handlePaymentSuccess(paymentId: number, bookingId: number) {
|
2023-09-30 04:52:32 +00:00
|
|
|
const { booking, user: userWithCredentials, evt, eventType } = await getBooking(bookingId);
|
2023-09-28 12:03:01 +00:00
|
|
|
|
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
2023-09-30 04:52:32 +00:00
|
|
|
const requiresConfirmation = doesBookingRequireConfirmation({
|
|
|
|
booking: {
|
|
|
|
...booking,
|
|
|
|
eventType,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (requiresConfirmation) {
|
2023-09-28 12:03:01 +00:00
|
|
|
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]);
|
2023-09-30 04:52:32 +00:00
|
|
|
if (!isConfirmed) {
|
|
|
|
if (!requiresConfirmation) {
|
|
|
|
await handleConfirmation({
|
|
|
|
user: userWithCredentials,
|
|
|
|
evt,
|
|
|
|
prisma,
|
|
|
|
bookingId: booking.id,
|
|
|
|
booking,
|
|
|
|
paid: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await handleBookingRequested({
|
|
|
|
evt,
|
|
|
|
booking,
|
|
|
|
});
|
|
|
|
log.debug(`handling booking request for eventId ${eventType.id}`);
|
|
|
|
}
|
2023-09-28 12:03:01 +00:00
|
|
|
} else {
|
|
|
|
await sendScheduledEmails({ ...evt });
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new HttpCode({
|
|
|
|
statusCode: 200,
|
|
|
|
message: `Booking with id '${booking.id}' was paid and confirmed.`,
|
|
|
|
});
|
|
|
|
}
|