Skip to content

Node JS – Integrate Stripe Payment Gateway in Express JS

Last updated on November 21, 2022

In this stripe node js tutorial, we gonna do stripe implementation in the node js project. For Stripe node js express js integration we gonna use stripe-node npm module.

One of the obvious reasons to love stripe is, it is pretty easy to integrate, and nowadays it is the most popular one. You can create a Stripe account via the stripe registration page, which you can use for both testing and production, both versions will contain different app key and secret key, make sure to update those keys before deploying your application to production.

Stripe is responsible for processing and keeping the client’s credit/debit card data so no information of essence would be stored on your server and you would not have to comply with all the rules that come with storing credit/debit cards.

Integrate Stripe Payment Gateway in Express JS
stripe node js tutorial

Stripe node js integration tutorial step by step guide.

Step 1:
stripe create account | arjunphp.com

Create an account on https://dashboard.stripe.com/register

Step 2:

Now login to your account and go to the APIs page (left menu item). From here grab your API keys.

stripe account page
Step 3:

Create a project directory and install the required dependencies with the below-shown commands.

$ mkdir express-stripe-demo
$ cd express-stripe-demo
$ npm init --yes
$ npm install --save express body-parser pug stripe 
  • mkdir express-stripe-demo – it will create a folder called “express-stripe-demo
  • cd express-stripe-demo – changing the directory to “express-stripe-demo
  • npm init --yes – will create package.json file under the current directory
  • npm install --save express body-parser pug stripe – it will download all mentioned modules into the node_modules directory
What do these packages do?
  • express is the Node framework.
  • body-parser will let us pull POST content from our HTTP request.
  • pug is the template engine
  • stripe is a payment gateway library

Now let’s setup the application, and create file called server.js with the below code, go through the comments of the code for better understanding.

// server.js

// Replace with your stripe public and secret keys
const keyPublishable = 'pk_test_xxxxxxxxxxxx';
const keySecret = 'sk_test_xxxxxxxxxxxxxx';

// import and create express object
const app = require("express")();
// import and create stripe object
const stripe = require("stripe")(keySecret);
// import pug templating engine 
const pug = require('pug');
// Node's path module, provides utilities for working with file and directory paths
const path = require('path');

const bodyParser = require('body-parser');
// support json encoded bodies
app.use(bodyParser.json()); 
// support encoded bodies
app.use(bodyParser.urlencoded({ extended: false })); 

// set view files directory as views
app.set('views',path.join(__dirname,'views'));
// set view engine as pug
app.set('view engine', 'pug')

// GET http://localhost:3000/
app.get("/", ((req, res) => {
    res.render("index"); // render the view file : views/index.pug
}));

// POST http://localhost:3000/charge
app.post("/charge", function(req, res) {

    let amount = 5*100; // 500 cents means $5 

    // create a customer 
    stripe.customers.create({
        email: req.body.stripeEmail, // customer email, which user need to enter while making payment
        source: req.body.stripeToken // token for the given card 
    })
    .then(customer =>
        stripe.charges.create({ // charge the customer
        amount,
        description: "Sample Charge",
            currency: "usd",
            customer: customer.id
        }))
    .then(charge => res.render("charge"));

});

// app listening on port 3000
app.listen(3000, () => {
    console.log(`App is running at: http://localhost:3000/`);
});

Now let’s create view files to accept payment and show the status of the translation

//views/charge.pug
h2 You successfully paid $5.00!
//views/index.pug
html
  body
    form(action="/charge", method="post")
      article
        label Amount: $5.00
      script(
        src="//checkout.stripe.com/v2/checkout.js",
        class="stripe-button",
        data-key='pk_test_xxxxxxxxxxxxxxxx',
        data-locale="auto",
        data-description="Sample Charge",
        data-amount="500")

Start your server with the below command, you should able to access the website at http://localhost:3000/

$ node server.js

After successful payment, you should able to see the below output –

You successfully paid $5.00!
0 0 votes
Article Rating
Subscribe
Notify of
guest

6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments