feat: Write same metadata to Stripe for booking payment and no show fee payment (#23634)

* Add `findBookingById` to `BookingRepository`

* Create `generateMetadata` to Stripe `PaymentService`

* `create` method to use `generateMetadata`

* Remove unused trpc endpoint

* Pass booking id to chargeCard method

* Use generateMetadata in chargeCard method

* Remove unused stripeAppKeys

* Add error handling if booking doesn't contain attendee

* Rename  to

* Return attendees in a consistent order

---------

Co-authored-by: Hariom Balhara <[email protected]>
This commit is contained in:
Joe Au-YeungandHariom Balhara authored and GitHub committed 2025-09-15 14:20:05 +05:30
1 parent 7e876e3457
commit 31270ca86b
3 files changed
+94 -243

No files matched your search

@@ -8,6 +8,7 @@ import { getErrorFromUnknown } from "@calcom/lib/errors";
import { ErrorWithCode } from "@calcom/lib/errors";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import { BookingRepository } from "@calcom/lib/server/repository/booking";
import prisma from "@calcom/prisma";
import type { Booking, Payment, PaymentOption, Prisma } from "@calcom/prisma/client";
import type { EventTypeMetadata } from "@calcom/prisma/zod-utils";
@@ -86,17 +87,16 @@ export class PaymentService implements IAbstractPaymentService {
automatic_payment_methods: {
enabled: true,
},
metadata: {
identifier: "cal.com",
metadata: this.generateMetadata({
bookingId,
calAccountId: userId,
calUsername: username,
userId,
username,
bookerName,
bookerEmail: bookerEmail,
bookerPhoneNumber: bookerPhoneNumber ?? null,
eventTitle: eventTitle || "",
bookingTitle: bookingTitle || "",
},
}),
};
const paymentIntent = await this.stripe.paymentIntents.create(params, {
@@ -217,20 +217,18 @@ export class PaymentService implements IAbstractPaymentService {
}
}
async chargeCard(payment: Payment, _bookingId?: Booking["id"]): Promise<Payment> {
async chargeCard(payment: Payment, bookingId: Booking["id"]): Promise<Payment> {
try {
if (!this.credentials) {
throw new Error("Stripe credentials not found");
}
const stripeAppKeys = await prisma.app.findFirst({
select: {
keys: true,
},
where: {
slug: "stripe",
},
});
const bookingRepository = new BookingRepository(prisma);
const booking = await bookingRepository.findByIdIncludeUserAndAttendees(bookingId);
if (!booking) {
throw new Error(`Booking ${bookingId} not found`);
}
const paymentObject = payment.data as unknown as StripeSetupIntentData;
@@ -252,6 +250,10 @@ export class PaymentService implements IAbstractPaymentService {
throw new Error(`Stripe paymentMethod does not exist for setupIntent ${setupIntent.id}`);
}
if (!booking.attendees[0]) {
throw new Error(`Booking attendees are empty for setupIntent ${setupIntent.id}`);
}
const params: Stripe.PaymentIntentCreateParams = {
amount: payment.amount,
currency: payment.currency,
@@ -259,6 +261,16 @@ export class PaymentService implements IAbstractPaymentService {
payment_method: setupIntent.payment_method as string,
off_session: true,
confirm: true,
metadata: this.generateMetadata({
bookingId,
userId: booking.user?.id,
username: booking.user?.username,
bookerName: booking.attendees[0].name,
bookerEmail: booking.attendees[0].email,
bookerPhoneNumber: booking.attendees[0].phoneNumber ?? null,
eventTitle: booking.eventType?.title || null,
bookingTitle: booking.title,
}),
};
const paymentIntent = await this.stripe.paymentIntents.create(params, {
@@ -284,7 +296,7 @@ export class PaymentService implements IAbstractPaymentService {
return paymentData;
} catch (error) {
log.error("Stripe: Could not charge card for payment", _bookingId, safeStringify(error));
log.error("Stripe: Could not charge card for payment", bookingId, safeStringify(error));
const errorMappings = {
"your card was declined": "your_card_was_declined",
@@ -422,4 +434,36 @@ export class PaymentService implements IAbstractPaymentService {
isSetupAlready(): boolean {
return !!this.credentials;
}
private generateMetadata({
bookingId,
userId,
username,
bookerName,
bookerEmail,
bookerPhoneNumber,
eventTitle,
bookingTitle,
}: {
bookingId: number;
userId: number | null | undefined;
username: string | null | undefined;
bookerName: string;
bookerEmail: string;
bookerPhoneNumber: string | null;
eventTitle: string | null;
bookingTitle: string;
}) {
return {
identifier: "cal.com",
bookingId,
calAccountId: userId ?? null,
calUsername: username ?? null,
bookerName,
bookerEmail: bookerEmail,
bookerPhoneNumber: bookerPhoneNumber ?? null,
eventTitle: eventTitle || "",
bookingTitle: bookingTitle || "",
};
}
}