351 lines
9.8 KiB
TypeScript
351 lines
9.8 KiB
TypeScript
// api/checkout/route.ts
|
|
|
|
import { createClient } from "@/utils/supabase/server";
|
|
import Stripe from "stripe";
|
|
|
|
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY || "", {
|
|
apiVersion: "2024-10-28.acacia",
|
|
});
|
|
|
|
// ... existing imports and stripe initialization ...
|
|
|
|
export async function POST(req: Request) {
|
|
const {
|
|
quantity,
|
|
color,
|
|
free_shipping,
|
|
}: { quantity: number; color: string; free_shipping: boolean } =
|
|
await req.json();
|
|
|
|
console.log("foobar", quantity, color, free_shipping);
|
|
|
|
const supabase = createClient();
|
|
const {
|
|
data: { session },
|
|
} = await supabase.auth.getSession();
|
|
|
|
const successUrl = session
|
|
? `${req.headers.get("origin")}/home?session_id={CHECKOUT_SESSION_ID}`
|
|
: `${req.headers.get("origin")}/login?session_id={CHECKOUT_SESSION_ID}`;
|
|
|
|
try {
|
|
const stripeSession = await stripe.checkout.sessions.create({
|
|
line_items: [
|
|
{
|
|
price: "price_1R4nqzLTb7Djmo1RZuUmWj6c",
|
|
quantity: quantity,
|
|
},
|
|
],
|
|
mode: "payment",
|
|
success_url: successUrl,
|
|
cancel_url: `${req.headers.get("origin")}`,
|
|
billing_address_collection: "required",
|
|
phone_number_collection: {
|
|
enabled: true, // Enable phone number collection
|
|
},
|
|
shipping_address_collection: {
|
|
allowed_countries: [
|
|
"AC",
|
|
"AD",
|
|
"AE",
|
|
"AF",
|
|
"AG",
|
|
"AI",
|
|
"AL",
|
|
"AM",
|
|
"AO",
|
|
"AQ",
|
|
"AR",
|
|
"AT",
|
|
"AU",
|
|
"AW",
|
|
"AX",
|
|
"AZ",
|
|
"BA",
|
|
"BB",
|
|
"BD",
|
|
"BE",
|
|
"BF",
|
|
"BG",
|
|
"BH",
|
|
"BI",
|
|
"BJ",
|
|
"BL",
|
|
"BM",
|
|
"BN",
|
|
"BO",
|
|
"BQ",
|
|
"BR",
|
|
"BS",
|
|
"BT",
|
|
"BV",
|
|
"BW",
|
|
"BY",
|
|
"BZ",
|
|
"CA",
|
|
"CD",
|
|
"CF",
|
|
"CG",
|
|
"CH",
|
|
"CI",
|
|
"CK",
|
|
"CL",
|
|
"CM",
|
|
"CN",
|
|
"CO",
|
|
"CR",
|
|
"CV",
|
|
"CW",
|
|
"CY",
|
|
"CZ",
|
|
"DE",
|
|
"DJ",
|
|
"DK",
|
|
"DM",
|
|
"DO",
|
|
"DZ",
|
|
"EC",
|
|
"EE",
|
|
"EG",
|
|
"EH",
|
|
"ER",
|
|
"ES",
|
|
"ET",
|
|
"FI",
|
|
"FJ",
|
|
"FK",
|
|
"FO",
|
|
"FR",
|
|
"GA",
|
|
"GB",
|
|
"GD",
|
|
"GE",
|
|
"GF",
|
|
"GG",
|
|
"GH",
|
|
"GI",
|
|
"GL",
|
|
"GM",
|
|
"GN",
|
|
"GP",
|
|
"GQ",
|
|
"GR",
|
|
"GS",
|
|
"GT",
|
|
"GU",
|
|
"GW",
|
|
"GY",
|
|
"HK",
|
|
"HN",
|
|
"HR",
|
|
"HT",
|
|
"HU",
|
|
"ID",
|
|
"IE",
|
|
"IL",
|
|
"IM",
|
|
"IN",
|
|
"IO",
|
|
"IQ",
|
|
"IS",
|
|
"IT",
|
|
"JE",
|
|
"JM",
|
|
"JO",
|
|
"JP",
|
|
"KE",
|
|
"KG",
|
|
"KH",
|
|
"KI",
|
|
"KM",
|
|
"KN",
|
|
"KR",
|
|
"KW",
|
|
"KY",
|
|
"KZ",
|
|
"LA",
|
|
"LB",
|
|
"LC",
|
|
"LI",
|
|
"LK",
|
|
"LR",
|
|
"LS",
|
|
"LT",
|
|
"LU",
|
|
"LV",
|
|
"LY",
|
|
"MA",
|
|
"MC",
|
|
"MD",
|
|
"ME",
|
|
"MF",
|
|
"MG",
|
|
"MK",
|
|
"ML",
|
|
"MM",
|
|
"MN",
|
|
"MO",
|
|
"MQ",
|
|
"MR",
|
|
"MS",
|
|
"MT",
|
|
"MU",
|
|
"MV",
|
|
"MW",
|
|
"MX",
|
|
"MY",
|
|
"MZ",
|
|
"NA",
|
|
"NC",
|
|
"NE",
|
|
"NG",
|
|
"NI",
|
|
"NL",
|
|
"NO",
|
|
"NP",
|
|
"NR",
|
|
"NU",
|
|
"NZ",
|
|
"OM",
|
|
"PA",
|
|
"PE",
|
|
"PF",
|
|
"PG",
|
|
"PH",
|
|
"PK",
|
|
"PL",
|
|
"PM",
|
|
"PN",
|
|
"PR",
|
|
"PS",
|
|
"PT",
|
|
"PY",
|
|
"QA",
|
|
"RE",
|
|
"RO",
|
|
"RS",
|
|
"RU",
|
|
"RW",
|
|
"SA",
|
|
"SB",
|
|
"SC",
|
|
"SE",
|
|
"SG",
|
|
"SH",
|
|
"SI",
|
|
"SJ",
|
|
"SK",
|
|
"SL",
|
|
"SM",
|
|
"SN",
|
|
"SO",
|
|
"SR",
|
|
"SS",
|
|
"ST",
|
|
"SV",
|
|
"SX",
|
|
"SZ",
|
|
"TA",
|
|
"TC",
|
|
"TD",
|
|
"TF",
|
|
"TG",
|
|
"TH",
|
|
"TJ",
|
|
"TK",
|
|
"TL",
|
|
"TM",
|
|
"TN",
|
|
"TO",
|
|
"TR",
|
|
"TT",
|
|
"TV",
|
|
"TW",
|
|
"TZ",
|
|
"UA",
|
|
"UG",
|
|
"US",
|
|
"UY",
|
|
"UZ",
|
|
"VA",
|
|
"VC",
|
|
"VE",
|
|
"VG",
|
|
"VN",
|
|
"VU",
|
|
"WF",
|
|
"WS",
|
|
"XK",
|
|
"YE",
|
|
"YT",
|
|
"ZA",
|
|
"ZM",
|
|
"ZW",
|
|
"ZZ",
|
|
],
|
|
},
|
|
allow_promotion_codes: true,
|
|
shipping_options: [
|
|
{
|
|
shipping_rate_data: {
|
|
type: "fixed_amount",
|
|
fixed_amount: {
|
|
amount: 0,
|
|
currency: "usd",
|
|
},
|
|
display_name: "Global Shipping",
|
|
delivery_estimate: {
|
|
minimum: {
|
|
unit: "week",
|
|
value: 2,
|
|
},
|
|
maximum: {
|
|
unit: "week",
|
|
value: 3,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
// shipping_options: free_shipping
|
|
// ? []
|
|
// : [
|
|
// {
|
|
// shipping_rate_data: {
|
|
// type: "fixed_amount",
|
|
// fixed_amount: {
|
|
// amount: 1000,
|
|
// currency: "usd",
|
|
// },
|
|
// display_name: "Global Shipping",
|
|
// delivery_estimate: {
|
|
// minimum: {
|
|
// unit: "week",
|
|
// value: 2,
|
|
// },
|
|
// maximum: {
|
|
// unit: "week",
|
|
// value: 3,
|
|
// },
|
|
// },
|
|
// },
|
|
// },
|
|
// ],
|
|
metadata: {
|
|
device_color: color,
|
|
},
|
|
});
|
|
|
|
return new Response(JSON.stringify({ url: stripeSession.url }), {
|
|
status: 200,
|
|
});
|
|
} catch (error) {
|
|
console.log("Error creating checkout session", error);
|
|
return new Response(
|
|
JSON.stringify({ error: (error as Error).message }),
|
|
{
|
|
status: 500,
|
|
}
|
|
);
|
|
}
|
|
}
|