Client SDK
Setup
The Braintree Android SDK helps you accept payments in your Android app.
Requirements
- Android API >= 21
It goes without saying, but we'll say it anyway: we always recommend using the latest versions of our SDKs.
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.
Get the SDK
In your build.gradle
, add the following:
- Groovy
dependencies {
compile 'com.braintreepayments.api:braintree:3.20.1'
}
Then, continue to Browser switch setup below.
Browser switch setup
Some of our payment flows utilize a browser switch, such as PayPal, Venmo, and 3D Secure verification. A URL scheme must be defined to return to your app from the browser.
Edit your AndroidManifest.xml
to include BraintreeBrowserSwitchActivity
and set the android:scheme
:
- Xml
<activity android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"
android:launchMode="singleTask"
android:exported="true">
<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(appCompatActivity, authorization)
with the current AppCompatActivity
and either a client token, or tokenization key.
- Java
@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 AppCompatActivity
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.
- Java
@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
.
- Java
@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.
- Java
@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.