• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

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

April 11, 2020 by

Questions › pass response data to a global variable or pass it to another function nodejs express js
0
Vote Up
Vote Down
Garmaine asked 4 years 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();

});

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: express, javascript, node.js, web

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023