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

Top 7 Most Popular Node.js Frameworks You Should Know

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser.

One of the main advantages of Node is that it enables developers to use JavaScript on both the front-end and the back-end of an application. This not only makes the source code of any app cleaner and more consistent, but it significantly speeds up app development too, as developers only need to use one language.

Node is fast, scalable, and easy to get started with. Its default package manager is npm, which means it also sports the largest ecosystem of open-source libraries. Node is used by companies such as NASA, Uber, Netflix, and Walmart.

But Node doesn't come alone. It comes with a plethora of frameworks. A Node framework can be pictured as the external scaffolding that you can build your app in. These frameworks are built on top of Node and extend the technology's functionality, mostly by making apps easier to prototype and develop, while also making them faster and more scalable.

Below are 7of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Express

With over 43,000 GitHub stars, Express is the most popular Node framework. It brands itself as a fast, unopinionated, and minimalist framework. Express acts as middleware: it helps set up and configure routes to send and receive requests between the front-end and the database of an app.

Express provides lightweight, powerful tools for HTTP servers. It's a great framework for single-page apps, websites, hybrids, or public HTTP APIs. It supports over fourteen different template engines, so developers aren't forced into any specific ORM.

Meteor

Meteor is a full-stack JavaScript platform. It allows developers to build real-time web apps, i.e. apps where code changes are pushed to all browsers and devices in real-time. Additionally, servers send data over the wire, instead of HTML. The client renders the data.

The project has over 41,000 GitHub stars and is built to power large projects. Meteor is used by companies such as Mazda, Honeywell, Qualcomm, and IKEA. It has excellent documentation and a strong community behind it.

Koa

Koa is built by the same team that built Express. It uses ES6 methods that allow developers to work without callbacks. Developers also have more control over error-handling. Koa has no middleware within its core, which means that developers have more control over configuration, but which means that traditional Node middleware (e.g. req, res, next) won't work with Koa.

Koa already has over 26,000 GitHub stars. The Express developers built Koa because they wanted a lighter framework that was more expressive and more robust than Express. You can find out more about the differences between Koa and Express here.

Sails

Sails is a real-time, MVC framework for Node that's built on Express. It supports auto-generated REST APIs and comes with an easy WebSocket integration.

The project has over 20,000 stars on GitHub and is compatible with almost all databases (MySQL, MongoDB, PostgreSQL, Redis). It's also compatible with most front-end technologies (Angular, iOS, Android, React, and even Windows Phone).

Nest

Nest has over 15,000 GitHub stars. It uses progressive JavaScript and is built with TypeScript, which means it comes with strong typing. It combines elements of object-oriented programming, functional programming, and functional reactive programming.

Nest is packaged in such a way it serves as a complete development kit for writing enterprise-level apps. The framework uses Express, but is compatible with a wide range of other libraries.

LoopBack

LoopBack is a framework that allows developers to quickly create REST APIs. It has an easy-to-use CLI wizard and allows developers to create models either on their schema or dynamically. It also has a built-in API explorer.

LoopBack has over 12,000 GitHub stars and is used by companies such as GoDaddy, Symantec, and the Bank of America. It's compatible with many REST services and a wide variety of databases (MongoDB, Oracle, MySQL, PostgreSQL).

Hapi

Similar to Express, hapi serves data by intermediating between server-side and client-side. As such, it's can serve as a substitute for Express. Hapi allows developers to focus on writing reusable app logic in a modular and prescriptive fashion.

The project has over 11,000 GitHub stars. It has built-in support for input validation, caching, authentication, and more. Hapi was originally developed to handle all of Walmart's mobile traffic during Black Friday.

Difference between AngularJS, React, Ember, Backbone, and Node.js.

The most common thing between all of them is that they are Single Page Apps. The SPA is a single page where much of the information remains the same and only some piece of data gets modified when you click on other categories/option.

Node.js Tutorial for Beginners | Node.js Crash Course | Node.js Certification Training

This courseis designed for professionals who aspire to be application developers and gain expertise in building real-time, highly-scalable applications in Node.js. The following professionals can go for this course :

Why learn Node.js?

Node.js uses JavaScript - a language known to millions of developers worldwide - thus giving it a much lower learning curve even for complete beginners. Using Node.js you can build simple Command Line programs or complex enterprise level web applications with equal ease. Node.js is an event-driven, server-side, asynchronous development platform with lightning speed execution. Node.js helps you to code the most complex functionalities in just a few lines of code...

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

The Complete Node.js Developer Course (3rd Edition)

Angular & NodeJS - The MEAN Stack Guide

NodeJS - The Complete Guide (incl. MVC, REST APIs, GraphQL)

Docker for Node.js Projects From a Docker Captain

Intro To MySQL With Node.js - Learn To Use MySQL with Node!

Node.js Absolute Beginners Guide - Learn Node From Scratch

React Node FullStack - Social Network from Scratch to Deploy

Selenium WebDriver - JavaScript nodeJS webdriver IO & more!

Complete Next.js with React & Node - Beautiful Portfolio App

Build a Blockchain & Cryptocurrency | Full-Stack Edition