Francisco Prieto
Francisco Prieto

Reputation: 13

pass response data to a global variable or pass it to another function nodejs express js

I have this function which is a post request to an API (app.post), and it sends a token and URL that redirects to the webpay. I need that token in another function (app.get) so I can get the payment status. How can I save that token in a global variable? I tried everything, global variable (var and global.) functions that returns the value of the token and nothing worked. I'm kind of new to nodejs and express.

//Function that returns a token and redirects to webpay
app.post('/realizarPago', async function (request, response) {
var options = {
    'method': 'POST',
    'hostname': 'sandbox.flow.cl',
    'path': '/api/payment/create',
    'headers': {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Cookie': //xxx
    },
    'maxRedirects': 20
  };

  var req = https.request(options, (res) => {
    var chunks = [];

    res.on("data", (chunk) => {
      chunks.push(chunk);
    });


    res.on("end", (chunk) => {
      var body = Buffer.concat(chunks);
      var datos = body.toString();
      datos = JSON.parse(datos);
      // console.log(datos);
      // console.log(datos.token);
      var token = datos.token; //I need this var token to be used in another function (get)
      urlRedirect = urlRedirect + token
      response.redirect(urlRedirect);
    
    });

    res.on("error", function (error) {
      console.error("Error en función /realizarPago");
      res.render('pages/pagoFail');
    });

  });

  var postData = qs.stringify({
    'flowOrder': flowOrder,
    'amount': amount,
    'email': userEmail,
    'urlConfirmation': urlConfirmation,
    'urlReturn': urlReturn,
    'commerceOrder': commerceOrder,
    'paymentMethod': paymentMethod,
    'apiKey': apiKey,
    // 'optional':{ },
    'timeout': 300, //5 minutos para hacer la transacción, o se cierra.
    'subject': subject,
    's': hash
  });
  req.write(postData);
  req.end();

});
    
//I need the token in this function so I can get the payment status, and a new hash
app.get('/pagoSuccess', function (req, res) {

  var strConcat = "apiKey" + apiKey + "token" + token; //need the token to make another hash
  var newHash = CryptoJS.createHmac("sha256", SecretKey).update(strConcat).digest('hex');
 
  var options = {
    'method': 'GET',
    'hostname': 'sandbox.flow.cl',
    'path': '/api/payment/getStatus?apiKey=' + apiKey + '&token=' + token + '&s=' + newHash,//the token //received in the code above has to be here 
   

    'headers': {
      'Cookie': //xxx
    },
    'maxRedirects': 20
  };

  var reqs = https.request(options, function (resp) {
    var chunks = [];

    resp.on("data", function (chunk) {
      chunks.push(chunk);
    });

    resp.on("end", function (chunk) {
      body = Buffer.concat(chunks);
      body = JSON.parse(body);
      //var dat = body
      console.log('json***************', body);
      res.render('pages/pagoSuccess', { body: body })

    });

    resp.on("error", function (error) {
      console.error(error);
    });
  });
  
  reqs.end();

});

Upvotes: 1

Views: 1245

Answers (1)

Ender_The_Xenocide
Ender_The_Xenocide

Reputation: 350

To save the token as a global variable you can place the line var token at the very top, outside of all functions. Then replace var token = datos.token; with token = datos.token;. This should fix it.

Upvotes: 1

Related Questions