繁体   English   中英

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

[英]Can't create Braintree client token with customer ID

直接从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');

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

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

  1. 如果在生成客户端令牌时指定customerId ,则它必须是有效的。 在为初次使用客户创建客户端令牌时,您不需要包含客户ID。 通常,您在处理提交结帐表单时创建创建客户 ,然后将该客户ID存储在数据库中以供日后使用。 我将与我们的文档团队讨论澄清有关此问题的文档。
  2. res.write接受一个字符串或缓冲区。 由于您编写的是response.clientToken ,由于它是使用无效的客户ID创建的,因此undefined ,您收到的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');

暂无
暂无

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

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