编辑:Meteor 1.3+ 的更新
我发布了一个大大简化了流程的软件包。包裹是planefy:paypal-ipn-listener。
先安装再打包:
$ meteor add planefy:paypal-ipn-listener
然后,在服务器代码中:
import { IpnListener } from 'meteor/planefy:paypal-ipn-listener';
const listener = new IpnListener({
path: '/mypath',
allow_sandbox: true // in development
});
listener.onVerified((err, ipn) => console.log(ipn));
listener.onError((err) => console.log(err));
请参阅readme 了解更多选项。
原始答案
我也绞尽脑汁想弄清楚这一点。
首先,如果您还没有以下包,请添加它们。
meteor add meteorhacks:npm
meteor add meteorhacks:picker
如果你使用的是 iron:router,那么你实际上不需要meteorhacks:picker(见底部的更新)
在您的应用程序根目录中创建一个 package.json(如果它不存在),并添加以下内容以告诉 meteorhacks:npm 需要安装哪些 npm 包:
{
"paypal-ipn" : "3.0.0",
"body-parser": "1.14.1",
}
在服务器代码中,配置 Picker 以正确解析 JSON 请求/响应:
const bodyParser = Meteor.npmRequire('body-parser');
Picker.middleware(bodyParser.urlencoded({ extended: false }));
Picker.middleware(bodyParser.json());
同样在服务器代码中,定义一个 Picker 路由来处理传入的 IPN 消息
Picker.route('/ipn', function(params, req, res, next) {
res.writeHead(200);
const ipn = Meteor.npmRequire("paypal-ipn");
// create a wrapped version of the verify function so that
// we can verify synchronously
const wrappedVerify = Async.wrap(ipn,"verify");
let verified = false;
// only handle POSTs
if (req.method === "POST") {
// PayPal expects you to immeditately verify the IPN
// so do that first before further processing:
try {
//second argument is optional, and you'll want to remove for production environments
verified = wrappedVerify(req.body, {"allow_sandbox" : true});
} catch (err) {
// do something with error
}
if (verified === 'VERIFIED') {
let payment = req.body;
// do stuff with payment
}
}
res.end();
});
更新:如果您使用的是 Iron:router,则不需要使用 Picker。您可以直接使用 iron:router 定义一个仅服务器的路由器,如下所示:
Router.map(function () {
this.route('ipn', {
path: '/ipn',
where: 'server',
action: function() {
var ipn = Meteor.npmRequire("paypal-ipn");
var wrappedVerify = Async.wrap(ipn, "verify");
var request = this.request;
var verified;
if (request.method !== 'POST') {
this.next();
} else {
try {
verified = wrappedVerify(request.body, {"allow_sandbox" : true});
} catch (error) {
//do something with error
}
if (verified === "VERIFIED") {
var payment = request.body;
//do something with payment
}
this.next();
}
}
});
});