Meteor 应用程序中 PayPal 的 IPN 侦听器
IPN listener for PayPal in Meteor app
我遇到了一个问题。
我正在尝试将 PayPal 按钮与流星应用程序集成。但是为了获得完整的功能,我需要处理 IPN。
因为我至少要知道交易状态。
我已经有企业帐户并且我在路径上打开了 IPN:
http://domein.com/ipn
我试过使用 PayPal documentation,但也没有用。
我花了两天时间,仍然找不到任何有用的东西。
也许有人知道如何在流星应用程序中实现 IPN 侦听器?
编辑: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 requests/responses:
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();
}
}
});
});
我遇到了一个问题。 我正在尝试将 PayPal 按钮与流星应用程序集成。但是为了获得完整的功能,我需要处理 IPN。 因为我至少要知道交易状态。 我已经有企业帐户并且我在路径上打开了 IPN:
http://domein.com/ipn
我试过使用 PayPal documentation,但也没有用。 我花了两天时间,仍然找不到任何有用的东西。 也许有人知道如何在流星应用程序中实现 IPN 侦听器?
编辑: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 requests/responses:
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();
}
}
});
});