a804a29516
* chore: Stripe paid apps flow * chore: Subscription * chore: Webhooks * chore: Abstract functions * chore: Lockfile * chore: Webhook handler * chore: Use catch-all * chore: Webhook changes, etc * chore: Cleanup * chore: Use actual price id * chore: Updates * chore: Install normally until expiry date * Disable team install for paid apps and cal.ai\ * Fix the same at another place * Fix Typescript error * redactedCause doesnt have message has enumerable prop * Fix reinstallation of an already installed app * chore: Remove unused deps * chore: Ensure index * chore: Price in usd * chore: PR suggestion * Fix missing packages in yarn.lock --------- Co-authored-by: Hariom <hariombalhara@gmail.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>
75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
import { Prisma } from "@prisma/client";
|
|
import Stripe from "stripe";
|
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
import prisma from "@calcom/prisma";
|
|
|
|
export async function getStripeCustomerIdFromUserId(userId: number) {
|
|
// Get user
|
|
const user = await prisma.user.findUnique({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
select: {
|
|
email: true,
|
|
name: true,
|
|
metadata: true,
|
|
},
|
|
});
|
|
|
|
if (!user?.email) throw new HttpError({ statusCode: 404, message: "User email not found" });
|
|
|
|
const customerId = await getStripeCustomerId(user);
|
|
|
|
return customerId;
|
|
}
|
|
|
|
const userType = Prisma.validator<Prisma.UserArgs>()({
|
|
select: {
|
|
email: true,
|
|
metadata: true,
|
|
},
|
|
});
|
|
|
|
export type UserType = Prisma.UserGetPayload<typeof userType>;
|
|
/** This will retrieve the customer ID from Stripe or create it if it doesn't exists yet. */
|
|
export async function getStripeCustomerId(user: UserType): Promise<string> {
|
|
let customerId: string | null = null;
|
|
|
|
if (user?.metadata && typeof user.metadata === "object" && "stripeCustomerId" in user.metadata) {
|
|
customerId = (user?.metadata as Prisma.JsonObject).stripeCustomerId as string;
|
|
} else {
|
|
/* We fallback to finding the customer by email (which is not optimal) */
|
|
const customersResponse = await stripe.customers.list({
|
|
email: user.email,
|
|
limit: 1,
|
|
});
|
|
if (customersResponse.data[0]?.id) {
|
|
customerId = customersResponse.data[0].id;
|
|
} else {
|
|
/* Creating customer on Stripe and saving it on prisma */
|
|
const customer = await stripe.customers.create({ email: user.email });
|
|
customerId = customer.id;
|
|
}
|
|
|
|
await prisma.user.update({
|
|
where: {
|
|
email: user.email,
|
|
},
|
|
data: {
|
|
metadata: {
|
|
...(user.metadata as Prisma.JsonObject),
|
|
stripeCustomerId: customerId,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
return customerId;
|
|
}
|
|
|
|
const stripePrivateKey = process.env.STRIPE_PRIVATE_KEY || "";
|
|
export const stripe = new Stripe(stripePrivateKey, {
|
|
apiVersion: "2020-08-27",
|
|
});
|