MCPcopy Create free account
hub / github.com/chiragksharma/BuildFast / createCheckoutSession

Function createCheckoutSession

src/pages/api/create-checkout-session.ts:12–43  ·  view source on GitHub ↗
(req: NextApiRequest, res: NextApiResponse)

Source from the content-addressed store, hash-verified

10const { type } = StripeConfig as { type: 'payment' | 'subscription' };
11
12const createCheckoutSession = async (req: NextApiRequest, res: NextApiResponse) => {
13 if (req.method !== 'POST') {
14 res.setHeader('Allow', 'POST');
15 return res.status(405).end('Method Not Allowed');
16 }
17
18 const { priceId } = req.body;
19
20 if (!priceId) {
21 return res.status(400).json({ error: 'Price ID is required' });
22 }
23
24 try {
25 const session = await stripe.checkout.sessions.create({
26 payment_method_types: ['card'],
27 line_items: [
28 {
29 price: priceId,
30 quantity: 1,
31 },
32 ],
33 mode: type,
34 success_url: `${req.headers.origin}/success?session_id={CHECKOUT_SESSION_ID}`,
35 cancel_url: `${req.headers.origin}`,
36 });
37
38 return res.status(200).json({ sessionId: session.id });
39 } catch (error) {
40 console.error('Error creating checkout session:', error);
41 return res.status(500).json({ error: 'Internal Server Error' });
42 }
43};
44
45export default createCheckoutSession;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected