我正在使用 nodejs 和 express。这是我在从 Paypal 返回时运行的代码。我只收到来自 Paypal 的 302 错误响应。我看到了几个使用 ssl://而不是 https://的示例,但是 nodejs 大喊说它不是 https 模块的有效协议(protocol)。有人有用于 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();
});
这个
最佳答案
您缺少 Accept: */*
header 。此外,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);
});
关于node.js - nodejs Paypal pdt 返回 302,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34659232/