const express = require('express'); const stripe = require('stripe')('sk_test_51O1jDqSCGoxyY1pkuz8S9ZLdnBOHcFrheJvM7pjjdFImdbCrzE5kjdwFjr01RlpYTZoNCOaGwDR6FjwsKq8ULsxL00JmE0Xo7M'); const bodyParser = require('body-parser'); const app = express(); const port = 4000; // Middleware to parse JSON bodies app.use(bodyParser.json()); // API endpoint for creating a Stripe checkout session app.post('/create-checkout-session', async (req, res) => { try { const session = await stripe.checkout.sessions.create({ payment_method_types: ['card'], line_items: [{ price_data: { currency: 'inr', product_data: { name: 'Product Name', }, unit_amount: req.body.amount * 100, }, quantity: 1, }], mode: 'payment', success_url: req.body.success_url, cancel_url: req.body.cancel_url, }); // Redirect the user to the Stripe checkout page res.json(session.url); } catch (error) { console.error(error); res.status(500).json({ error: 'An error occurred while creating the checkout session' }); } }); // app.post('/process-payment', async (req, res) => { // const sessionId = "cs_test_a1naEbq6kxn5s2zSRdhh20aMAj8OVtVojY7aJK18DYdq3dvdgT8DeeUYNc"; // // try { // const checkoutSession = await stripe.checkout.sessions.retrieve(sessionId); // console.log("check",checkoutSession) // const paymentIntent = checkoutSession.payment_intent; // console.log("payment",paymentIntent) // // // Confirm the payment intent // const paymentResult = await stripe.paymentIntents.confirm(paymentIntent.id, { // payment_method_types: paymentIntent.payment_method_types, // }); // // if (paymentResult.status === 'succeeded') { // // Payment succeeded // res.status(200).json({ message: 'Payment succeeded' }); // } else { // // Payment failed // res.status(400).json({ message: 'Payment failed' }); // } // } catch (error) { // console.error(error); // res.status(500).json({ message: 'Server error' }); // } // }); app.get('/get-list', async (req, res) => { const invoices = await stripe.invoices.list({ limit: 3, }); res.json({invoices}) }) // Start the server app.listen(port, () => { console.log(`Server running on port ${port}`); });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter