无法使用客户ID创buildBraintree客户端令牌

从Braintree的教程直接复制,您可以创build一个客户端令牌与这样的客户ID:

gateway.clientToken.generate({ customerId: aCustomerId }, function (err, response) { clientToken = response.clientToken }); 

我声明了var aCustomerId = "customer"但是node.jsclosures了错误

 new TypeError('first argument must be a string or Buffer') 

当我尝试生成没有customerId的令牌时,一切正常(尽pipe我从来没有得到一个新的客户端令牌,但这是另一个问题)。

编辑:这是完整的testing代码请求:

 var http = require('http'), url=require('url'), fs=require('fs'), braintree=require('braintree'); var clientToken; var gateway = braintree.connect({ environment: braintree.Environment.Sandbox, merchantId: "xxx", //Real ID and Keys removed publicKey: "xxx", privateKey: "xxx" }); gateway.clientToken.generate({ customerId: "aCustomerId" //I've tried declaring this outside this block }, function (err, response) { clientToken = response.clientToken }); http.createServer(function(req,res){ res.writeHead(200, {'Content-Type': 'text/html'}); res.write(clientToken); res.end("<p>This is the end</p>"); }).listen(8000, '127.0.0.1'); 

免责声明:我为Braintree工作:)

我很抱歉听到您的实施遇到问题。 有几件事情可能会在这里出错:

  1. 如果您在生成客户端令牌时指定了customerId ,则该客户端令牌必须是有效的。 为第一次客户创build客户令牌时,不需要包含客户ID。 通常情况下,您会在处理结帐表单提交时创build客户 ,然后将该客户ID存储在数据库中供以后使用。 我会和我们的文档小组讨论如何解决这个问题。
  2. res.write需要一个string或一个缓冲区。 由于您正在编写response.clientToken ,因为它是使用无效的客户ID创build的,所以undefined ,您收到的first argument must be a string or Buffer错误。

其他一些说明:

  • 如果您使用无效的customerId创build令牌,或者在处理请求时出现另一个错误,则response.success将为false,然后可以检查响应以查找失败的原因。
  • 您应该在http请求处理程序中生成您的客户端令牌,这将允许您为不同的客户生成不同的令牌,并更好地处理由您的请求导致的任何问题。

只要你指定一个有效的customerId ,下面的代码应该可以工作

 http.createServer(function(req,res){ // a token needs to be generated on each request // so we nest this inside the request handler gateway.clientToken.generate({ // this needs to be a valid customer id // customerId: "aCustomerId" }, function (err, response) { // error handling for connection issues if (err) { throw new Error(err); } if (response.success) { clientToken = response.clientToken res.writeHead(200, {'Content-Type': 'text/html'}); // you cannot pass an integer to res.write // so we cooerce it to a string res.write(clientToken); res.end("<p>This is the end</p>"); } else { // handle any issues in response from the Braintree gateway res.writeHead(500, {'Content-Type': 'text/html'}); res.end('Something went wrong.'); } }); }).listen(8000, '127.0.0.1');