Configuration

In order to use the Drop-in UI, you'll first need to get a tokenization key from the Control Panel or generate a client token on your server.

Setup

There are several ways to include Braintree's Drop-in in your project.

Gradle

Gradle is the preferred build system for working with the Braintree Android SDK.

In your app-level build.gradle, add the following:

Groovy
dependencies {
  implementation 'com.braintreepayments.api:drop-in:3.7.1'
}

Maven

You must be using android-maven-plugin.

In your pom.xml add the following:

XML
<dependencies>
  <dependency>
    <groupId>com.braintreepayments.api</groupId>
    <artifactId>drop-in</artifactId>
    <version>[3.0.0,)</version>
    <type>aar</type>
    <configuration>
      <manifestMergeLibraries>true</manifestMergeLibraries>
    </configuration>
  </dependency>
</dependencies>

Client-side implementation

Starting Drop-in

If you're going to launch the Drop-in UI from a button, and you've set the button's onClick handler in XML via android:onClick="onBraintreeSubmit", add the method as:

Java
public void onBraintreeSubmit(View v) {
  DropInRequest dropInRequest = new DropInRequest()
    .clientToken(clientToken);
  startActivityForResult(dropInRequest.getIntent(this), REQUEST_CODE);
}

When your user provides payment information, your app receives a paymentMethodNonce in your calling Activity#onActivityResult, which you will override to get the response or any possible errors:

Java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == REQUEST_CODE) {
    if (resultCode == RESULT_OK) {
      DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
      // use the result to update your UI and send the payment method nonce to your server
    } else if (resultCode == RESULT_CANCELED) {
      // the user canceled
    } else {
      // handle errors here, an exception may be available in
      Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
    }
  }
}

Configuring payment methods

Additional steps are required for the Drop-in UI to accept payment methods other than cards. After completing the Drop-in setup instructions, follow the steps below for each payment method type.

PayPal

In order for your Drop-in to support PayPal payments, you must allow for PayPal's browser switch by adding an intent filter to your AndroidManifest.xml.

Google Pay

In order for your Drop-in to support Google Pay, you must ensure you've added the required meta-data tag in your AndroidManifest.xml. Then, construct a GooglePaymentRequest object and pass it to your DropInRequest object.

Java
private void enableGooglePay(DropInRequest dropInRequest) {
    GooglePaymentRequest googlePaymentRequest = new GooglePaymentRequest()
      .transactionInfo(TransactionInfo.newBuilder()
        .setTotalPrice("1.00")
        .setTotalPriceStatus(WalletConstants.TOTAL_PRICE_STATUS_FINAL)
        .setCurrencyCode("USD")
        .build())
      // We recommend collecting and passing billing address information
      // with all Google Pay transactions as a best practice.
      .billingAddressRequired(true);
      // Optional in sandbox; if set in sandbox, this value must be
      // a valid production Google Merchant ID.
      .googleMerchantId("merchant-id-from-google");

    dropInRequest.googlePaymentRequest(googlePaymentRequest);
}

If using a client token with a customer id, the Google Pay card will not automatically be vaulted. You can use the payment method nonce to create a payment method on your server.

Venmo

To support Venmo payments in the Drop-in UI, make sure to follow the browser switch setup instructions in the Client SDK Setup and complete the full Venmo integrations.

Displaying the last used payment method

If your user already has an existing payment method, you may not need to show Drop-in. You can check if they have an existing payment method using DropInResult#fetchDropInResult. A payment method will only be returned when using a client token created with a customer_id.

Java
DropInResult.fetchDropInResult(activity, clientToken, new DropInResult.DropInResultListener() {
    @Override
    public void onError(Exception exception) {
        // an error occurred
    }

    @Override
    public void onResult(DropInResult result) {
        if (result.getPaymentMethodType() != null) {
            // use the icon and name to show in your UI
            int icon = result.getPaymentMethodType().getDrawable();
            int name = result.getPaymentMethodType().getLocalizedName();

            PaymentMethodType paymentMethodType = result.getPaymentMethodType();
            if (paymentMethodType == PaymentMethodType.ANDROID_PAY || paymentMethodType == PaymentMethodType.GOOGLE_PAYMENT) {
                // The last payment method the user used was Android Pay or Google Pay.
                // The Android/Google Pay flow will need to be performed by the
                // user again at the time of checkout.
            } else {
                // Use the payment method show in your UI and charge the user
                // at the time of checkout.
                PaymentMethodNonce paymentMethod = result.getPaymentMethodNonce();
            }
        } else {
            // there was no existing payment method
        }
    }
  }
});

Next steps

Next Page: Customization →