无法使用客户ID创建Braintree客户令牌 [英] Can't create Braintree client token with customer ID
问题描述
直接从Braintree的教程中复制,您可以创建具有以下客户ID的客户令牌:
Copied directly from Braintree's tutorial, you can create a client token with a customer ID like this:
gateway.clientToken.generate({
customerId: aCustomerId
}, function (err, response) {
clientToken = response.clientToken
});
我声明 var aCustomerId = customer
但是node.js关闭并显示错误
I declare var aCustomerId = "customer"
but node.js closes with the error
new TypeError('first argument must be a string or Buffer')
当我尝试生成没有customerId的令牌时,一切正常(尽管我从未获得新的客户端令牌,但这是另一个问题)。
When I try to generate a token without the customerId, everything works fine (though I never get a new client token but that's another question).
编辑:这是要求的完整测试代码:
Here is the complete test code as requested:
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工作:)
Disclaimer: I work for Braintree :)
很遗憾听到您在实施过程中遇到麻烦。这里有些事情可能会出错:
I'm sorry to hear that you are having trouble with your implementation. There are a few things that might be going wrong here:
- 如果您指定
customerId
生成客户令牌时,它必须是有效的。为初次创建客户的客户令牌时,无需包括客户ID。通常,您在处理时会创建创建客户提交结帐表格,然后将该客户ID存储在数据库中,以备后用。我将与我们的文档团队联系,以澄清有关此文档的信息。 -
res.write
需要一个字符串或一个缓冲区。由于您正在编写response.clientToken
(由于使用无效的客户ID创建的未定义
),因此您收到了第一个参数必须是字符串或缓冲区
错误。
- If you specify a
customerId
when generating a client token, it must be a valid one. You do not need to include a customer id when creating a client token for a first time customers. Typically you would create create a customer when handling the submission of your checkout form, and then store that customer id in a database for use later. I'll talk to our documentation team about clarifying the documentation around this. res.write
takes a string or a buffer. Since you were writingresponse.clientToken
, which wasundefined
because it was created with an invalid customer id, you were receiving thefirst argument must be a string or Buffer
error.
其他注意事项:
- 如果您创建的令牌中包含无效的
customerId
,或者还有另一个错误处理您的请求,response.success
将为假,然后您可以检查响应失败的原因。 - 您应该在http请求处理程序内生成您的客户令牌,这将使您为不同的客户生成不同的令牌,并更好地处理由于您的请求而导致的任何问题。
- If you create a token with an invalid
customerId
, or there is another error processing your request,response.success
will be false, you can then inspect the response for the reason why it failed. - You should generate your client token within the http request handler, this will allow you generate different tokens for different customers, and to better handle any issues that result from your request.
下面的代码应该可以工作,只要您指定有效的 customerId
The following code should work, provided you specify a valid 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');
这篇关于无法使用客户ID创建Braintree客户令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!