Search code examples
laravel-4payment-gatewaybraintree

Unknown Payment Method Nonce


My setup is:

I tried to add a new customer as showed in the tutorial which works fine. But when I tried to add a payment method from the Front-End with the given code from the JavaScript SDK. I used the Drop-In-UI which generates me a Payment Method Nonce which is part of my question.

I configured the PHP Backend with the Sandbox credentials and pasted the example Code given in the docs.

When I try to create a user, everything's fine:

$result = Braintree_Customer::create(array(
    'id' => Auth::id(),
    'firstName' => 'Mike',
    'lastName' => 'Jones',
    'company' => 'Jones Co.',
    'email' => 'mike.jones@example.com',
    'phone' => '281.330.8004',
    'fax' => '419.555.1235',
    'website' => 'http://example.com'
));

As soon as it comes to the payment nonce, nothing works:

$result = Braintree_Transaction::sale(array(
    'amount' => '10.00',
    'paymentMethodNonce' => Input::get('payment_method_nonce'),
    'customer' => array(
        'id' => Auth::id()
    ),
    'options' => array(
        'storeInVaultOnSuccess' => true,
    )
));

The server keeps saying 93108: Unknown paymentMethodNonce. This seems a little bit confusing and strange to me since the Input::get('payment_method_nonce') represents a valid string.


Solution

  • After a very long day with intensive studies of the Documentation i finally found the issue.

    You need to pass the Client Token which is generated by the PHP Library (not the CSE Token from the Sandbox!) into the JavaScript snippet provided by Braintree:

    braintree.setup(
            "{{ Braintree_ClientToken::generate(['customerId' => Auth::id()]) }}",
            'dropin', {
                container: 'dropin'
            }
    );
    

    The Drop-In-UI Code is copy+paste from the doc.

    <form id="checkout" method="post" action="/checkout">
        {!! csrf_field() !!}
        <div id="dropin"></div>
        <input type="submit" value="Pay $10">
    </form>
    

    Hopefully someone saves a lot of time with this answer provided.

    Edit: In Laravel 5 you need to manually add the csrf-field (I've updated the code), otherwise you will get a TokenMismatchException from Laravel.