2017-02-14 3 views
0

Ich versuche paypals Knoten SDK zu verwenden, um eine Zahlung Umleitung nach ihrem Beispiel-Code zu tun:Express-Server, paypal Kasse, res.redirect und Uncaught Syntaxerror: unerwartetes Token <in JSON an Position 0

router.post('/', (req,res,next) => { 

    //create a payment: 
    var payReq = JSON.stringify({ 
     intent:'sale', 
     payer:{ 
      payment_method:'paypal' 
     }, 
     redirect_urls:{ 
      return_url:'http://localhost:3000/pay/payment', // this is where they get redirected after paying. 
      cancel_url:'http://localhost:3000/' 
     }, 
     transactions:[{ 
      amount:{ 
      total: req.headers.amount, 
      currency:'AUD' 
      }, 
      description:'eventID: ' + req.headers.eventid + ' userID: ' + req.headers.userid 
     }] 
    }); 

    paypal.payment.create(payReq, (error, payment) => { 
     var links = {}; 

     if(error){ 
      console.log('something went wrong with paypal'); 
      console.error(JSON.stringify(error)); 
     } else { 
      // Capture HATEOAS links 
      payment.links.forEach(function(linkObj){ 
       links[linkObj.rel] = { 
        href: linkObj.href, 
        method: linkObj.method 
       }; 
      }) 

      // If redirect url present, redirect user 
      if (links.hasOwnProperty('approval_url')){ 
       //REDIRECT USER TO links['approval_url'].href 
       console.log('redirecting to ' + JSON.stringify(links['approval_url'])); 
       var redirectUrl = JSON.stringify(links['approval_url'].href); 
       res.redirect(301, redirectUrl); 
      } else { 
       console.error('no redirect URI present'); 
      } 
     } 
    }); 
}); 

Paypals Genehmigung arbeitet und sie mir diese in JSON senden, die ich als die Umleitung Route bin mit, die sich von der Aussage über console.logged wird:

redirecting to {"href":" https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-The-token-goes-here ","method":"REDIRECT"}

Aber nachdem ich ausführen, um die res.redirect meine Browser-Konsole Spießen aus diesem Fehler und die Weiterleitung doesn ' t auftreten, verweist er auf diesen Code in meinem vendor.ca6865e ... .bundle.js: 1787:

t.prototype.json=function(){return"string"==typeof this._body?JSON.parse(this._body):this._body instanceof ArrayBuffer?JSON.parse(this.text()):this._body},t.prototype.text=function(){return this._body instanceof i.a?this._body.toString():this._body instanceof ArrayBuffer?String.fromCharCode.apply.... 

Unexpected token < in JSON at position 0 
    at JSON.parse (<anonymous>) 
    at e.t.json (vendor.ca6865e….bundle.js:1787) 
    at e.project (main.c0aa810….bundle.js:1) 
    at e._next (vendor.ca6865e….bundle.js:474) 
    at e.next (vendor.ca6865e….bundle.js:1) 
    at XMLHttpRequest.h (vendor.ca6865e….bundle.js:1780) 
    at t.invokeTask (vendor.ca6865e….bundle.js:2950) 
    at Object.onInvokeTask (vendor.ca6865e….bundle.js:854) 
    at t.invokeTask (vendor.ca6865e….bundle.js:2950) 
    at e.runTask (vendor.ca6865e….bundle.js:2950) 
    at XMLHttpRequest.invoke (vendor.ca6865e….bundle.js:2950) 


ORIGINAL STACKTRACE: 
    t.handleError @ vendor.ca6865e….bundle.js:1640 
next @ vendor.ca6865e….bundle.js:1068 
e.object.i @ vendor.ca6865e….bundle.js:1117 
e.__tryOrUnsub @ vendor.ca6865e….bundle.js:1 
e.next @ vendor.ca6865e….bundle.js:1 
e._next @ vendor.ca6865e….bundle.js:1 
e.next @ vendor.ca6865e….bundle.js:1 
e.next @ vendor.ca6865e….bundle.js:35 
e.emit @ vendor.ca6865e….bundle.js:1117 
t.triggerError @ vendor.ca6865e….bundle.js:854 
onHandleError @ vendor.ca6865e….bundle.js:854 
t.handleError @ vendor.ca6865e….bundle.js:2950 
e.runTask @ vendor.ca6865e….bundle.js:2950 
invoke @ vendor.ca6865e….bundle.js:2950 


SyntaxError: Unexpected token < in JSON at position 0 
    at JSON.parse (<anonymous>) 
    at e.t.json (vendor.ca6865e….bundle.js:1787) 
    at e.project (main.c0aa810….bundle.js:1) 
    at e._next (vendor.ca6865e….bundle.js:474) 
    at e.next (vendor.ca6865e….bundle.js:1) 
    at XMLHttpRequest.h (vendor.ca6865e….bundle.js:1780) 
    at t.invokeTask (vendor.ca6865e….bundle.js:2950) 
    at Object.onInvokeTask (vendor.ca6865e….bundle.js:854) 
    at t.invokeTask (vendor.ca6865e….bundle.js:2950) 
    at e.runTask (vendor.ca6865e….bundle.js:2950) 
    at XMLHttpRequest.invoke (vendor.ca6865e….bundle.js:2950) 

Antwort

0

Derp, war es die JSON.stringify, dass es zu brechen. Es stellte sich heraus, dass es nicht notwendig war.

Verwandte Themen