How to add Payment in Node Js

Payment method is required in most of the e-commerce or any paid subscriber based company. In Node Js application its not very hard to make a payment or you can say any payment method. We are having a E-commerce website and we are going to make a Checkout page.


How payment Process works

  1. We need to verify credit/debit card details,

  2. Is it expired or not

  3. We have to charge and after we charge it we have to manage payments so that includes Fraud protection also.

  4. Managing any disputes and so on

  5. Process the Order in our application.

IN short

  • Collect Payment Method

  • Verify Payment Method

  • Charge Payment Method

  • Manage Payments

  • Process Order in App

Its better to use 3rd party payment method (outsource the Payment method) because its complex and there are problems with dispute and many other problems, We can use many other payment method such as Google Pay, Paytm or Stripe.

How to Use stripe?

Client --------------------------> Collect Credit Card Data

|

Token<---| |

| |--------------| |

| | V

| |------------Stripe Server(3rd Party)

| ^

V |

Server (Node App)--------------------> Create Payment Data


First Lets make the Checkout page

There is a lot we need to do in this module

<%- include('../includes/head.ejs') %> <link rel="stylesheet" href="/css/cart.css"> </head> <body> <%- include('../includes/navigation.ejs') %> <main> <ul class="cart__item-list"> <% products.forEach(p=> { %> <li class="cart__item"> <h1><%= p.productId.title %></h1> <h2>Quantity: <%= p.quantity %> </h2> </li> <% }) %> </ul> <div class="centered"> <h2>Total:<%= totalSum %> </h2> </div> <div class="centered"> <button id= "order-btn" class="btn">ORDER</button> <script src="https://js.stripe.com/v3/"></script> <script> var stripe = Stripe('

//your Key

') var orderBtn = document.getElementById('order-btn') orderBtn.addEventListener('click', function(){ stripe.redirectToCheckout({ sessionId: '<%= sessionId %>' }) }) </script> </div> </main> <%- include('../includes/end.ejs') %>


Controllers

in Shop page

On top we need to write

const stripe = require('stripe')('//your stipe Api key ') our stripe api key


exports.getCheckout = ( req, res, next) => { let products; let total = 0; req.user .populate('cart.items.productId') .execPopulate() .then(user => { products = user.cart.items; total = 0; products.forEach(p => { total += p.quantity * p.productId.price; }) return stripe.checkout.sessions.create({ payment_method_types: ['card'], line_items: products.map(p => { return { name: p.productId.title, description: p.productId.description, amount: p.productId.price * 100, currency: 'INR', quantity: p.quantity }; }), success_url: req.protocol + '://' + req.get('host') + '/checkout/success', //localhost:3000 cancel_url: req.protocol + '://' + req.get('host') + '/checkout/cancel' }) }) .then(session => { res.render('shop/checkout' , { path: '/checkout', pageTitle: 'Checkout', products: products, totalSum: total, sessionId: session.id }) }) .catch(err => { const error = new Error(err) error.httpStatusCode = 500; return next(err); }) }; exports.postCheckoutSuccess = (req, res, next) => { req.user .populate('cart.items.productId') .execPopulate() .then(user => { const products = user.cart.items.map(i => { return { quantity: i.quantity, product: { ...i.productId._doc } }; }); const order = new Order({ user: { email: req.user.email, userId: req.user }, products: products }); return order.save(); }) .then(result => { return req.user.clearCart(); }) .then(() => { res.redirect('/orders'); }) .catch(err => { const error = new Error(err); error.httpStatusCode = 500; return next(error); }); };


in routers

router.get('/checkout', isAuth, shopController.getCheckout); router.get('/checkout/success', shopController.postCheckoutSuccess); router.get('/checkout/cancel', shopController.getCheckout);

0 views0 comments

Recent Posts

See All

SQL UNION Operator UNION operator is used to combine the results of two or more SELECT statements Every SELECT statement within UNION must have the same number of columns The columns must also have si

JOIN clause is used to combine rows from two or more tables. INNER JOIN === selects records that have matching values in both tables SELECT Orders.OrderID, Customers.CustomersID, Orders.OrdersDate FRO