2
\$\begingroup\$

I have been trying to create a transaction history request from Braintree using a client ID on Braintree vault, and it took far too long to figure out how, so here is the code on server that I eventually used. Let me know if it can be improved.

Nodejs server side:

    exports.transaction_histroy = function(req, res) {
  if (req.user) {
      //where req.user.braintree id is the id saved on the local mongodb server.
      if (req.user.braintreeid) {
        var stream = require("stream");
        var customerStream = gateway.transaction.search(function (search) {
          search.customerId().is(req.user.braintreeid)
        });
        res.write("[");
        var first = true;       
        customerStream.on("data", function (customer) {
          if (first) {
            first = false;
            var temp =  JSON.stringify(customer);
          } else {
            var temp =  "," + JSON.stringify(customer);
          }
          res.write(temp);
        });
        customerStream.on("end", function () {
          res.write("]");
          res.end();
        });
      }
    } else {
      res.redirect('/signin');
    }
  };

JQuery browser request:

function streamTransactions(){
$.ajax({
    url: '/users/'+user+'/transaction_histroy',
    success: function(data) {
        console.log(JSON.parse(data))
        $('#transaction_histroy').append(
            data
            )
    },
    error : function(err){
        console.log(err)
    }
}); 

HTML:

<div id="transaction_histroy"></div>
\$\endgroup\$
  • \$\begingroup\$ 1) Does this work as intended? 2) How does this differ from your originally intended solution? \$\endgroup\$ – Mast Aug 10 '17 at 7:22
  • 1
    \$\begingroup\$ 1)This works as intended \$\endgroup\$ – JonoD Aug 10 '17 at 7:36
  • 1
    \$\begingroup\$ 2)The worked examples on Braintree dont describe the JSON.parse requirements to return the data client side as an object. Without the string modifications the returned data cannot be worked with. i.e for a table or div html injection. here is the github example link \$\endgroup\$ – JonoD Aug 10 '17 at 7:38
  • \$\begingroup\$ Perfect, I hope you get some great answers. \$\endgroup\$ – Mast Aug 10 '17 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.