* Add payment option to schema * Add payment option to Stripe zod * Set payment option on event type * Create manual payment intent in Stripe * Set payment option from Stripe app * Add payment option to DB * Pass React.ReactNode to checkbox * Create uncaptured payment intent * WIP * Capture card in setup intent * Show charge card option * Charge card from booking page * Bug fixes * Clean up * Clean up app card * Add no-show fee messaging on booking page * Send payment email on payment & add price * Fix messaging * Create no show fee charged email * Send charge fee collected email * Disable submit on card failure * Clean up * Serverside prevent charging card again if already charged * Only confirm booking if paid for * Type fixes * More type fixes * More type fixes * Type fix * Type fixes * UI changes * Payment component rework * Update apps/web/public/static/locales/en/common.json Co-authored-by: Alex van Andel <me@alexvanandel.com> * Update apps/web/public/static/locales/en/common.json Co-authored-by: Alex van Andel <me@alexvanandel.com> * Update apps/web/components/dialog/ChargeCardDialog.tsx Co-authored-by: Alex van Andel <me@alexvanandel.com> * Update packages/trpc/server/routers/viewer/payments.tsx Co-authored-by: Alex van Andel <me@alexvanandel.com> * Revert GTM config * Adjust payment option dropdown * Show alert when seats are set * Small bug fixes * Create collect card method * clean up * Prevent seats & charge no-show fee to be enabled together * Do not charge no-show fee on unconfirmed bookings * Add check to collect card method * Webhook send request emails * Fix some dark mode colours * Change awaiting payment language * Type fixes * Set height of Select and TextField both to 38px to fix alignment * Fix message seats & payment error message * Type fix --------- Co-authored-by: Alex van Andel <me@alexvanandel.com>
109 lines
2.7 KiB
TypeScript
109 lines
2.7 KiB
TypeScript
import { Prisma } from "@prisma/client";
|
|
|
|
import { HttpError as HttpCode } from "@calcom/lib/http-error";
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import stripe from "./server";
|
|
|
|
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 HttpCode({ 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,
|
|
},
|
|
});
|
|
|
|
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;
|
|
}
|
|
|
|
export async function deleteStripeCustomer(user: UserType): Promise<string | null> {
|
|
const customerId = await getStripeCustomerId(user);
|
|
|
|
if (!customerId) {
|
|
console.warn("No stripe customer found for user:" + user.email);
|
|
return null;
|
|
}
|
|
|
|
//delete stripe customer
|
|
const deletedCustomer = await stripe.customers.del(customerId);
|
|
|
|
return deletedCustomer.id;
|
|
}
|
|
|
|
export async function retrieveOrCreateStripeCustomerByEmail(email: string, stripeAccountId: string) {
|
|
const customer = await stripe.customers.list(
|
|
{
|
|
email,
|
|
limit: 1,
|
|
},
|
|
{
|
|
stripeAccount: stripeAccountId,
|
|
}
|
|
);
|
|
|
|
if (customer.data[0]?.id) {
|
|
return customer.data[0];
|
|
} else {
|
|
const newCustomer = await stripe.customers.create(
|
|
{ email },
|
|
{
|
|
stripeAccount: stripeAccountId,
|
|
}
|
|
);
|
|
return newCustomer;
|
|
}
|
|
}
|