How to Integrate Stripe Payment Gateway in Node.js ?

16 Jun, 2022
Payment gateways help the user to make their payments. There are many payment gateways available in the market like Razorpay, Google pay, etc but the most popular among them is Stripe payment gateway. Stripe is the premier option for online credit card processing and it is also the most popular premium payment gateway. Introduction:
It’s easy to get started and easy to use.It is widely used and popular module for processing payments.User friendly services and highly sercured.
It’s easy to get started and easy to use.
It is widely used and popular module for processing payments.
User friendly services and highly sercured.
Installation of stripe module:
You can visit the link Install stripe module. You can install this package by using this command.
npm install stripe
After installing stripe module, you can check your stripe version in command prompt using the command.
npm version stripe
After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.
node index.js
Requiring module: 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 Official Website and create an account, then you can get your secret key as well as publishable key.
Filename: Home.ejs 
html
<!DOCTYPE html><html><title>Stripe Payment Demo</title><body>    <h3>Welcome to Payment Gateway</h3>    <form action="payment" method="POST">       <script          src="//checkout.stripe.com/v2/checkout.js"          class="stripe-button"          data-key="<%= key %>"          data-amount="2500"          data-currency="inr"          data-name="Crafty Gourav"          data-description="Handmade Art and Craft Products"          data-locale="auto" >        </script>    </form></body></html>
Filename: index.js 
javascript
const express = require('express')const bodyparser = require('body-parser')const path = require('path')const app = express() var Publishable_Key = 'Your_Publishable_Key'var Secret_Key = 'Your_Secret_Key' const stripe = require('stripe')(Secret_Key) const port = process.env.PORT || 3000 app.use(bodyparser.urlencoded({extended:false}))app.use(bodyparser.json()) // View Engine Setupapp.set('views', path.join(__dirname, 'views'))app.set('view engine', 'ejs') app.get('/', function(req, res){    res.render('Home', {       key: Publishable_Key    })}) app.post('/payment', function(req, res){     // Moreover you can take more details from user    // like Address, Name, etc from form    stripe.customers.create({        email: req.body.stripeEmail,        source: req.body.stripeToken,        name: 'Gourav Hammad',        address: {            line1: 'TC 9/4 Old MES colony',            postal_code: '452331',            city: 'Indore',            state: 'Madhya Pradesh',            country: 'India',        }    })    .then((customer) => {         return stripe.charges.create({            amount: 2500,     // Charing Rs 25            description: 'Web Development Product',            currency: 'INR',            customer: customer.id        });    })    .then((charge) => {        res.send("Success")  // If no error occurs    })    .catch((err) => {        res.send(err)       // If some error occurs    });}) app.listen(port, function(error){    if(error) throw error    console.log("Server created Successfully")})
Steps to run the program:
The project structure will look like this:  The “Home.ejs” is kept in the views folder.Make sure you have ‘view engine’ like I have used “ejs” and also install express, body-parser, and stripe using the following commands:
The project structure will look like this:  The “Home.ejs” is kept in the views folder.
Make sure you have ‘view engine’ like I have used “ejs” and also install express, body-parser, and stripe using the following commands:
npm install ejs
npm install express
npm install body-parser
npm install stripe
Run index.js file using below command:
Run index.js file using below command:
node index.js
Open browser and type this URL:
Open browser and type this URL:
http://localhost:3000/
Then you will see the Payment Gateway form as shown below: Then click on ‘Pay with Card’ button and then you will see the stripe payment form as shown below:  Fill this form with correct credit card details and click on ‘Pay’ button and then if no errors occurs, then following message will be displayed: Now go to your stripe dashboard and you can see the current payment details as shown below: 
Then you will see the Payment Gateway form as shown below: 
Then click on ‘Pay with Card’ button and then you will see the stripe payment form as shown below:  Fill this form with correct credit card details and click on ‘Pay’ button and then if no errors occurs, then following message will be displayed: 
Now go to your stripe dashboard and you can see the current payment details as shown below: 
So this is how you can integrate Stripe payment gateway in node.js. There are other payment gateways available in the market like Razorpay, Google Pay, etc.
sumitgumber28
Node.js-Misc
Node.js
Web Technologies
Web technologies Questions
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to install the previous version of node.js and npm ?
Node.js fs.writeFile() Method
Difference between promise and async await in Node.js
Mongoose | findByIdAndUpdate() Function
JWT Authentication with Node.js
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Remove elements from a JavaScript Array