无法使用客户ID创建Braintree客户端令牌

Rob*_*Rob 11 javascript braintree node.js

直接从Braintree的教程复制,您可以使用如下客户ID创建客户端令牌:

gateway.clientToken.generate({
    customerId: aCustomerId
}, function (err, response) {
    clientToken = response.clientToken
});
Run Code Online (Sandbox Code Playgroud)

我声明var aCustomerId = "customer"但是node.js因错误而关闭

new TypeError('first argument must be a string or Buffer')
Run Code Online (Sandbox Code Playgroud)

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

编辑:这是完整的测试代码:

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');
Run Code Online (Sandbox Code Playgroud)

Nic*_*lin 23

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

很遗憾听到您的实施遇到问题.这里有一些可能出错的地方:

  1. 如果指定customerId生成客户端令牌的时间,则必须是有效令牌.在为初次使用客户创建客户端令牌时,您不需要包含客户ID.通常,您在处理提交结帐表单时创建创建客户,然后将该客户ID存储在数据库中以供日后使用.我将与我们的文档团队讨论澄清有关此问题的文档.
  2. res.write采用字符串或缓冲区.由于您正在编写response.clientToken,这是undefined因为它是使用无效的客户ID创建的,因此您收到first argument must be a string or Buffer错误.

其他一些说明:

  • 如果您创建一个带有无效的令牌customerId,或者处理您的请求时出现另一个错误response.success,那么您将可以检查响应是否失败.
  • 您应该在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');
Run Code Online (Sandbox Code Playgroud)

  • 我第一次遇到同样的问题.但是我希望代码能够在第一时间和返回客户一起工作!如果我包含customerId,它会为第一次客户抛出错误,但如果我不包含它,则退货客户将没有保存信用卡.那我该如何正确实现呢?! (3认同)