【问题标题】:Can't create Braintree client token with customer ID无法使用客户 ID 创建 Braintree 客户端令牌
【发布时间】:2015-01-15 03:50:14
【问题描述】:

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

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

我声明 var aCustomerId = "customer" 但 node.js 关闭并出现错误

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

当我尝试在没有 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');

【问题讨论】:

  • 你能提供更多关于你正在使用的代码的上下文吗?

标签: javascript node.js braintree


【解决方案1】:

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

得知您在实施过程中遇到问题,我们深感遗憾。这里可能会出现一些问题:

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

其他一些注意事项:

  • 如果您使用无效的customerId 创建令牌,或者在处理您的请求时出现另一个错误,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');

【讨论】:

  • @Rob 正确。通常对于第一次购买的客户,您不会有一个 id(这很好),但对于回头客,该 id 将与该客户的详细信息一起存储在您的数据库中,并且您希望在生成客户令牌时使用它。跨度>
  • 我第一次遇到同样的问题。但我希望代码既适用于第一次又适用于回头客!如果我包含 customerId,它会为第一次客户抛出错误,但如果我不包含它,返回客户将没有保存的信用卡。那么我该如何正确实现呢?!
  • @HughHou 不幸的是,如果不添加一些逻辑来处理新客户和现有客户,就无法做到这一点。通常,您会将braintree customerId 存储在应用程序数据存储中的用户模型上。然后,您可以检查用户在您的控制器中是否有 Braintree 客户 ID;如果他们这样做,您将包括在clientToken.generate 的选项中,否则您将创建 Braintree 客户,将其保存到您的数据存储中,并使用它来生成客户端令牌。
  • @NickTomlin 这也不是最佳做法,因为可以在 BT 控制面板上手动删除客户。我建议先尝试找到客户 (developers.braintreepayments.com/reference/request/customer/…) 并采取相应措施。 (仅当客户确实存在时才在生成中包含 customerId)
  • 这应该是官方文档的一部分,在这方面不是很清楚。
猜你喜欢
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
  • 2018-04-27
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
相关资源
最近更新 更多