Forum
Perch Shop - Braintree - Generating Client Token
Hi guys,
I'm having some issues generating a Client Token for use with the Drop-in Payment UI.
- Is anybody able to provide some code examples of how we're supposed to do this? I've been going round in circles in the Braintree docs!
- Do we need to generate a fresh Client Token for each visitor?
I've got this so far:
checkout.php:
<?php
perch_shop_cart([
'template'=>'cart/cart_static.html'
]);
perch_shop_order_addresses();
perch_shop_payment_form('braintree');
if (perch_member_logged_in() && perch_post('payment_method_nonce')) {
$return_url = 'https://staging.flykartel.com/shop/success';
perch_shop_checkout('braintree', [
'return_url' => $return_url,
'cancel_url' => 'https://staging.flykartel.com',
'token' => perch_post('payment_method_nonce')
]);
}
?>
braintree_payment_form.html (just the ajax call):
var clientToken =
$.ajax({
type: "GET",
url: "/cms/templates/pages/shop/gen_client_token.php",
success: function(){
console.log('Y')
},
error: function(){
console.log('N')
}
});
braintree.setup(clientToken, "dropin", {
container: 'dropin-container',
paypal: {
singleUse: true,
amount: <perch:shop id="amount" escape="true" />,
currency: '<perch:shop id="currency" escape="true" />'
},
});
gen_client_token.php:
<?php echo($clientToken = Braintree_ClientToken::generate()); ?>
The trouble is that the AJAX call just returns an error about the Braintree_Configuration class not being found, or about how the merchantID has to be set.
Everything's already set up, and the payment gateway does actually work, I managed to generate a client token by echoing it out, copying it and pasting it in to the braintree.setup
call, but if we have to generate a new token for each client, that's obviously not going to work.
Any ideas?
Thanks! Harry
Is this a different implementation method than the basic method we ship with?
Hi Drew,
Nope - not as far as I'm aware. I've followed the Perch Shop docs for the Braintree gateway, but that didn't generate a client token or anything so I've had to piece it together from Braintree's docs too.
One immediate problem I can see is that you're calling
perch_shop_checkout()
after other content has been output to the page.Ah - the reason for that is that if I use:
Before I call the payment form, it doesn't work and throws a fatal error. Perhaps because
braintree.setup()
gets called in the payment form?I can't really support your custom solution. If you want to put together a basic version using the example from the docs then I can help with any issues you have with that.
I've tried using the example in the docs, but I suppose I was over-complicating things.
Am I right in saying that the
token
parameter here is the Client Token that Braintree is looking for?Thanks Drew - I totally over-complicated the example from the docs. Grabbed a fresh download and took the braintree form from there. Sorted!