Hosted Fields

Setup and Integration

braintree.setupAnchorIcon

Configure your integration using braintree.setup() with "custom" as your integration type:

  1. JavaScript
braintree.setup(CLIENT_AUTHORIZATION, 'custom', options);
ParametersAnchorIcon
ArgumentTypeDescription
CLIENT AUTHORIZATIONStringEither a tokenization key or a client token
optionsObject
KeyTypeDescription
idStringThe reference to the DOM id of your form element.
hostedFieldsObjectSee Hosted Fields options
paypalObjectOptional - see PayPal options
Learn more about the global setup options and callbacks such as onPaymentMethodReceived, onReady, and onError

Basic integrationAnchorIcon

To start using Hosted Fields, you need to create a basic HTML checkout form. You will need to define <div> containers in place of the < input > elements that would normally comprise your credit card input fields (card number, expiration date, and CVV).

Note

Depending on your AVS settings, you may also want to include a postal/ZIP code field for later use in server-side Transaction calls.

Here's a sample form that uses Hosted Fields:

  1. HTML
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Checkout</title>
  </head>
  <body>
    <form action="/" id="my-sample-form" method="post">
      <label for="card-number">Card Number</label>
      <div id="card-number"></div>

      <label for="cvv">CVV</label>
      <div id="cvv"></div>

      <label for="expiration-date">Expiration Date</label>
      <div id="expiration-date"></div>

      <input type="submit" value="Pay" />
    </form>
<script src="https://js.braintreegateway.com/js/braintree-3.111.0.min.js"></script> <script> braintree.setup("CLIENT_AUTHORIZATION", "custom", { id: "my-sample-form", hostedFields: { number: { selector: "#card-number" }, cvv: { selector: "#cvv" }, expirationDate: { selector: "#expiration-date" } } }); </script> </body> </html>
When braintree.setup is executed, it will take over the submit event of #my-sample-form. When a submit is attempted, Braintree.js will securely collect the payment information and attempt to tokenize the credit card.

By default, the form will be submitted with a hidden payment_method_nonce input. However, if you have specified an onPaymentMethodReceived callback, a paymentMethod object containing the nonce will be returned and the form will not be submitted.

For more information about Braintree.js, visit the Setup Guide.

PayPalAnchorIcon

If you have PayPal configured, include the PayPal configuration object inside the custom configuration object. Learn more about specific PayPal settings on the PayPal client guide:

  1. JavaScript
braintree.setup('CLIENT_AUTHORIZATION', 'custom', {
  id: 'my-sample-form',
  hostedFields: {
    // ...
  },
  paypal: {
    // ...
  }
});

CustomizationAnchorIcon

You can find all the Hosted Fields configuration options in the JavaScript reference.

Handling errors withonErrorAnchorIcon

As with onPaymentMethodReceived, the general usage of the onError callback is documented in the Setup method options reference.

Validation errorsAnchorIcon

When client-side validation fails in Hosted Fields, onError will fire with a VALIDATION type. This error payload will also present information about which fields were invalid.

KeyTypeDescription
typeStringVALIDATION
messageString 'Some payment method input fields are invalid.'
'User did not enter a payment method' (if all fields are empty)
detailsObject An object containing specific details of the error, if the fields are not all empty.
KeyTypeDescription
invalidFieldKeysArrayA collection of field key strings, such as number, cvv, postalCode, and expirationDate, which indicates the field(s) containing invalid user input.
isEmptyBooleanfalse
isValidBooleanfalse

If you accept cookies, we’ll use them to improve and customize your experience and enable our partners to show you personalized PayPal ads when you visit other sites. Manage cookies and learn more