Files
shafi-VM 761b780aa2 fix(bookings): refund all seat payments when cancelling a paid seated… (#29685)
* fix(bookings): refund all seat payments when cancelling a paid seated booking

A paid seated booking creates one Payment row per seat on the same bookingId (each seat's payment_intent.succeeded webhook flips only its own payment via handlePaymentSuccess). processPaymentRefund only refunded payment.find((p) => p.success) — the first successful payment — so fully cancelling a multi-seat paid booking with an auto-refund policy refunded a single seat and left the other attendees charged with no error surfaced.

Refund every successful, not-yet-refunded payment instead.

Tests: unit regression in processPaymentRefund.test.ts (all payments refunded, verified red before the fix); integration test in handleCancelBooking.test.ts proving a full cancel of a seated multi-payment booking hands all payments to the refund path.

* fix(bookings): make seat refunds resilient and drop dayjs in test

Address CodeRabbit review on #29685:

- processPaymentRefund now attempts every seat's refund independently and throws an aggregate error, so one provider failure no longer skips the remaining seats.
- Add a regression test for that resilience, and use native Date instead of dayjs for fixed-offset test timestamps.
2026-07-09 14:23:03 +05:30

105 lines
3.5 KiB
TypeScript

import { getPaymentAppData } from "@calcom/app-store/_utils/payments/getPaymentAppData";
import dayjs from "@calcom/dayjs";
import { handlePaymentRefund } from "@calcom/features/bookings/lib/payment/handlePaymentRefund";
import { RefundPolicy } from "@calcom/lib/payment/types";
import prisma from "@calcom/prisma";
import type { Payment, Prisma } from "@calcom/prisma/client";
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
export const processPaymentRefund = async ({
booking,
teamId,
}: {
booking: {
startTime: Date;
endTime: Date;
payment: Payment[];
eventType: {
owner?: {
id: number;
} | null;
metadata?: Prisma.JsonValue;
} | null;
};
teamId?: number | null;
}) => {
const { startTime, eventType, payment } = booking;
if (!teamId && !eventType?.owner) return;
// A paid seated booking creates one Payment per seat on the same bookingId, so refund every
// successful (not-yet-refunded) payment — not just the first — otherwise the other seats stay charged.
const successfulPayments = payment.filter((p) => p.success && !p.refunded);
const [successPayment] = successfulPayments;
if (!successPayment) return;
const eventTypeMetadata = EventTypeMetaDataSchema.parse(eventType?.metadata);
const appData = getPaymentAppData({
currency: successPayment.currency,
metadata: eventTypeMetadata,
price: successPayment.amount,
});
if (!appData?.refundPolicy || appData.refundPolicy === RefundPolicy.NEVER) return;
const credentialWhereClause: Prisma.CredentialFindManyArgs["where"] = {
appId: successPayment.appId,
};
if (eventType?.owner) {
credentialWhereClause.userId = eventType.owner.id;
} else if (teamId) {
credentialWhereClause.teamId = teamId;
}
const paymentAppCredentials = await prisma.credential.findMany({
where: credentialWhereClause,
select: {
key: true,
appId: true,
app: {
select: {
categories: true,
dirName: true,
},
},
},
});
const paymentAppCredential = paymentAppCredentials.find((credential) => {
return credential.appId === successPayment.appId;
});
if (!paymentAppCredential) return;
const { refundPolicy, refundCountCalendarDays, refundDaysCount } = appData;
//refundDaysCount would always be present in case DAYS is selected, but adding it in AND jut for type safety
if (refundPolicy === RefundPolicy.DAYS && refundDaysCount) {
const refundDeadline =
refundCountCalendarDays === true
? dayjs(startTime).subtract(refundDaysCount, "days")
: // businessDaysSubtract exists on extended dayjs instance, but ts is messing up
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
dayjs(startTime).businessDaysSubtract(refundDaysCount);
if (dayjs().isAfter(refundDeadline)) return;
}
// Attempt every refund independently so one provider failure doesn't leave the remaining seats
// charged, then surface an aggregate error for the caller to log/report.
const refundErrors: unknown[] = [];
for (const paymentToRefund of successfulPayments) {
try {
await handlePaymentRefund(paymentToRefund.id, paymentAppCredential);
} catch (error) {
refundErrors.push(error);
}
}
if (refundErrors.length) {
throw new Error(
`Failed to refund ${refundErrors.length} of ${successfulPayments.length} payment(s): ${refundErrors
.map((error) => (error instanceof Error ? error.message : String(error)))
.join("; ")}`
);
}
};