2022-09-08 00:38:37 +00:00
|
|
|
import stripe from "@calcom/app-store/stripepayment/lib/server";
|
|
|
|
|
|
|
|
export async function getCustomerAndCheckoutSession(checkoutSessionId: string) {
|
|
|
|
const checkoutSession = await stripe.checkout.sessions.retrieve(checkoutSessionId);
|
|
|
|
const customerOrCustomerId = checkoutSession.customer;
|
|
|
|
let customerId = null;
|
|
|
|
|
|
|
|
if (!customerOrCustomerId) {
|
2022-12-07 19:55:47 +00:00
|
|
|
return { checkoutSession, stripeCustomer: null };
|
2022-09-08 00:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof customerOrCustomerId === "string") {
|
|
|
|
customerId = customerOrCustomerId;
|
|
|
|
} else if (customerOrCustomerId.deleted) {
|
2022-12-07 19:55:47 +00:00
|
|
|
return { checkoutSession, stripeCustomer: null };
|
2022-09-08 00:38:37 +00:00
|
|
|
} else {
|
|
|
|
customerId = customerOrCustomerId.id;
|
|
|
|
}
|
|
|
|
const stripeCustomer = await stripe.customers.retrieve(customerId);
|
|
|
|
if (stripeCustomer.deleted) {
|
2022-12-07 19:55:47 +00:00
|
|
|
return { checkoutSession, stripeCustomer: null };
|
2022-09-08 00:38:37 +00:00
|
|
|
}
|
|
|
|
return { stripeCustomer, checkoutSession };
|
|
|
|
}
|