greevesh's avatar

JavaScript in Laravel 6.0 Braintree: InvalidArgumentException invalid keys: tokenizationKey

I'm really having trouble knowing what to do next. After rummaging through the Braintree documentation and getting help from the support team I still don't know where to go. I was hoping someone with Braintree knowledge could help me out here.

Here's the error again: JavaScript in Laravel 6.0 Braintree: InvalidArgumentException invalid keys: tokenizationKey

Instead of a client token to pass to the server, I'm using a tokenization key. This is supposed to be simpler because it can be reused and displayed in the script.

Here is the element I'm trying to pass to:

<input name="tokenizationKey" id="tokenizationKey" type="hidden" value="">

And here is the script that's supposed to pass the tokenizationKey:

{{-- BRAINTREE INTEGRATION --}}
<script src="https://js.braintreegateway.com/web/3.57.0/js/client.min.js"></script>
<script src="https://js.braintreegateway.com/web/dropin/1.21.0/js/dropin.min.js"></script>
<script>
    var button = document.querySelector('#submit-payment');
    var tokenizationKey = 'sandbox_csv9z8wd_7x6bffskqkpyhp6g';

    braintree.dropin.create({
        authorization: tokenizationKey, 
        container: '#dropin-container'
    }, function (createErr, instance) {
        button.addEventListener('click', function () {
        instance.requestPaymentMethod(function (requestPaymentMethodErr, payload) {
            // submit payload.nonce to your server
            document.querySelector('#nonce').value = payload.nonce;
            document.getElementById('tokenizationKey').value = tokenizationKey;
            console.log(tokenizationKey); 
        });
        });
    });
</script>
{{-- END BRAINTREE INTEGRATION --}}

When I console.log the tokenizationKey it appears, but for some reason it's being marked as invalid.

Here's the backed that's supposed to receive it:

$gateway = new Braintree\Gateway([
            'environment' => config('services.braintree.environment'),
            'merchantId' => config('services.braintree.merchantId'),
            'publicKey' => config('services.braintree.publicKey'),
            'privateKey' => config('services.braintree.privateKey')
        ]);

        // request()->validate([
        //     'name' => 'required|min:5',
        //     'email' => 'required|min:10',
        //     'address' => 'required|min:10',
        //     'address2',
        //     'country' => 'required',
        //     'postcode' => 'required',
        //     'card-name' => 'required'
        // ]);

        $amount = Cart::total();
        $tokenizationKey = $request->tokenizationKey;

        $result = $gateway->transaction()->sale([
            'amount' => $amount, 
            'tokenizationKey' => $tokenizationKey,         
            'options' => [
                'submitForSettlement' => true
            ]
        ]);

if ($result->success or !is_null($result->transaction)) 
        {
            $transaction = $result->transaction;
            return redirect()->route('confirmation')
            ->with('paymentSuccessMessage', 'Thank you! Your payment has been accepted.
                                             A confirmation email has also been sent.');

            Mail::send(new OrderConfirmed);
            Cart::destroy(); 
        } 

What am I missing?

Thanks in advance

0 likes
0 replies

Please or to participate in this conversation.