简体   繁体   English

Meteor Braintree - 通过Meteor方法创建客户端令牌

[英]Meteor Braintree — Create Client Token via Meteor Method

I'm trying to get Braintree Payments working in a Meteor app. 我正在努力让Braintree Payments在Meteor应用程序中运行。 I'm stuck at trying to return the result of generating a token (server side, via a Meteor Method) to be used client side. 我一直试图返回生成令牌(服务器端,通过Meteor方法)的结果,以便在客户端使用。

I've tried this: 我试过这个:

/server/braintree.js /server/braintree.js

Meteor.methods({
  createClientToken: function() {

    var token = gateway.clientToken.generate({
        customerId: this.userId
      }, function(err, response) {
          clientToken = response.clientToken
          return clientToken
        }
      )

    console.log(token)
    return token
  }
})

which returns true . 返回true

I've also tried this: 我也试过这个:

Meteor.methods({
  createClientToken: function() {

    var clientToken
    gateway.clientToken.generate({
        customerId: this.userId
      }, function(err, response) {
          clientToken = response.clientToken
        }
      )

    console.log(clientToken)
    return clientToken
  }
})

Which returns undefined . 哪个返回undefined

The function(err, response) is being called asynchronously, yes? function(err, response)被异步调用,是吗? If so, that would be the explanation of the problem. 如果是这样,那就是问题的解释。 Seems that trying to return a value from an asynchronous function is a bit of a pain point in Javascript. 似乎尝试从异步函数返回值是Javascript中的一个痛点。 I've read a number of SO answers on it (like this one , this one and this one ) but none have seemed to lead me in the right direction. 我已经阅读了一些SO答案(就像这一个这一个这一个 ),但似乎没有一个让我朝着正确的方向前进。

Also, I believe I may need to be using Meteor's wrapAsync method, correct? 另外,我相信我可能需要使用Meteor的wrapAsync方法,对吗? I've tried this (and found this and this relevant SO questions on it), but still can't seem to get things right. 我已经试过这(发现这个这个相关的等等吧题),但似乎仍不能得到正确的事情。

Grateful for any feedback. 感谢任何反馈。

Update: 更新:

For a working approach to integrating Braintree with Meteor, check out the example repo (many thanks @Nick Tomlin for this) 有关将Braintree与Meteor集成的工作方法,请查看示例repo (非常感谢@Nick Tomlin)

Disclaimer: I work for Braintree :) 免责声明:我为Braintree工作:)

I'm not familiar with Meteor, but as @mrak noted clientToken.generate is asynchronous and you will definitely handle that appropriately in your method. 我不熟悉Meteor,但正如@mrak所说, clientToken.generate是异步的,你肯定会在你的方法中适当地处理它。

In your current code, clientToken is undefined because console.log(clientToken) executes immediately, before you receive a clientToken from the callback for clientToken.generate . 在当前的代码, clientToken是不确定的,因为console.log(clientToken)立即执行,您会收到之前clientToken从回调clientToken.generate Asynchronous programming can take a while to wrap your head around if you are used to coding in a synchronous matter, but there are many resources out there to help you (here is one ). 如果您习惯于在同步问题中编码,异步编程可能需要一段时间才能完成,但是有很多资源可以帮助您(这里是一个 )。

It appears that Meteor.wrapAsync will indeed provide what you need, here is an untested example implementation. 看来Meteor.wrapAsync确实会提供你需要的东西,这是一个未经测试的示例实现。

Meteor.methods({
  createClientToken: function() {
    var createToken = Meteor.wrapAsync(gateway.clientToken.generate, gateway.clientToken);

    var response = createToken({});

    return response.clientToken;
  }
});

Update 更新

I've created a very basic Braintree + Meteor application that may be of some use to you (if it is not, please file an issue on the GH repo to help improve it!) 我已经创建了一个非常基本的Braintree + Meteor应用程序 ,可能对您有用(如果不是,请在GH repo上提出问题以帮助改进它!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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