Rob
Rob

Reputation: 15150

How do I insert a Braintree clientToken?

The Braintree documentation says you create an environment as in their example var gateway=braintree.connect(environmental variables) and then create a clientToken by doing

gateway.clientToken.generate({}, function (err, response) {
    var clientToken = response.clientToken;
  });

Then their example says to insert the client token into the form used for payment

braintree.setup("CLIENT-TOKEN-FROM-SERVER", "custom", {id: "checkout"});

but they also state that clientToken is an object. I do not see anywhere how to get the token value and the return value is only a boolean. I do find that gateway.clientToken is reported as an object by using typeof().

On Stackoverflow, I see a couple of people saying the clientToken object is supposed to be a base64 encoded value but how you do this? Shouldn't Braintree's code do that?

So I'm missing a step somewhere or I can't find the right documentation but I'm definitely lost.

EDIT: Going by the response from Braintree, in the answers below, clientToken is not being filled by response.clientToken in any of the forms I've attempted this, which is many. Looking at examples from all over the web, which are few,and Braintree's light documentation, looks like I'm doing everything right.

EDIT2: My solution to the problem is in my answer below.

Upvotes: 0

Views: 1917

Answers (2)

Rob
Rob

Reputation: 15150

The problem lies in where you put the code to interact with Braintree within any node.js calls you make. In my case, I had it inside http.createServer so I could write values out while debugging this but some part of all this won't let that work. I haven't figured the exact details yet.

Also, the example code from Braintree shows clientToken inside the gateway.clientToken.generate() call which would lead me to think that's where it belongs even while not understanding how I was to extract that token value. So moving clientToken outside of that call, something I thought I once tried, solved this at least in part.

I say "in part" because you are to create a new token with every new customer and that can't be accomplished this way. I still need to determine how to generate a new token with every new customer visit. It may be only a matter of making it another function call but I haven't tried it yet.

In addition, I'm still not clear about whether a new customer needs to be created. I read the answer is no but I've also read the answer is yes so confusion reigns but this is probably a different question topic.

Upvotes: 2

agf
agf

Reputation: 176740

I work at Braintree. If you have more detailed questions, please get in touch with our support team.

It sounds like you're confusing gateway.clientToken which is the object that allows you to get a client token, and the client token itself, which is what is received by gateway.clientToken.generates callback -- response.clientToken.

Once you have the token (here, var clientToken), you need to get it to your client. From the "Hello, Server!" docs:

There are a number of ways to get your client token into JavaScript so you can setup Braintree. Many people choose to interpolate the client token into the view which contains the JavaScript to setup Braintree.

Upvotes: 1

Related Questions