2013-04-05 69 views
1

我试图让我的node.js快递应用程序的paypal ipn工作,我必须验证ipn邮件,一旦我收到它通过“发送回内容的确切顺序他们被接收到并且在它之前使用命令_notify-validate“。他们给出的例子是一个查询字符串是这样的:节点快递bodyParser为贝宝IPN

https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_notify-validate&mc_gross=19.95&protection_eligibility=Eligible&address_status=confirmed&payer_id=LPLWNMTBWMFAY&tax=0.00&...&payment_gross=19.95&shipping=0.00 

但是,因为我使用bodyParser,该request.body是一个JSON对象。如果我要附加“cmd = _notify-validate”并将其发回,我将如何将它作为简单查询字符串接收并将其作为简单查询字符串发送,而不会摆脱bodyParser?我仍然需要这条路由上的json解析版本来实际解释数据。另外,在服务器端发送POST是什么样的? (我只是做res.send(str)?)

回答

1

我用paypal-ipn模块为节点,事实证明json解析的机构是好的。我使用这个模块的主要问题是确保使用res.send(200)做出响应,否则paypal的ipn会每隔一段时间不断发送消息约一分钟。以下是帮助的代码:

exports.ipn = function(req,res){ 
    var params = req.body 
    res.send(200); 

    ipn.verify(params, function callback(err, msg) { 
     if (err) { console.log(err); return false } 

     if (params.payment_status == 'Completed') { 

      // Payment has been confirmed as completed 
      // do stuff, save transaction, etc. 
     } 
    }); 
} 
1

由于您确实询问了如何进行HTTP POST请求,因此您如何操作。

var options = { 
    host: 'example.com', 
    port: '80', 
    path: '/pathname', 
    method: 'POST', 
    headers: { 
    'Content-Type': 'application/x-www-form-urlencoded', 
    'Content-Length': post_data.length 
    } 
}; 

var post_req = http.request(options, function (res) { 
    res.setEncoding('utf8'); 
    var chunks = ''; 
    res.on('data', function (chunk) { 
    chunks += chunk; 
    }); 
    res.on('end', function() { 
    console.log(chunks); 
    }); 
}); 

post_req.write(post_data); 
post_req.end(); 
+0

哦,不错,非常感谢! – 2013-05-01 20:36:11

0

我也为这个IPN努力工作了一两天。我有一个与bodyparser和url编码类似的问题。

以下是NodeJS中的一些工作示例代码,用于侦听传入的IPN消息并根据Paypal沙箱进行验证。

https://github.com/HenryGau/node-paypal-ipn

可以在tests文件夹中运行由mocha subscriptionMessage.js的subscriptionMessage.js模仿/模拟贝宝IPN消息。

希望它有帮助。