Implementing a payment gateway on your website in Node JS

Introduction


Implementing a payment gateway on your website in the past was so difficult and expensive. Most programmers when it comes to implementing a payment gateway they begin to do some equations on their mind like costs, security, and implementation.

And not all payment gateway supports international transactions which is why some projects cannot scale easily.

A solution for you

In the last few years, tons of payment gateway has been bombing the internet and they have helped to streamline the process of implementation, costs, and security. Here are few providers:
I used Paystack in one of my project written in Node JS to build an API that can handle payments. Paystack already has an API to work with payments.

To sign up for a test account, create an account with Paystack to get your public and secret keys and they are not very expensive to use. When your ready to go live, then you can sign up for a live account. To get information about the Paystack pricing visit https://paystack.com/pricing


Let's build an app to handle customer subscription for a TV service


Before you begin, the basic steps you need to follow to handle customers subscriptions are:
Initialize transaction: To initialize a transaction for a customer that wants to subscribe for a TV service, call this api(https://api.paystack.co/transaction/initialize) and pass the required body information like amount, email and metadata. Below are the snapshots from Postman:






If you look at the returned data from the snapshot, you will notice an authorization_url and a
reference code. Copy the authorization_url link and paste into your browser, you will be redirected to a secured page to fill in your test card details.



 After you have filled your card details, click on the pay button and all is done. Simple right? The next step is to build our own API to subscribe customers and commit. I have written the code in Node JS to do the job but i would only explain and depict the basics. You can set up an express app to create a basic subscription app that uses MongoDB as your database storage mechanism.

Building the NodeJS App


Before you start coding, open your terminal and run npm install request to install the required package.

exports.addCustomerSubscription = function (req, res) {
    var option = {
        headers: {
            'Authorization': req.headers['key']
        },
        url: 'https://api.paystack.co/transaction/verify/' + req.params.reference
    }
    request(option, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var data = JSON.parse(body).data;
            if (data.status == "abandoned" || data.status == "Abandoned") {
                res.send("Your transaction is not yet complete");
            }
            else if (data.status == "success") {
                //update customer details
                Customer.findOne({ _Id: data.metadata.customerId }).exec(function (err, cust) {
                    if (err) {
                        return res.send(err);
                    }            
                    cust.isactive = true; //Activate customer
                    //cust.email = data.customer.email;
                    cust.save(function (err) {
                        if (err) { return res.send(err); }
                        res.send(cust);
                    });
                });
            }
            else {
                res.send("Transaction error");
            }
        } else {
            return res.send("Reference code not found");
        }
    });
}


Summary

In the code about, you would notice a data.metadata.customerId, which is the metadata that was passed in Postman that contains the customer id. The customer id would be searched for in the database and an update will occur by activating the customer. So, create a basic app and add the customer model to see it works and you don't need to worry much about implementing a payment gateway.


In the next post, I will show you how to do this in Asp.Net MVC.

Comments

  1. The main idea of node.js development is use of non-blocking and event-driven I/O to remain insubstantial and efficient in the face of data-intensive real-time applications which run across distributed devices.
    node.js development Sydney

    ReplyDelete
  2. Great site for these post and i am seeing the most of contents have useful for my Carrier.Thanks to such a useful information.Any information are commands like to share him.
    node js developer london

    ReplyDelete
  3. It is a great information about the Node. Js It will be informative to me.

    Regards,

    PHP web developer

    ReplyDelete

Post a Comment

Popular posts from this blog

Solved: Jwt Authentication in Asp.Net Web Api And Mvc

Sending Notification Message with Firebase with JavaScript and .Net client

Using SignalR with Unity