2016-01-08 38 views
0

我遇到了一个问题。 我尝试将贝宝按钮与流星应用程序集成。但为了完整的功能,我需要使用IPN进行处理。 因为我必须知道至少交易状态。 我已经有企业帐户,我打开IPN的路径:在流星应用程序中使用PayPal的IPN监听器

http://domein.com/ipn 

我尝试使用PayPal documentation,但它并不能帮助了。 我花了两天时间,仍然找不到任何有用的东西。 也许有人知道如何在流星应用中实现IPN监听器?


回答

5

编辑:更新流星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 

如果您使用的铁:路由器,那么你其实并不需要需要meteorhacks:选择器(见在底部更新)

在应用程序根目录下创建的package.json(如果它不”牛逼已经存在),并添加以下告诉meteorhacks:NPM其中NPM包需要安装:

{ 
    "paypal-ipn" : "3.0.0", 
    "body-parser": "1.14.1", 
} 

在服务器代码,配​​置选择器正确地解析JSON请求/响应:

const bodyParser = Meteor.npmRequire('body-parser'); 
Picker.middleware(bodyParser.urlencoded({ extended: false })); 
Picker.middleware(bodyParser.json()); 

此外,在服务器代码,定义一个选择器路由处理传入的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(); 

}); 

更新:如果您使用的铁:路由器,你不需要使用机械手。你可以只定义服务器只与铁直接路由器:路由器,就像这样:

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(); 
      } 

     } 
    }); 
    }); 
+0

我用铁的路由器,可以安装和使用这两个包? –

+0

你应该可以。选择器仅用于服务器端路由。你也可以用铁路路由器定义一个仅服务器路由,我会更新我的回答 – bluebird

+0

谢谢!我会马上试试 –