fix: booking_paid webhook and added new payment metadata (#11093)

This commit is contained in:
alannnc 2023-09-06 12:40:27 -07:00 committed by GitHub
parent f9eb335d0b
commit b81b221bf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 4 deletions

View File

@ -338,7 +338,67 @@ export async function handleConfirmation(args: {
);
})
);
await Promise.all(promises);
if (paid) {
let paymentExternalId: string | undefined;
const subscriberMeetingPaid = await getWebhooks({
userId: triggerForUser ? booking.userId : null,
eventTypeId: booking.eventTypeId,
triggerEvent: WebhookTriggerEvents.BOOKING_PAID,
teamId: booking.eventType?.teamId,
});
const bookingWithPayment = await prisma.booking.findFirst({
where: {
id: bookingId,
},
select: {
payment: {
select: {
id: true,
success: true,
externalId: true,
},
},
},
});
const successPayment = bookingWithPayment?.payment?.find((item) => item.success);
if (successPayment) {
paymentExternalId = successPayment.externalId;
}
const paymentMetadata = {
identifier: "cal.com",
bookingId,
eventTypeId: booking.eventType?.id,
bookerEmail: evt.attendees[0].email,
eventTitle: booking.eventType?.title,
externalId: paymentExternalId,
};
const bookingPaidSubscribers = subscriberMeetingPaid.map((sub) =>
sendPayload(sub.secret, WebhookTriggerEvents.BOOKING_PAID, new Date().toISOString(), sub, {
...evt,
...eventTypeInfo,
bookingId,
eventTypeId: booking.eventType?.id,
status: "ACCEPTED",
smsReminderNumber: booking.smsReminderNumber || undefined,
paymentId: bookingWithPayment?.payment?.[0].id,
metadata: {
...(paid ? paymentMetadata : {}),
},
}).catch((e) => {
console.error(
`Error executing webhook for event: ${WebhookTriggerEvents.BOOKING_PAID}, URL: ${sub.subscriberUrl}`,
e
);
})
);
// I don't need to await for this
Promise.all(bookingPaidSubscribers);
}
} catch (error) {
// Silently fail
console.error(error);

View File

@ -18,7 +18,7 @@ export type EventTypeInfo = {
export type WebhookDataType = CalendarEvent &
EventTypeInfo & {
metadata?: { [key: string]: string };
metadata?: { [key: string]: string | number | boolean | null };
bookingId?: number;
status?: string;
smsReminderNumber?: string;
@ -28,6 +28,7 @@ export type WebhookDataType = CalendarEvent &
triggerEvent: string;
createdAt: string;
downloadLink?: string;
paymentId?: number;
};
function getZapierPayload(

View File

@ -241,13 +241,13 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
}
}
if (input?.price || input.metadata?.apps?.stripe?.price) {
data.price = input.price || input.metadata?.apps?.stripe?.price;
if (input.metadata?.apps?.stripe?.price) {
data.price = input.metadata?.apps?.stripe?.price;
const paymentCredential = await ctx.prisma.credential.findFirst({
where: {
userId: ctx.user.id,
type: {
contains: "_payment",
equals: "stripe_payment",
},
},
select: {
@ -260,6 +260,22 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
const { default_currency } = stripeDataSchema.parse(paymentCredential.key);
data.currency = default_currency;
}
}
if (input.metadata?.apps?.paypal?.price) {
data.price = input.metadata?.apps?.paypal?.price;
const paymentCredential = await ctx.prisma.credential.findFirst({
where: {
userId: ctx.user.id,
type: {
equals: "paypal_payment",
},
},
select: {
type: true,
key: true,
},
});
if (paymentCredential?.type === "paypal_payment" && input.metadata?.apps?.paypal?.currency) {
data.currency = input.metadata?.apps?.paypal?.currency.toLowerCase();
}