The Braintree Android SDK helps you accept payments in your Android app.
Requirements
- Android API >= 16
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:
dependencies {
compile 'com.braintreepayments.api:braintree:2.+'
}
Then, continue to Browser switch setup below.
Maven
You must be using android-maven-plugin
4.1.0 or higher.
In your pom.xml
add the following:
<dependencies>
<dependency>
<groupId>com.braintreepayments.api</groupId>
<artifactId>braintree</artifactId>
<version>[2.0.0,)</version>
<type>aar</type>
<configuration>
<manifestMergeLibraries>true</manifestMergeLibraries>
</configuration>
</dependency>
</dependencies>
Then, continue to Browser switch setup below.
Browser switch setup
For PayPal a URL scheme must be defined to accept return browser switches.
Edit your AndroidManifest.xml
to include BraintreeBrowserSwitchActivity
and set the android:scheme
:
<activity android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="${applicationId}.braintree" />
</intent-filter>
</activity>
Initialization
The setup of BraintreeFragment
is simply a call to BraintreeFragment.newInstance(activity, authorization)
with the current Activity
and either a client token, or tokenization key.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
mBraintreeFragment = BraintreeFragment.newInstance(this, mAuthorization);
// mBraintreeFragment is ready to use!
} catch (InvalidArgumentException e) {
// There was an issue with your authorization string.
}
}
This static method will:
- Prepare a
BraintreeFragment
. - Add the
BraintreeFragment
to the given activity. - Check for validity on the given
mAuthorization
string.
Register listeners
Braintree provides several interfaces which will be called when events occur. Any Braintree interface that BraintreeFragment
's host Activity
implements will be automatically managed for you. However, if you wish to manage the Braintree listeners yourself, use BraintreeFragment#addListener
. In this case, be sure to use BraintreeFragment#removeListener
to clean up in the event of a state change to avoid memory leaks.
The following interfaces may be implemented to receive events:
PaymentMethodNonceCreatedListener
is called when aPaymentMethodNonce
has been successfully tokenized. Send the nonce from thisPaymentMethodNonce
to your server to create a transaction.JavaCopyCopied@Override public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) { // Send this nonce to your server String nonce = paymentMethodNonce.getNonce(); }
ConfigurationListener
is called after configuration has been retrieved from Braintree. TheConfiguration
class contains information about your current environment as well as information about which payment methods are currently available.BraintreeCancelListener
is called whenBraintreeFragment
is notified of aActivity#RESULT_CANCELED
result code inFragment#onActivityResult
.JavaCopyCopied@Override public void onCancel(int requestCode) { // Use this to handle a canceled activity, if the given requestCode is important. // You may want to use this callback to hide loading indicators, and prepare your UI for input }
BraintreeErrorListener
is called when there has been an error.ErrorWithResponse
is called when there are validations errors with the request.Exception
is thrown when an error such as a network issue or server error occurs.JavaCopyCopied@Override public void onError(Exception error) { if (error instanceof ErrorWithResponse) { ErrorWithResponse errorWithResponse = (ErrorWithResponse) error; BraintreeError cardErrors = errorWithResponse.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()); } } } }
ProGuard
A ProGuard configuration is provided as part of the Braintree Android SDK. There is no need to add any Braintree specific rules to your ProGuard configuration.
See also
- Get Started
- Braintree Android SDK reference
- Braintree Android GitHub repository
- Braintree Android version changelog