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
});
I declare var aCustomerId = "customer"
but node.js closes with the error
new TypeError('first argument must be a string or Buffer')
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).
EDIT: 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');
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
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 writing response.clientToken
, which was undefined
because it was created with an invalid customer id, you were receiving the first argument must be a string or Buffer
error.Some other notes:
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.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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With