(req, res)
| 3 | const stripe = new Stripe(process.env.NEXT_SECRET_STRIPE_KEY); |
| 4 | |
| 5 | export default async function handler(req, res) { |
| 6 | if (req.method === 'POST') { |
| 7 | try { |
| 8 | // Create Checkout Sessions from body params. |
| 9 | const params = { |
| 10 | submit_type: 'pay', |
| 11 | mode: 'payment', |
| 12 | payment_method_types: ['card'], |
| 13 | billing_address_collection: 'auto', |
| 14 | shipping_options: [ |
| 15 | { shipping_rate: 'shr_1MJIEoHbmXqvpyhdyi5WNQHl' }, |
| 16 | { shipping_rate: 'shr_1MJIGgHbmXqvpyhdQCdPgK8F' } |
| 17 | ], |
| 18 | line_items: req.body.map((item) => { |
| 19 | const img = item.image[0].asset._ref; |
| 20 | const newImage = img.replace('image-', 'https://cdn.sanity.io/images/dow10h3v/production/').replace('-png', '.png'); |
| 21 | |
| 22 | return { |
| 23 | price_data: { |
| 24 | currency: 'usd', |
| 25 | product_data: { |
| 26 | name: item.name, |
| 27 | images: [newImage], |
| 28 | }, |
| 29 | unit_amount: item.price * 100, |
| 30 | }, |
| 31 | adjustable_quantity: { |
| 32 | enabled:true, |
| 33 | minimum: 1, |
| 34 | }, |
| 35 | quantity: item.quantity |
| 36 | } |
| 37 | }), |
| 38 | success_url: `${req.headers.origin}/successPay`, |
| 39 | cancel_url: `${req.headers.origin}/canceled`, |
| 40 | } |
| 41 | const session = await stripe.checkout.sessions.create(params); |
| 42 | |
| 43 | res.status(200).json(session); |
| 44 | } catch (err) { |
| 45 | res.status(err.statusCode || 500).json(err.message); |
| 46 | } |
| 47 | } else { |
| 48 | res.setHeader('Allow', 'POST'); |
| 49 | res.status(405).end('Method Not Allowed'); |
| 50 | } |
| 51 | } |
nothing calls this directly
no outgoing calls
no test coverage detected