Payment With Stripe in Node.js Application Example & Tutorial

Payment With Stripe in Node.js Application Example & Tutorial

Payment With Stripe in Node.js Application Example & Tutorial - 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.

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 [email protected]. 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

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Hire Top Node JS Developers | Best Node.js Development Company India

Hire dedicated Node JS developers & programmers in India for custom full-stack NodeJS web development projects on hourly/full-time basis. Strict NDA, 16+ years exp & 2500+ clients|450+ Experts

Hire Dedicated Node.js Developers - Hire Node.js Developers

Get business-centric Node.Js development services from expert Node.JS developers. We have expertise in developing & maintaining Node JS apps as per the business requirements.

Node JS Development Company| Node JS Web Developers-SISGAIN

SISGAIN is the top rated node js development company providing professional services on node js web and mobile development.

Why use Node.js for Web Development? Benefits and Examples of Apps

Get Node.js developers & programmers, and dedicated Node coders in India with average 5 years of experience. 15+ Yrs Exp, 500+ Staff, 13800+ Projects, 6800+ Clients.

Node JS Development Company | Hire Node.js Developers

Looking to hire Node js developers? One of the top Node js development companies in India & USA offers cost-effective Node js web development services.