Skip to content

How to integrate the CardConnect payment with Node JS

Last updated on November 21, 2022

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

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

Step 1: Initialize the project with the 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 the below command.

npm install axios

Step 3: We gonna use two APIs, first one is to get a payment auth token and the second one is to charge the customer. copy and paste the 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 the below command.

node payment.js
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments