简体   繁体   中英

Stuck with integrating Braintree with Meteor.js

So, I used sudo npm install -g braintree to install the package to a clean meteor project and have the following code:

if (Meteor.isClient){

    Meteor.call('getBraintree')

    braintree.setup("/* very long client token */", 'dropin', {
    container: 'dropin'
    });
};

if (Meteor.isServer) {
  Meteor.startup(function(){
    var braintreeApi = Meteor.npmRequire('braintree'),
        gateway = braintree.connect({
      environment: braintree.Environment.Sandbox,
      merchantId: "merchantId",
      publicKey: "publicKey",
      privateKey: "privateKey"
    });
  });  

Meteor.methods({
  'getBraintree': function getBraintree(){
    var braintreeWeb = Meteor.npmRequire('braintree-web');
    }
  });
};

...and braintree is not defined @ braintree.connect({ (I have sandbox access and all my keys in order). If I used npm to install the package to my Meteor directory, is there anything further that I'd have to do to my packages.js file as shown here , considering npm now works with meteor in v1? More generally, how do I configure a project so npm packages can be installed and used?

Edit: code is updated as of 12/11

Disclaimer: I work for Braintree :) Always feel free to reach out to our support if you are having issues with your integration.

Update : I've created an extremely basic Braintree and Meteor example application that may be of some help you.

Another disclaimer: I know very little about Meteor. I'll try to answer the broader non-meteor specific issues, and update with more meteor specific information if I can get it. Here are few potential issues:

  1. You need both the client and the server side modules for a Braintree integration, braintree is the server-side (node) Braintree library and braintree-web is the client side package. I am not sure on the specifics of consuming a client side npm module in Meteor, so it may be easier for you to use a tool like bower or to hot link to the client side javascript by placing a script tag on your page:

    <script src="https://js.braintreegateway.com/v2/braintree.js"></script>

  2. braintree.connect is a server side method, and as such should only be called on the server (probably once during startup). You'll need it to generate a clientToken to be used on the client side. I have very little experience with Meteor, but I think it is suitable to call braintree.connect within Meteor.startup on the server:

      // a better pattern would be to place this in a server/index.js file // within your project, which Meteor knows to load as server only code Meteor.startup(function () { if (Meteor.isServer) { var braintree = Meteor.npmRequire('braintree'); gateway = braintree.connect({ environment: braintree.Environment.Sandbox, publicKey: process.env.BT_PUBLIC_KEY, privateKey: process.env.BT_PRIVATE_KEY, merchantId: process.env.BT_MERCHANT_ID }); } }); 
  3. braintree.setup should only be run on the client side, it is what interacts with the form on the client side.

Braintree has two packages on npm. One is for the server-side and the other is for client side.

https://www.npmjs.com/package/braintree will provide the server code ( var gateway = braintree.connect… )

https://www.npmjs.com/package/braintree-web on the other hand, will provide the code for running in the browser ( braintree.setup("/*very long client token*/", 'dropin', { ).

In the case of meteor, you may need to include both packages. (Make sure to double check that your private key is kept secret on your server! It may be helpful to use the /server special directory to serve this purpose.)

require is not available inside Meteor like that. Use this package https://github.com/meteorhacks/npm to use npm packages.

这是一个为流星和Braintree同步包装的软件包https://atmospherejs.com/patrickml/braintree

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM