43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import Stripe from "stripe";
|
|
import checkSubscription from "./checkSubscription";
|
|
|
|
export default async function paymentCheckout(
|
|
stripeSecretKey: string,
|
|
email: string,
|
|
priceId: string
|
|
) {
|
|
const stripe = new Stripe(stripeSecretKey, {
|
|
apiVersion: "2022-11-15",
|
|
});
|
|
|
|
const listByEmail = await stripe.customers.list({
|
|
email: email.toLowerCase(),
|
|
expand: ["data.subscriptions"],
|
|
});
|
|
|
|
const isExistingCostomer = listByEmail?.data[0]?.id || undefined;
|
|
|
|
const TRIAL_PERIOD_DAYS = process.env.TRIAL_PERIOD_DAYS;
|
|
const session = await stripe.checkout.sessions.create({
|
|
customer: isExistingCostomer ? isExistingCostomer : undefined,
|
|
line_items: [
|
|
{
|
|
price: priceId,
|
|
quantity: 1,
|
|
},
|
|
],
|
|
mode: "subscription",
|
|
customer_email: isExistingCostomer ? undefined : email.toLowerCase(),
|
|
success_url: `${process.env.BASE_URL}?session_id={CHECKOUT_SESSION_ID}`,
|
|
cancel_url: `${process.env.BASE_URL}/login`,
|
|
automatic_tax: {
|
|
enabled: true,
|
|
},
|
|
subscription_data: {
|
|
trial_period_days: TRIAL_PERIOD_DAYS ? Number(TRIAL_PERIOD_DAYS) : 14,
|
|
},
|
|
});
|
|
|
|
return { response: session.url, status: 200 };
|
|
}
|