How to integrate the CardConnect payment with Node JS

This tutorial shows how we can integrate the CardConnect payment gateway within your nodeJs based projects. We gonna use axios npm module to make request to CardConnect APIs.

Here are simple steps to access the CardConnect APIs and make payments using CardConnect.Authorization

Step 1: Initialize the project with npm init command. It will create a package.json file in the project root folder.

npm init --yes

Step 2: Install the dependencies of the project, as I mentioned above we gonna use <code>axios</code>, it’s a Promise based HTTP client. So let’s install it with below command.

npm install axios

Step 3: We gonna use two APIs, first one is to get payment auth token and the second one is to charge the customer. copy and paste below code in a .js file, ex: payment.js file and run the code.

const axios = require('axios');

const credentials = new Buffer('testing:testing123').toString('base64')

const getAuthTokenForThePayment = async (data) => {

    try {

        const config = {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Basic ${credentials}`
            }
        };

        const URL = 'https://fts.cardconnect.com:6443/cardconnect/rest/auth';

        return await axios.put(URL, data, config);

    } catch (error) {

        throw (error);

    }
}

const makeCharge = async (data) => {

    try {

        const config = {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Basic ${credentials}`
            }
        };

        const URL = 'https://fts.cardconnect.com:6443/cardconnect/rest/capture';

        return await axios.put(URL, data, config);

    } catch (error) {

        throw (error);

    }
}



(async() => {

    const paymentRequest = await getAuthTokenForThePayment({
        account: '4444333322221111',
        merchid: '496160873888',
        amount: '1000', // Smallest currency unit. e.g. 100 cents to charge $1.00
        expiry: '1220',
        currency: 'USD'
    });

    const charge = await makeCharge({
        merchid: paymentRequest.data.merchid, 
        retref: paymentRequest.data.retref
    });

    console.log(charge);

})();

Step 4: Run the above code with below command.

    node payment.js

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

DMCA.com Protection Status