Phone: +91 80884989467 Email: [email protected]
Follow us

How to setup stripe payment gateway using NodeJS

Stripe Payment Gateway Setup in NodeJS

“Stripe is an American technology company based in San Francisco, California.[3] Its software allows individuals and businesses to make and receive payments over the Internet.” (from Wikipedia)

Why use the Stripe plugin?
  • Accept all major debit and credit cards as well as local payment methods
  • Benefit from Stripe Elements, which helps your users input their payment information quickly and correctly at checkout
  • Achieve PCI-DSS compliance with Stripe Elements hosted input fields
  • Supports Strong Customer Authentication (SCA)
  • Optimized for mobile and fast checkout with Apple Pay, Google Pay, and Payment Request API support
  • Better detect and prevent fraud with the help of Stripe Radar and optional 3D Secure
  • Easily capture authorizations and process refunds from your WooCommerce Dashboard
  • Support recurring payments with various payment methods via WooCommerce Subscriptions
  • Benefit from free updates and new innovative functionality as Stripe launches new features and products

Today we’re going to deep dive into the stripe payment gateway, I’m preferring this payment gateway because it doesn’t require any type of document to run it into production. In this article, I’m going to use NodeJS as the backend and EJS as frontend so let’s get started. Create package.json

{
   "name":"module-stripe-payment",
   "version":"1.0.0",
   "description":"",
   "main":"index.js",
   "scripts":{
      "start":"node index",
      "test":"echo \"Error: no test specified\" && exit 1"
   },
   "keywords":[
      "stripe",
      "payment",
      "gateway",
      "nodejs",
      "ejs",
      "express"
   ],
   "author":"Ajay",
   "license":"ISC",
   "dependencies":{
      "body-parser":"^1.19.0",
      "ejs":"^3.0.1",
      "express":"^4.17.1",
      "stripe":"^8.33.0"
   }
}

first create index.ejs and put below code inside it

<form method="post" action="/charge">
   <script src="https://checkout.stripe.com/checkout.js"    
      class="stripe-button"    data-key="** your public key **"    
      data-name="third"    
      data-description="test"    
      data-amount="45"    
      data-currency="INR"    
      data-locale="auto">      </script>      
   <input type="hidden" name="dataAmount" value="45">  
</form>

Here I have created a div and added my payment form inside it enclosed under the form tag. The form has action as /charge and method as a post. Inside the form, I have added a script tag which is basically used for the default pay button used by stripe. It takes several parameters like src which is to get the checkout.js file, class stripe-button which is used for the pay button, data-amount is for total payable amount, data-key which is your public key depending upon the production mode. it can vary in production & development mode. Then next is currency and locale. Here is my success.ejs

<h1 style="color: green; text-align: center;">your payment has been successful</h1>

Now in the backend create a route called /charge and add payment logic inside it.

const express = require('express');
const ejs = require('ejs');
const bodyParser = require('body-parser');
const stripe = require('stripe')('** your private key **');
const PORT = process.env.PORT || 3000;
const app = express();
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({
	extended: false
}));
app.use(bodyParser.json());
app.get('/', function (req, res) {
	res.render('index');
});
app.post('/charge', function (req, res) {
	const token = req.body.stripeToken;
	const amount = req.body.amountTotal;
	var charge = stripe.charges.create({
		amount: amount,
		currency: 'INR',
		source: token
	}, function (err) {
		if (err) {
			throw err;
		}
	});
	res.redirect('/success');
});
app.get('/success', function (req, res) {
	res.render('success');
});
app.listen(PORT, function (err) {
	if (err) {
		throw err;
	}
});

Now start your application by running npm start Now open your browser and type http://localhost:3000/