Attempt to fix Stripe webhooks (#2355)

Split the large transaction so we can debug better what's causing the error from Stripe dashboard
pull/2361/head^2
Omar López 2022-04-04 14:39:59 -06:00 committed by GitHub
parent a7f5250b4a
commit ffff59dd00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 55 additions and 47 deletions

View File

@ -1,3 +1,4 @@
import { Prisma } from "@prisma/client";
import { buffer } from "micro"; import { buffer } from "micro";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import Stripe from "stripe"; import Stripe from "stripe";
@ -22,55 +23,49 @@ export const config = {
async function handlePaymentSuccess(event: Stripe.Event) { async function handlePaymentSuccess(event: Stripe.Event) {
const paymentIntent = event.data.object as Stripe.PaymentIntent; const paymentIntent = event.data.object as Stripe.PaymentIntent;
const payment = await prisma.payment.update({ const payment = await prisma.payment.findFirst({
where: { where: {
externalId: paymentIntent.id, externalId: paymentIntent.id,
}, },
data: { select: {
success: true, id: true,
booking: { bookingId: true,
update: { },
paid: true, });
confirmed: true,
}, if (!payment?.bookingId) throw new Error("Payment not found");
},
const booking = await prisma.booking.findUnique({
where: {
id: payment.bookingId,
}, },
select: { select: {
bookingId: true, title: true,
booking: { description: true,
startTime: true,
endTime: true,
confirmed: true,
attendees: true,
location: true,
userId: true,
id: true,
uid: true,
paid: true,
destinationCalendar: true,
user: {
select: { select: {
title: true,
description: true,
startTime: true,
endTime: true,
confirmed: true,
attendees: true,
location: true,
userId: true,
id: true, id: true,
uid: true, credentials: true,
paid: true, timeZone: true,
email: true,
name: true,
locale: true,
destinationCalendar: true, destinationCalendar: true,
user: {
select: {
id: true,
credentials: true,
timeZone: true,
email: true,
name: true,
locale: true,
destinationCalendar: true,
},
},
}, },
}, },
}, },
}); });
if (!payment) throw new Error("No payment found");
const { booking } = payment;
if (!booking) throw new Error("No booking found"); if (!booking) throw new Error("No booking found");
const { user } = booking; const { user } = booking;
@ -111,22 +106,35 @@ async function handlePaymentSuccess(event: Stripe.Event) {
if (booking.location) evt.location = booking.location; if (booking.location) evt.location = booking.location;
let bookingData: Prisma.BookingUpdateInput = {
paid: true,
confirmed: true,
};
if (booking.confirmed) { if (booking.confirmed) {
const eventManager = new EventManager(user); const eventManager = new EventManager(user);
const scheduleResult = await eventManager.create(evt); const scheduleResult = await eventManager.create(evt);
bookingData.references = { create: scheduleResult.referencesToCreate };
await prisma.booking.update({
where: {
id: booking.id,
},
data: {
references: {
create: scheduleResult.referencesToCreate,
},
},
});
} }
const paymentUpdate = prisma.payment.update({
where: {
id: payment.id,
},
data: {
success: true,
},
});
const bookingUpdate = prisma.booking.update({
where: {
id: booking.id,
},
data: bookingData,
});
await prisma.$transaction([paymentUpdate, bookingUpdate]);
await sendScheduledEmails({ ...evt }); await sendScheduledEmails({ ...evt });
throw new HttpCode({ throw new HttpCode({