1

我正在使用 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();
});

这个

4

2 回答 2

0

你缺少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);
});
于 2017-04-07T08:28:55.217 回答
-1

尝试在没有 JSON 的情况下发布

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

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

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

于 2016-03-04T06:48:28.103 回答