node.js - nodejs Paypal pdt 返回 302

标签 node.js paypal paypal-pdt

我正在使用 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/

相关文章:

javascript - 在指定的超时时间内未调用 jasmine 异步回调

Node.js => 类型错误 : Object [object Object] has no method 'save'

microsoft-metro - Paypal Windows8 Checkout SDK 是否支持循环支付?

PayPal PDT 停止返回我的网站

paypal - nopCommerce Paypal PDTHandler

javascript - 自写Node-RED Node 中require(<Module>)的问题

node.js - Nodejs上传base64图像到azure blob存储结果出现 "ResourceNotFound"错误

php - 购物车商品总金额与订单金额不匹配

Paypal 按钮 - 添加变量以返回 URL