Payment gateway, Stripe is one of the very common payment gateways which is being used in most of the applications. So today I am going to create a sample application in Node.js to integrate stripe in Node.js express application.

Here I will try to make everything very briefly to make the things clear. I am not going to use any DB or any kind of authentication while routing.

Let’s Get Started

Step 1: Create a Stripe account to get the key and secret.

At first we need to create an account which we can go after navigating to https://dashboard.stripe.com/register. Once completing the registration form, We need to login to the stripe account.

After login to dashboard, We need to generate the key and secret mainly. So move to APIs section via Developers tab available at the left sidebar. From there we can get the key and secret needed in our app.

Step 2: Create Node.js project folder with needed dependencies

We need to create a basic app of nodejs with needed dependencies installed. So run below commands over terminal.

mkdir stripe-payment-nodejs-express-tutorial

cd stripe-payment-nodejs-express-tutorial

npm init --yes

npm i stripe express path pug body-parser

With the above command, we have created the Node.js project folder moved to the project folder and installed the needed package after creating package.json with NPM init.

Step 3: Create server.js file

This is the file where we will write everything in this demo app. So create a file named, server.js inside project folder and put the below code inside it.

const keyPublishable = '****************************************'; // Enter the key here
const keySecret = '*******************************'; // enter the secret here
 
const app = require("express")();
const stripe = require("stripe")(keySecret);
const pug = require('pug');
const path = require('path');
 
const bodyParser = require('body-parser');
 
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
 
// to serve files of views directory
app.set('views',path.join(__dirname,'views'));
app.set('view engine', 'pug') // setting pug as view engine
 
// to open the payment page on base url
app.get("/", ((req, res) => {
res.render("index",{keyPublishable: keyPublishable});
}));
 
app.post("/pay", function(req, res) {
 
let amount = 10*100;
 
// create a customer
stripe.customers.create({
email: req.body.stripeEmail, // customer email
source: req.body.stripeToken // token for the card
})
.then(customer =>
stripe.charges.create({ // charge the customer
amount,
description: "Sample Charge",
currency: "usd",
customer: customer.id
}))
.then(charge => res.render("pay")); // render the payment successful alter page after payment
 
});
 
// app listening on port 3000
app.listen(3000, () => {
console.log('server is running on port 3000');
});

Step 4: Create View

In the end, We are going to create the view part to show the make payment page and then show the payment confirmation page. At the project root folder, We need to create a folder name views and create two pug files inside it named index.pug and pay.pug for payment page and confirmation page respectively.

put the below code inside index.pug file.

html
body
div(style='height:300px; width:100%;background-color:#e2e2e2; text-align:center; padding-top:50px')
p(style='font-size:30px;') jsonworld.com
p Payment with stripe in Nodejs express application.
 
form(action="/pay", method="post")
article
label Amount: html
body
div(style='height:300px; width:100%;background-color:#e2e2e2; text-align:center; padding-top:50px')
p(style='font-size:30px;') jsonworld.com
p Payment with stripe in Nodejs express application.
 
form(action="/pay", method="post")
article
label Amount: $10.00
script(
src="//checkout.stripe.com/v2/checkout.js",
class="stripe-button",
data-key='*********************************',
data-locale="auto",
data-description="Sample payment",
data-amount="1000")
0.00
script(
src="//checkout.stripe.com/v2/checkout.js",
class="stripe-button",
data-key='*********************************',
data-locale="auto",
data-description="Sample payment",
data-amount="1000")

put the below code inside pay.pug file.

html
body
div(style='height:300px; width:100%;background-color:#e2e2e2; text-align:center; padding-top:50px')
h2 Payment done successfully! html
body
div(style='height:300px; width:100%;background-color:#e2e2e2; text-align:center; padding-top:50px')
h2 Payment done successfully! $10.00
0.00

Step 5: Run the app

Start the Node.js server with npm start or node server.js and check the app over http:localhost:3000/. You will see page like below:

Conclusion

So in this demo, We learn to integrate stripe with Node.js express application. You can find other demos of Node.js sample application here

** **That’s all for now. Thank you for reading and I hope this demo will be very helpful to payment with stripe in Node.js application.

Let me know your thoughts over the email demo.jsonworld@gmail.com. I would love to hear them and If you like this article, share with your friends.

You can download complete code from here. Download Code

#node-js

Payment With Stripe in Node.js Application Example & Tutorial
3 Likes257.80 GEEK