The Braintree Android SDK helps you accept payments in your Android app.

Requirements

  • Android API >= 10

Installation

There are several ways to include Braintree in your project, but Gradle is the preferred build system for working with the Braintree Android SDK.

Build systems

Gradle

In your build.gradle, add the following:

Groovy
Copy
Copied
dependencies {
  compile 'com.braintreepayments.api:braintree:1.7.10'
}

Then, continue to Initialization below.

Maven

You must be using android-maven-plugin 4.1.0 or higher.

In your pom.xml add the following:

XML
Copy
Copied
<dependencies>
  <dependency>
    <groupId>com.braintreepayments.api</groupId>
    <artifactId>braintree</artifactId>
    <version>[1.0.0,)</version>
    <type>aar</type>
    <configuration>
      <manifestMergeLibraries>true</manifestMergeLibraries>
    </configuration>
  </dependency>
</dependencies>

Then, continue to Initialization below.

Manual

availability

braintree-api is a subset of braintree and does not include the Drop-in UI. You will only be able to use the Drop-in UI using Maven or Gradle.

Download braintree-api from Maven Central and include it in your libs folder.

note

The full JAR includes the PayPal Android SDK and Gson, which could cause conflicts if you are already including them.

Next, add the following to your AndroidManifest.xml depending on which features you are using:

XML
Copy
Copied
<!-- Required permissions for all integrations -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<!-- Required to use PayPal -->
<activity android:name="com.paypal.android.sdk.payments.PayPalProfileSharingActivity" />
<activity android:name="com.paypal.android.sdk.payments.ProfileSharingConsentActivity" />
<activity android:name="com.paypal.android.sdk.payments.FuturePaymentInfoActivity" />
<activity android:name="com.paypal.android.sdk.payments.LoginActivity" />
<activity android:name="com.paypal.android.sdk.payments.PayPalTouchActivity"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<service android:name="com.paypal.android.sdk.payments.PayPalService"
    android:exported="false" />

<!-- Required to use 3D Secure -->
<activity android:name="com.braintreepayments.api.threedsecure.ThreeDSecureWebViewActivity" />

Then, continue to Initialization below.

Initialization

The setup of Braintree is asynchronous and requires a callback. Calling Braintree#setup with a client token from your server will provide your callback with an instance of Braintree or an error. Check setupSuccessful before attempting to use Braintree.

Java
Copy
Copied
Braintree.setup(this, CLIENT_TOKEN_FROM_SERVER, new BraintreeSetupFinishedListener() {
  @Override
  public void onBraintreeSetupFinished(boolean setupSuccessful, Braintree braintree, String errorMessage, Exception exception) {
    if (setupSuccessful) {
      // Braintree is now setup and available for use
    } else {
      // Braintree could not be initialized, check errors and try again
      // This is usually a result of a network connectivity error
    }
  }
});

Register listeners

We provide several interfaces which will be called when events occur. If you are using Braintree#onResume and Braintree#onPause in your Activity, any Braintree interface that your Activity implements will be automatically managed for you. However, if you wish to use Braintree outside of an Activity or manage the listeners yourself, use Braintree#addListener. In this case, be sure to use Braintree#removeListener to clean up in the event of a state change to avoid memory leaks.

The following interfaces may be implemented to receive events:

  • Braintree#PaymentMethodCreatedListener is called when a PaymentMethod has been successfully tokenized. Send the nonce from this PaymentMethod to your server to create a transaction.

    Java
    Copy
    Copied
    @Override
    public void onPaymentMethodCreated(PaymentMethod paymentMethod) {
      // Send this nonce to your server
      String nonce = paymentMethod.getNonce();
    }
  • Alternatively, Braintree#PaymentMethodNonceListener can be implemented to receive only the nonce.

    Java
    Copy
    Copied
    @Override
    public void onPaymentMethodNonce(String paymentMethodNonce) {
      // Send this nonce to your server
    }
  • Braintree#PaymentMethodUpdatedListener is called with a list of the user's payment methods.

    Java
    Copy
    Copied
    @Override
    public void onPaymentMethodsUpdated(List<PaymentMethod> paymentMethods) {
      // Display the payment methods to the user
    }
  • Braintree#ErrorListener is called when there has been an error. onUnrecoverableError is called when an error such as a network issue or sever error occurs. onRecoverableError is called when there are validation errors with the request.

    Java
    Copy
    Copied
    @Override
    public void onUnrecoverableError(Throwable throwable) {
      // Braintree has encountered an error, check the error and try again.
    }
    
    @Override
    public void onRecoverableError(ErrorWithResponse error) {
      BraintreeError cardErrors = error.errorFor("creditCard");
      if (cardErrors != null) {
        // There is an issue with the credit card.
        BraintreeError expirationMonthError = cardErrors.errorFor("expirationMonth");
        if (expirationMonthError != null) {
          // There is an issue with the expiration month.
          setErrorMessage(expirationMonthError.getMessage());
        }
        // ...
      }
      // ...
    }

Notes

Recreating an Activity

Braintree includes an onSaveInstanceState and restoreSavedInstanceState method in order to avoid calling Braintree#setup multiple times when an Activity has been destroyed and recreated due to rotation.

Call Braintree.onSaveInstanceState in Activity#onSaveInstanceState:

Java
Copy
Copied
@Override
protected void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);

  if (mBraintree != null) {
    mBraintree.onSaveInstanceState(outState);
  }
}

Call Braintree.restoreSavedInstanceState in Activity#onCreate:

Java
Copy
Copied
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  mBraintree = Braintree.restoreSavedInstanceState(this, savedInstanceState);
  if (mBraintree != null) {
    // mBraintree is ready to use
  } else {
    Braintree.setup(this, clientToken, this);
  }
}

Suspending listeners

In the event an activity is unable to handle requests (e.g. paused due to device rotation), events can be paused until listeners are reconnected.

For example, we handle device rotation in Drop-in by overriding onPause and onResume:

Java
Copy
Copied
protected void onPause() {
  super.onPause();

  mBraintree.lockListeners();
  mBraintree.removeListener(this);
}

protected void onResume() {
  super.onResume();

  mBraintree.addListener(this);
  mBraintree.unlockListeners();
}

ProGuard

If you are compiling your app with ProGuard, add the following lines to your ProGuard config:

-dontwarn com.google.android.gms.**

See also

Still have questions?

If you can’t find an answer, contact us