【问题标题】:nodejs paypal pdt return 302nodejs 贝宝 pdt 返回 302
【发布时间】:2016-04-12 02:01:31
【问题描述】:

我正在使用 nodejs 和 express。这是我在从 Paypal 返回时运行的代码。我只收到来自 Paypal 的 302 错误响应。我看到了几个使用 ssl:// 而不是 https:// 但 nodejs 大喊大叫说它不是 https 模块的有效协议的例子。有人有用于 PDT 和 IPN 的可用 nodejs 脚本吗?

var purchaseID = req.query.tx;
var atoken = MYAuthToken;
var postDataArray = {'cmd':'_notify-synch','tx': purchaseID, 'at': atoken}
var postData = JSON.stringify(postDataArray);
console.log(postData);
var options = {
    hostname: 'www.sandbox.paypal.com',
    port: 443,
    path: '/cgi-bin/webscr',
method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': postData.length
        }
    };

    var req = https.request(options, function(res) {
        console.log('STATUS: '+ res.statusCode);
        console.log('HEADERS: '+ JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            console.log('BODY: '+chunk);
        });
        res.on('end', function() {
            console.log('No more data in response.')
        });
    });
    req.on('error', function(e) {
        console.log('problem with request: '+e.message);
    });
    req.write(postData);
    req.end();
});

这个

【问题讨论】:

    标签: node.js paypal paypal-pdt


    【解决方案1】:

    您缺少 Accept: */* 标头。此外,JSON.stringify 不是 application/x-www-form-urlencoded。以下是一些工作代码供您构建:

    var request = require('request');
    var endpoint = 'www.sandbox.paypal.com';
    var options = {
      form: {
        cmd: '_notify-synch',
        tx: tx,
        at: auth
      },
      headers: {
        Accept: '*/*'
      }
    };
    request.post('https://' + endpoint + '/cgi-bin/webscr', options, function(e, r, body) {
      return console.log(body);
    });
    

    【讨论】:

    • 纯客户端 JavaScript 可以吗?我只有 Google Blogger,没有供 require('request') 工作的网络服务器。
    【解决方案2】:

    尝试在不使用 JSON 的情况下发布

    var postData = "cmd=_notify-synch,at=" + at + ",tx=" + tx;

    遇到问题时,我已经编辑了几次。我是节点新手,所以只是通过反复试验破解解决方案。您的帖子使我朝着解决方案前进。所以这是适用于您的代码的 postData。很高兴看到 FAIL 和 SUCCESS 消息通过。注意..需要&的

    var postData = "cmd=_notify-synch&at=" + at + "&tx=" + tx;

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 2016-04-07
      • 2021-02-12
      • 2015-04-30
      • 2018-03-18
      • 2012-01-21
      • 2011-01-26
      • 2018-12-21
      • 2012-05-16
      相关资源
      最近更新 更多