2

I am trying to integrate paytm payment gateway to my node.js application. Whats happening is whenever i am trying to pay through my pautm number, it sends me OTP and when i enter the OTP and cick on verify it dissappeas and the next option with card payment is being selected. Again if I try to pay with card, the payment is done successfully but it doesnot redirects me anywhere and times out.

Here is my code, please tell me what i am doing wrong.

const checksumLib = require('./paytm/checksum/checksum');
const port = 8000;
var today = new Date();


app.post('/payment', function(req, res) {
    var payDetails = {
        number: req.body.number,
        amount: req.body.amount
    };
    let params = {};
    params['MID'] = 'XXXXXXXXXXXXXXXXXXXXX',
    params['WEBSITE'] = 'WEBSTAGING',
    params['CHANNEL_ID'] = 'WEB',
    params['INDUSTRY_TYPE_ID'] = 'Retail',
    params['ORDER_ID'] = 'ORD' + today.getDate()+today.getHours()+today.getMinutes()+today.getSeconds(),
    params['CUST_ID'] = 'CUST' + today.getDate()+today.getHours()+today.getMinutes()+today.getSeconds(),
    params['TXN_AMOUNT'] = payDetails.amount,
    params['CALLBACK_URL'] = 'http://localhost:' + port + '/callback',
    params['MOBILE_NO'] = payDetails.number

    checksumLib.genchecksum(params, 'XXXXXXXXXXXX', function(err, checksum) {
        let txn_url = "https://securegw-stage.paytm.in/order/process";
        let form_fields = "";
        for(x in params) {
            form_fields+= "<input type='hidden' name='"+x+"' value='"+params[x]+"' >"
        }
        form_fields += "<input type='hidden' name='CHECKSUMHASH' value='"+checksum+"' >";
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write('<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" action="'+txn_url+'" name="f1">'+form_fields+'</form><script type="text/javascript">document.f1.submit();</script></body></html>');
        res.end();
    });
});

app.post('/callback', (req, res) => {
    var body = '';
  
    req.on('data', function (data) {
       body += data;
    });
  
     req.on('end', function () {
       var html = "";
       var post_data = qs.parse(body);
  
       // received params in callback
       console.log('Callback Response: ', post_data, "\n");
  
  
       // verify the checksum
       var checksumhash = post_data.CHECKSUMHASH;
       // delete post_data.CHECKSUMHASH;
       var result = checksum_lib.verifychecksum(post_data, 'XXXXXXXXXX', checksumhash);
       console.log("Checksum Result => ", result, "\n");
  
  
       // Send Server-to-Server request to verify Order Status
       var params = {};
       params['MID'] = 'XXXXXXXXXXXXX';
       params['ORDER_ID'] = 'ORD' + today.getDate()+today.getHours()+today.getMinutes()+today.getSeconds();
  
       checksum_lib.genchecksum(params, 'PdMi4rHSpiUzccv!', function (err, checksum) {
  
        params["CHECKSUMHASH"] = checksum;
        var post_data = JSON.stringify(params);
  
         var options = {
           hostname: 'securegw-stage.paytm.in', // for staging
           // hostname: 'securegw.paytm.in', // for production
           port: 443,
           path: '/order/status',
           method: 'POST',
           headers: {
             'Content-Type': 'application/x-www-form-urlencoded',
             'Content-Length': post_data.length
           }
         };
  
  
         // Set up the request
         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('S2S Response: ', response, "\n");
  
             var _result = JSON.parse(response);
               if(_result.STATUS == 'TXN_SUCCESS') {
                   res.send('payment sucess')
               }else {
                   res.send('payment failed')
               }
             });
         });
  
         // post the data
         post_req.write(post_data);
         post_req.end();
        });
       });
  });

1 Answers1

0

just add https this solved the issue. https://localhost/