2021-09-22 18:36:13 +00:00
|
|
|
import { buffer } from "micro";
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
import Stripe from "stripe";
|
|
|
|
|
|
|
|
import stripe from "@ee/lib/stripe/server";
|
|
|
|
|
|
|
|
import { CalendarEvent } from "@lib/calendarClient";
|
2021-12-01 10:32:08 +00:00
|
|
|
import { IS_PRODUCTION } from "@lib/config/constants";
|
2021-09-28 13:00:19 +00:00
|
|
|
import { HttpError } from "@lib/core/http/error";
|
2021-10-20 15:42:40 +00:00
|
|
|
import { getErrorFromUnknown } from "@lib/errors";
|
2021-09-22 19:52:38 +00:00
|
|
|
import EventManager from "@lib/events/EventManager";
|
|
|
|
import prisma from "@lib/prisma";
|
2021-10-26 16:17:24 +00:00
|
|
|
import { Ensure } from "@lib/types/utils";
|
|
|
|
|
|
|
|
import { getTranslation } from "@server/lib/i18n";
|
2021-09-22 18:36:13 +00:00
|
|
|
|
|
|
|
export const config = {
|
|
|
|
api: {
|
|
|
|
bodyParser: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async function handlePaymentSuccess(event: Stripe.Event) {
|
|
|
|
const paymentIntent = event.data.object as Stripe.PaymentIntent;
|
|
|
|
const payment = await prisma.payment.update({
|
|
|
|
where: {
|
|
|
|
externalId: paymentIntent.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
success: true,
|
|
|
|
booking: {
|
|
|
|
update: {
|
|
|
|
paid: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
bookingId: true,
|
|
|
|
booking: {
|
|
|
|
select: {
|
|
|
|
title: true,
|
|
|
|
description: true,
|
|
|
|
startTime: true,
|
|
|
|
endTime: true,
|
|
|
|
confirmed: true,
|
|
|
|
attendees: true,
|
|
|
|
location: true,
|
|
|
|
userId: true,
|
|
|
|
id: true,
|
|
|
|
uid: true,
|
|
|
|
paid: true,
|
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
credentials: true,
|
|
|
|
timeZone: true,
|
|
|
|
email: true,
|
|
|
|
name: true,
|
2021-10-28 22:58:26 +00:00
|
|
|
locale: true,
|
2021-09-22 18:36:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!payment) throw new Error("No payment found");
|
|
|
|
|
|
|
|
const { booking } = payment;
|
|
|
|
|
|
|
|
if (!booking) throw new Error("No booking found");
|
|
|
|
|
|
|
|
const { user } = booking;
|
|
|
|
|
|
|
|
if (!user) throw new Error("No user found");
|
|
|
|
|
2021-10-28 22:58:26 +00:00
|
|
|
const t = await getTranslation(user.locale ?? "en", "common");
|
2021-10-26 16:17:24 +00:00
|
|
|
|
|
|
|
const evt: Ensure<CalendarEvent, "language"> = {
|
2021-09-22 18:36:13 +00:00
|
|
|
type: booking.title,
|
|
|
|
title: booking.title,
|
|
|
|
description: booking.description || undefined,
|
|
|
|
startTime: booking.startTime.toISOString(),
|
|
|
|
endTime: booking.endTime.toISOString(),
|
|
|
|
organizer: { email: user.email!, name: user.name!, timeZone: user.timeZone },
|
|
|
|
attendees: booking.attendees,
|
2021-10-26 16:17:24 +00:00
|
|
|
uid: booking.uid,
|
|
|
|
language: t,
|
2021-09-22 18:36:13 +00:00
|
|
|
};
|
2021-10-28 22:58:26 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
if (booking.location) evt.location = booking.location;
|
|
|
|
|
|
|
|
if (booking.confirmed) {
|
|
|
|
const eventManager = new EventManager(user.credentials);
|
2021-10-26 16:17:24 +00:00
|
|
|
const scheduleResult = await eventManager.create(evt);
|
2021-09-22 18:36:13 +00:00
|
|
|
|
|
|
|
await prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
id: payment.bookingId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
references: {
|
|
|
|
create: scheduleResult.referencesToCreate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 13:00:19 +00:00
|
|
|
type WebhookHandler = (event: Stripe.Event) => Promise<void>;
|
2021-09-22 18:36:13 +00:00
|
|
|
|
2021-09-28 13:00:19 +00:00
|
|
|
const webhookHandlers: Record<string, WebhookHandler | undefined> = {
|
|
|
|
"payment_intent.succeeded": handlePaymentSuccess,
|
|
|
|
};
|
2021-09-22 18:36:13 +00:00
|
|
|
|
2021-09-28 13:00:19 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-09-22 18:36:13 +00:00
|
|
|
try {
|
2021-09-28 13:00:19 +00:00
|
|
|
if (req.method !== "POST") {
|
|
|
|
throw new HttpError({ statusCode: 405, message: "Method Not Allowed" });
|
|
|
|
}
|
|
|
|
const sig = req.headers["stripe-signature"];
|
|
|
|
if (!sig) {
|
|
|
|
throw new HttpError({ statusCode: 400, message: "Missing stripe-signature" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!process.env.STRIPE_WEBHOOK_SECRET) {
|
|
|
|
throw new HttpError({ statusCode: 500, message: "Missing process.env.STRIPE_WEBHOOK_SECRET" });
|
|
|
|
}
|
|
|
|
const requestBuffer = await buffer(req);
|
|
|
|
const payload = requestBuffer.toString();
|
|
|
|
|
|
|
|
const event = stripe.webhooks.constructEvent(payload, sig, process.env.STRIPE_WEBHOOK_SECRET);
|
2021-09-22 18:36:13 +00:00
|
|
|
|
2021-09-28 13:00:19 +00:00
|
|
|
const handler = webhookHandlers[event.type];
|
|
|
|
if (handler) {
|
|
|
|
await handler(event);
|
2021-09-22 18:36:13 +00:00
|
|
|
} else {
|
2021-11-30 23:50:49 +00:00
|
|
|
/** Not really an error, just letting Stripe know that the webhook was received but unhandled */
|
|
|
|
throw new HttpError({
|
|
|
|
statusCode: 202,
|
|
|
|
message: `Unhandled Stripe Webhook event type ${event.type}`,
|
|
|
|
});
|
2021-09-22 18:36:13 +00:00
|
|
|
}
|
|
|
|
} catch (_err) {
|
|
|
|
const err = getErrorFromUnknown(_err);
|
|
|
|
console.error(`Webhook Error: ${err.message}`);
|
|
|
|
res.status(err.statusCode ?? 500).send({
|
|
|
|
message: err.message,
|
2021-12-01 10:32:08 +00:00
|
|
|
stack: IS_PRODUCTION ? undefined : err.stack,
|
2021-09-22 18:36:13 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a response to acknowledge receipt of the event
|
|
|
|
res.json({ received: true });
|
|
|
|
}
|