'Issue With Paytm payment gateway integration(Custom checkout) in nodejs environment

I am using the nodejs environment to integrate PayTm payment gateway with Custom Checkout approach as mentioned in the link and on this process I need to use the Initiate Transaction API. Now the issue is, whenever I am calling the Initiate Transaction API from nodejs the paytm server server is responding as bellow-

{"head":{"requestId":null,"responseTimestamp":"1607066431489","version":"v1"},"body":{"extraParamsMap":null,"resultInfo":{"resultStatus":"U","resultCode":"00000900","resultMsg":"System error"}}}

So it has become hard to make out if I am missing out something on my code or the integration process mentioned in the document has missed something either. Please suggest. My code base is already mentioned below-

`

const https = require('https');
/* import checksum generation utility */
var PaytmChecksum = require("paytmchecksum");
//FOR TEST/STAGING OF APPLICATION
const MERCHANT_ID = 'XXXXXXXXXXXXXXXXXXX';
const MERCHANT_KEY = 'XXXXXXXXXXXXXXX';

function initPayProcess(orderId, payVal, custId, custMobile, custEmail) {
    var paytmParams = {};

    paytmParams.body = {
        "requestType": "Payment",
        "mid": MERCHANT_ID,
        "websiteName": "WEBSTAGING",
        "orderId": orderId,
        "callbackUrl": "",
        "txnAmount": {
            "value": payVal,
            "currency": "INR",
        },
        "userInfo": {
            "custId": custId,
            "mobile": custMobile,
            "email": custEmail,
        },
        "enablePaymentMode": {
            "mode": "BALANCE",
        }
    };

    /*
    * Generate checksum by parameters we have in body
    * Find your Merchant Key in your Paytm Dashboard at https://dashboard.paytm.com/next/apikeys 
    */
    return PaytmChecksum.generateSignature(JSON.stringify(paytmParams.body), MERCHANT_KEY)
        .then(function (checksum) {

            paytmParams.head = {
                "signature": checksum
            };

            var post_data = JSON.stringify(paytmParams);
            var options = {
                /* for Staging */
                hostname: 'securegw-stage.paytm.in',
                
                port: 443,
                path: '/theia/api/v1/initiateTransaction?mid=' + MERCHANT_ID + '&orderId=' + orderId,
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Content-Length': post_data.length
                }
            };

            var response = "";
            var post_req = https.request(options, function (post_res) {
                post_res.on('data', function (chunk) {
                    response += chunk;
                });

                post_res.on('end', function () {
                    console.log('Response: ', response);
                    //return response;
                });
            });

            post_req.write(post_data);
            post_req.end();
            return post_data;
        });
}

module.exports = { getCheckSum, initPayProcess };

`



Solution 1:[1]

I have also stumbled across this issue few months back and fortunately paytm devs replied to my email, had a few email discussions and found below code to be working for me.

Do remember to import Paytm from its node js library as shown in below code.

const Paytm = require("paytmchecksum");
var paytmParams = {};

      paytmParams = {
        body: {
          requestType: "Payment",
          mid: config.PaytmConfig.stag.mid,
          orderId: "ORDS" + new Date().getTime(),
          websiteName: config.PaytmConfig.stag.website,
          txnAmount: {
            value: paymentDetails.amount.toString(),
            currency: "INR",
          },
          userInfo: {
            custId: paymentDetails.customerId,
          },
          disablePaymentMode: [
            {
              mode: "CREDIT_CARD",
            },
          ],
          callbackUrl: process.env.HOST_URL + "/callbackfun",
        },
      };

      /*
       * Generate checksum by parameters we have in body
       * Find your Merchant Key in your Paytm Dashboard at https://dashboard.paytm.com/next/apikeys
       */
      const checksum = await Paytm.generateSignature(
        JSON.stringify(paytmParams.body),
        config.PaytmConfig.stag.key
      );
      // .then(function (checksum) {
      paytmParams.head = {
        channelId: "WAP",
        signature: checksum,
      };

      var post_data = JSON.stringify(paytmParams);

      var options = {
        /* for Staging */
        hostname: "securegw-stage.paytm.in",
        /* for Production */ // hostname: 'securegw.paytm.in',

        port: 443,
        path: `/theia/api/v1/initiateTransaction?mid=${config.PaytmConfig.stag.mid}&orderId=${paytmParams.body.orderId}`,
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          "Content-Length": post_data.length,
        },
      };

      const responsePaytm = await doRequest(options, post_data);
      
      
      
      
const https = require("https");
/**
 * Do a request with options provided.
 *
 * @param {Object} options
 * @param {Object} data
 * @return {Promise} a promise of request
 */
function doRequest(options, data) {
  return new Promise((resolve, reject) => {
    const req = https.request(options, (res) => {
      res.setEncoding("utf8");
      let responseBody = "";

      res.on("data", (chunk) => {
        responseBody += chunk;
      });

      res.on("end", () => {
        resolve(JSON.parse(responseBody));
      });
    });

    req.on("error", (err) => {
      reject(err);
    });

    req.write(data);
    req.end();
  });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dhaval Javia