Files
calendar/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.handler.ts
T
2dbc73c75b chore: fix all Typescript warnings (#8618)
* [CAL-1517] fix all Typescript warnings

* solve conflicts

* Update stripeCheckoutSession.handler.ts

Parse is a guard, so even though the variable is unused the parse itself is needed.

* Update ToolbarPlugin.tsx

Don't change dependency tree

---------

Co-authored-by: gitstart-calcom <gitstart@users.noreply.github.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Alex van Andel <me@alexvanandel.com>
2023-06-06 11:59:57 +00:00

64 lines
1.6 KiB
TypeScript

import stripe from "@calcom/app-store/stripepayment/lib/server";
import type { TStripeCheckoutSessionInputSchema } from "./stripeCheckoutSession.schema";
import { ZStripeCheckoutSessionInputSchema } from "./stripeCheckoutSession.schema";
type StripeCheckoutSessionOptions = {
input: TStripeCheckoutSessionInputSchema;
};
export const stripeCheckoutSessionHandler = async ({ input }: StripeCheckoutSessionOptions) => {
const { checkoutSessionId, stripeCustomerId } = input;
// Moved the following data checks to superRefine
ZStripeCheckoutSessionInputSchema.parse(input);
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,
};
}
};