2023-04-25 22:39:47 +00:00
|
|
|
import stripe from "@calcom/app-store/stripepayment/lib/server";
|
|
|
|
|
|
|
|
import type { TStripeCheckoutSessionInputSchema } from "./stripeCheckoutSession.schema";
|
2023-05-13 04:11:12 +00:00
|
|
|
import { ZStripeCheckoutSessionInputSchema } from "./stripeCheckoutSession.schema";
|
2023-04-25 22:39:47 +00:00
|
|
|
|
|
|
|
type StripeCheckoutSessionOptions = {
|
|
|
|
input: TStripeCheckoutSessionInputSchema;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const stripeCheckoutSessionHandler = async ({ input }: StripeCheckoutSessionOptions) => {
|
|
|
|
const { checkoutSessionId, stripeCustomerId } = input;
|
|
|
|
|
2023-05-13 04:11:12 +00:00
|
|
|
// Moved the following data checks to superRefine
|
|
|
|
const validationResult = ZStripeCheckoutSessionInputSchema.parse(input);
|
2023-04-25 22:39:47 +00:00
|
|
|
|
|
|
|
let customerId: string;
|
|
|
|
let isPremiumUsername = false;
|
|
|
|
let hasPaymentFailed = false;
|
|
|
|
if (checkoutSessionId) {
|
|
|
|
try {
|
|
|
|
const session = await stripe.checkout.sessions.retrieve(checkoutSessionId);
|
|
|
|
if (typeof session.customer !== "string") {
|
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
customerId = session.customer;
|
|
|
|
isPremiumUsername = true;
|
|
|
|
hasPaymentFailed = session.payment_status !== "paid";
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
customerId = stripeCustomerId!;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const customer = await stripe.customers.retrieve(customerId);
|
|
|
|
if (customer.deleted) {
|
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
valid: true,
|
|
|
|
hasPaymentFailed,
|
|
|
|
isPremiumUsername,
|
|
|
|
customer: {
|
|
|
|
username: customer.metadata.username,
|
|
|
|
email: customer.metadata.email,
|
|
|
|
stripeCustomerId: customerId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|