Node.js Express Stripe API Payment Gateway Integration to Setup Online Payments Full Tutorial 2020

Node.js Express Stripe API Payment Gateway Integration to Setup Online Payments Full Tutorial 2020

 

Welcome folks today in this post we will be integrating stripe API payments system inside node.js and express. This will be a complete tutorial from scratch. All the source code of the tutorial is shown below. A step by step youtube video is also shown below.

 

x
Node.js Express Stripe API Payment Gateway Integration to Setup Online Payments Full Tutorial 2021-22
 

Live Demo

 

 

You can see the live Demo of the App here

 

 

Get Started

 

 

In order to get started you need to install these dependencies which are listed below

 

npm i express

 

npm i ejs

 

npm i nodemon

 

npm i stripe

 

Now after installing all these dependencies you need to get your publishable key and secret key of your stripe account. Create a stripe account and after that log into it and take these keys from the dashboard like this as shown below.

 

 

 

 

 

 

 

Now we need to create our index.js file of our node.js project and copy paste the following code to it

 

 

 

const express = require('express') 
const bodyparser = require('body-parser') 
const path = require('path') 
const app = express() 

var Publishable_Key = '###publishablekey###'
var Secret_Key = '###secretkey#####'

const stripe = require('stripe')(Secret_Key) 

const port = process.env.PORT || 3000 

app.use(bodyparser.urlencoded({extended:false})) 
app.use(bodyparser.json()) 

// View Engine Setup 
app.set('views', path.join(__dirname, 'views')) 
app.set('view engine', 'ejs') 

app.get('/', function(req, res){ 
    res.render('Home', { 
    key: Publishable_Key 
    }) 
}) 

app.post('/payment', function(req, res){ 

    // Moreover you can take more details from user 
    // like Address, Name, etc from form 
    stripe.customers.create({ 
        email: req.body.stripeEmail, 
        source: req.body.stripeToken, 
        name: 'Gautam Sharma', 
        address: { 
            line1: 'TC 9/4 Old MES colony', 
            postal_code: '110092', 
            city: 'New Delhi', 
            state: 'Delhi', 
            country: 'India', 
        } 
    }) 
    .then((customer) => { 

        return stripe.charges.create({ 
            amount: 7000,    // Charing Rs 25 
            description: 'Web Development Product', 
            currency: 'USD', 
            customer: customer.id 
        }); 
    }) 
    .then((charge) => { 
        res.send("Success") // If no error occurs 
    }) 
    .catch((err) => { 
        res.send(err)    // If some error occurs 
    }); 
}) 

app.listen(port, function(error){ 
    if(error) throw error 
    console.log("Server created Successfully") 
})

 

See also  Build a Typing Speed Test Game in Browser Using HTML5 CSS3 and Javascript Full Project For Beginners

 

 

Here we have imported express and made a simple get request to load our index.html from ejs and also you have to replace our publishable keys and secret keys respectively. Now we need to create that Home.ejs file where we will render the html form which is required for transaction.

 

Make a views/Home.ejs file and copy paste the following code

See also  Koa.js Router GET and POST Request Example in Node.js and Javascript Using Koa-router Library Full Project For Beginners

 

 

<!DOCTYPE html> 
<html> 
<title>Stripe Payment Demo</title> 
<body> 
    <h3>Welcome to Payment Gateway</h3> 
    <form action="payment" method="POST"> 
    <script
        src="//checkout.stripe.com/v2/checkout.js"
        class="stripe-button"
        data-key="<%= key %>" 
        data-amount="7000" 
        data-currency="usd" 
        data-name="Gautam Sharma" 
        data-description="Buy React.js Complete Course" 
        data-locale="auto" > 
        </script> 
    </form> 
</body> 
</html>

 

 

 

 

 

 

 

 

 

 

 

DOWNLOAD SOURCE CODE

 

 

Leave a Reply