How to Integrate Stripe Payment Gateway in Node.js

Install this package by using this command
npm install stripe
To run the file you need to run the following command
node index.js
You need to include stripe module in your file by using these lines.
var stripe = require(‘stripe’)(‘Your_Secret_Key’);
To get your secret key, simply go to Stripe Offical Website and create an account, then you can get your secret key as well as publishable key.

const express = require(“express”);
const app = express();
var cors = require(‘cors’);

const stripe = require(“stripe”)(“Your_Secret_Key”);

app.use(express.static(“.”));
app.use(express.json());
app.use(cors());
const calculateOrderAmount = items => {
// Replace this constant with a calculation of the order’s amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400;
};

app.post(“/create-payment-intent”, async (req, res) => {
const items = req.body;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: 55,
currency: “gbp”,
metadata: items
});

res.send({
clientSecret: paymentIntent.client_secret
});
});

app.listen(4000, () => console.log(‘Node server listening on port 4000!’));

Leave a comment

Your email address will not be published. Required fields are marked *