当前位置:  开发笔记 > 编程语言 > 正文

Meteor app中的PayPal IPN监听器

如何解决《Meteorapp中的PayPalIPN监听器》经验,为你挑选了1个好方法。



1> bluebird..:

编辑:Meteor 1.3+的更新

我发布了一个大大简化过程的软件包.这个包是普通的: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));

有关更多选项,请参阅自述文件.

原始答案

我也试图解决这个问题.

首先,添加以下包,如果您还没有它们.

meteor add meteorhacks:npm
meteor add meteorhacks:picker

如果你正在使用铁:路由器,那么你实际上不需要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();

});

更新:如果您使用的是铁:路由器,则无需使用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();
            }

        }
    });
  });

推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有