Files
calendar/packages/lib/server/service/stripe.ts
T
sean-brydonandGitHub 5cd2ba1362 perf: move stripe to billing service out of app store (#20376)
* add more methods to billingService

* update profile handler

* stripe customer handler

* verify email

* move imports + move generic methods to billing service

* move to strip ebilling service class

* push changes to mocks

* fix enum

* fix typo

* correctly update customer metadata

* fix userId type to number

* fix types on billing services

* fix type errors due to return methods differing

* fix return types
2025-03-31 09:20:27 +00:00

63 lines
1.7 KiB
TypeScript

import stripe from "@calcom/features/ee/payments/server/stripe";
import {
ZStripeCheckoutSessionInputSchema,
type TStripeCheckoutSessionInputSchema,
} from "@calcom/trpc/server/routers/publicViewer/stripeCheckoutSession.schema";
export class StripeService {
static async getCheckoutSession(input: TStripeCheckoutSessionInputSchema) {
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,
};
}
}
}