Garmaine Staff asked 1 year ago

i have this function wich 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. im kinda 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 //recieved 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();

});