Phorce
Phorce

Reputation: 4642

Braintree - What to post for paymentMethodNonce

I am just getting started with Braintree and using it's API in PHP.

I have come across one thing, the "paymentMethodNouce" which is posted from the HTML file.

My question, is what does this need to include? I.e. do we post the send the credit card, billing information through here? And in what content does the form need to be in? I.e. Should it be like this:

    <form> 
      <input type="text" name="payment['creditcard']" value="124214124" />
      <input type="text" name"payment['billingaddress']" value="12312313"/>
   </form>

If this is not correct, what actually get's passed through to the paymentMethodNonce and how is the credit card details handled?

Upvotes: 2

Views: 8448

Answers (2)

Will
Will

Reputation: 1193

In addition to WitVault's answer, you have several solutions to go from step 5 to 6. You can send it with an AJAX call or create a hidden input in your form:

<input type="hidden" name="payment_method_nonce">

And when your token is generated, set the latter as value of the input element then submit the form:

var form = document.querySelector('#payment_form');
form.addEventListener('submit', function (event) {
   event.preventDefault();
   hostedFieldsInstance.tokenize(function (tokenizeErr, payload) {
      document.querySelector('input[name="payment_method_nonce"]').value = payload.nonce;
      form.submit();
   });
}, false);

Then step 6 will allow you to retrieve the token.

Upvotes: 0

WitVault
WitVault

Reputation: 24130

Let me describe the procedure

Payment method nonce

The payment method nonce is a string returned by the client SDK to represent a payment method. This string is a reference to the customer payment method details that were provided in your payment form and should be sent to your server where it can be used with the server SDKs to create a new transaction request.

To setup braintree

  1. First configure the environment and API credentials

    Braintree_Configuration::environment('sandbox'); Braintree_Configuration::merchantId('use_your_merchant_id'); Braintree_Configuration::publicKey('use_your_public_key'); Braintree_Configuration::privateKey('use_your_private_key');

  2. Get client token via ajax call from your server

    echo($clientToken = Braintree_ClientToken::generate());

  3. Use client token returned from braintree to set up your form. Your form will be visible only if you have client token available.

  4. Your form will now be rendering on your client. User fills in all info and submits the form.

  5. If eveything went fine braintree will return payment method nonce for that user.
  6. Store the payment method nonce on your server.

    $nonce = $_POST["payment_method_nonce"]

  7. Use that payment method nonce to perform the transaction in future.

    $result = Braintree_Transaction::sale([ 'amount' => '100.00', 'paymentMethodNonce' => nonceFromTheClient ]);

Upvotes: 10

Related Questions