Braintree Web Drop-in
A pre-made payments UI for accepting cards and alternative payments in the browser built using version 3 of the Braintree JS client SDK.
Because we're still in beta, the API and designs are subject to change. If you have any feedback in these areas, create an issue or email us at web-drop-in-beta@getbraintree.com.
What's new
- Updated UI to easily accommodate multiple payment methods
- Not in an iframe; feel free to style Drop-in to blend in with your website
- Open source and open development
Setup
Drop-in is currently available directly from our servers, which you can save locally or include in your project through a script tag:
<script src="https://js.braintreegateway.com/web/dropin/1.0.0-beta.6/js/dropin.min.js"></script>
Basic usage
Drop-in provides a payment method object containing the payment method nonce to send to your server. To get this object, use the requestPaymentMethod
function as shown below.
For credit cards, this attempts to validate the card form and will call the supplied callback with a payload, including the payment method nonce, if successful. If not successful, an error will be shown in the UI and the callback will be called with an error.
Other payment methods may behave differently. Refer to their documentation for details.
In your create
call, provide an authorization
and a selector
:
-
authorization
: Your client authorization should be a client token from your server or a tokenization key that can be found in the Braintree Control Panel. If you pass a customer ID when generating the client token, Drop-in will display that customer's saved payment methods and automatically store any newly-added payment methods in their Vault record.
-
selector
: This must be the selector for an empty element, such as a <div>
, where Drop-in will be included on your page.
var submitButton = document.querySelector('#submit-button');
braintree.dropin.create({
authorization: 'CLIENT_AUTHORIZATION',
selector: '#dropin-container'
}, function (err, dropinInstance) {
submitButton.addEventListener('click', function () {
dropinInstance.requestPaymentMethod(function (err, payload) {
if (err) {
return;
}
});
});
});
The structure of the credit card payment method object returned in the callback of requestPaymentMethod
can be found here.
Using PayPal
If PayPal is enabled for your merchant account, include PayPal configuration options in the create
call. The required flow
property can be either vault
or checkout
, depending on whether you want to use the PayPal Vault or Checkout flow.
braintree.dropin.create({
authorization: 'CLIENT_AUTHORIZATION',
selector: '#dropin-container',
paypal: {
flow: 'checkout',
amount: 10.00,
currency: 'USD'
}
}, callback);
You can find more PayPal configuration options in the Braintree JS client SDK v3 reference.
The structure of the PayPal payment method object returned in the callback of requestPaymentMethod
can be found here.
Full example
This is a full example of a Drop-in integration that only accepts credit cards.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Checkout</title>
</head>
<body>
<div id="dropin-container"></div>
<button id="submit-button">Purchase</button>
<script src="https://js.braintreegateway.com/web/dropin/1.0.0-beta.6/js/dropin.min.js"></script>
<script>
var submitButton = document.querySelector('#submit-button');
braintree.dropin.create({
authorization: 'CLIENT_AUTHORIZATION',
selector: '#dropin-container'
}, function (err, dropinInstance) {
if (err) {
console.error(err);
return;
}
submitButton.addEventListener('click', function () {
dropinInstance.requestPaymentMethod(function (err, payload) {
if (err) {
}
});
});
});
</script>
</body>
</html>
Localization
You can pass a locale
property to translate the Drop-in into other languages. Possible values are:
da_DK
de_DE
en
en_AU
en_GB
es_ES
fr_CA
fr_FR
id_ID
it_IT
ja_JP
ko_KR
nl_NL
no_NO
pl_PL
pt_BR
pt_PT
ru_RU
sv_SE
th_TH
zh_CN
zh_HK
zh_TW
Payment option priority
By default, Drop-in displays the credit/debit card form first, followed by PayPal (if enabled). You can customize this ordering with paymentOptionPriority
as shown in this example:
braintree.dropin.create({
paymentOptionPriority: ['paypal', 'card']
}, );
Payment options omitted from this array will not be offered to the customer.
Teardown
When you want to cleanly tear down anything set up by dropin.create
, use teardown()
. This may be useful in a single-page app.
var dropinInstance;
braintree.dropin.create({
}, function (err, dropin) {
dropinInstance = dropin;
});
dropinInstance.teardown(function (err) {
if (err) { }
});
Beta notes
While in beta, we're still actively working on Drop-in. This means you might have to change your integration when upgrading your Drop-in version. This includes any custom CSS styling applied to data-braintree-id
attributes.
Browser support will be limited during beta and will not include Internet Explorer 9 or 10, but will eventually include all browsers supported by Braintree.js.
Much of the behavior in this version of Drop-in differs from the previous version. At this point, adding the hidden payment_method_nonce
input and automatic form submission (the default behavior in the previous version) are not available.
Here are some of the features we're still working on:
- Event API: An event system to indicate when a payment method can be requested
- Full documentation in the Braintree developer docs and an API reference
- Support for additional types of payment methods
- Support for Internet Explorer 9 and 10