Client SDK

Setupanchor

important

The SSL certificates for all Braintree SDKs are set to expire by June 30, 2025. This will impact existing versions of the SDK in published versions of your app. To reduce the impact, upgrade the Android SDK to version 4.45.0+ or version 5.0.0+ for the new SSL certifications.

If you do not decommission your app versions that include the older SDK versions or force upgrade your app with the updated certificates by the expiration date, 100% of your customer traffic will fail.

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

Requirementsanchor

  • Minimum supported Android API 23
  • Requires Gradle JDK 17+
  • Requires Kotlin 1.9.10+
  • Requires Android Gradle Plugin 8.1.4+ (Giraffe or higher)

It goes without saying, but we'll say it anyway: we always recommend using the latest versions of our SDKs.

note

Our Android SDK and its modules follow semantic versioning. When updating your integration, be sure to update each module to the latest version.

Installationanchor

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 SDKanchor

In your build.gradle, add the dependencies for only the Braintree payment features you wish to use:

  1. Kotlin
  2. Groovy
dependencies {
    // to offer card payments
    implementation("com.braintreepayments.api:card:5.2.0")

    // to collect device data
    implementation("com.braintreepayments.api:data-collector:5.2.0")

    // to offer PayPal
    implementation("com.braintreepayments.api:paypal:5.2.0")

    // to offer local payments
    implementation("com.braintreepayments.api:local-payment:5.2.0")

    // to offer Google Pay
    implementation("com.braintreepayments.api:google-pay:5.2.0")

    // to perform 3DS verification 
    implementation("com.braintreepayments.api:three-d-secure:5.2.0")

    // to offer Venmo
    implementation("com.braintreepayments.api:venmo:5.2.0")
}

Some of our payment flows utilize App Links for returning from the payment experience back into your app. Follow the steps for setting up App Links.

Before using this feature, you must register your App Link domain in the Braintree Control Panel:

  1. Log into your Control Panel (e.g. Sandbox, or Production).
  2. Click on the gear icon in the top right corner. A drop-down menu will open.
  3. Select Account Settings from the drop-down menu.
  4. In the Processing Options tab, go to Payment Methods section.
  5. Next to PayPal, click the Link Sandbox link. This will give you option to link your Braintree and PayPal accounts.
    • If your accounts are already linked, you'd see an Options button instead.
  6. Click the View Domain Names button. This will take you to the PayPal Domain Names page.
    • Note: If you have a single PayPal account, it will be at the bottom of the page. If you have multiple PayPal accounts, it will be at the top right of the page.
  7. Click the + Add link on the top right of the page or scroll to the Specify Your Domain Names section.
  8. In the text box enter your list of domain names separated by commas.
    • Note: The value you enter must match your fully qualified domain name exactly – including the "www." if applicable.
  9. Click the Add Domain Names button.
  10. If the domain registration was successful for all the domain names listed in the text box, a banner will display the text "Successfully added domains". The registered domain names will be displayed in alphabetical order under the + Add button.
  11. If the registration was not successful for any of the domain names listed in the text box, a banner will display a list of domain names that failed qualified domain name validation along with their reasons for rejection. Any domain names that were successfully registered will be displayed in alphabetical order under the + Add button.
    • Note: You can re-enter the rejected domain names in the text area with the corrections applied.

Pass your App Link to the Client constructor (this example is using creating a PayPalClient):

  1. Kotlin
val payPalClient = PayPalClient(
    context = this,
    authorization = "[TOKENIZATION_KEY]",
    appLinkReturnUrl = Uri.parse("https://merchant-app.com") // Merchant App Link
)

Add an intent-filter to your AndroidManifest.xml file:

  1. Xml
<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="https" />
    <data android:host="merchant-app.com"/>
    <!-- required if your app link contains a path -->
    <data android:pathPrefix="/path" />
</intent-filter>

If your App Link contains a path, make sure to add <data android:pathPrefix="/[path]" />.

Browser switch setupanchor

Some of our payment flows utilize a browser switch. A URL scheme must be defined to return to your app from the browser.

Edit your AndroidManifest.xml to include an intent-filter and set the android:scheme on your Activity that will be responsible for handling the deep link back into the app:

  1. Xml
<activity android:name="com.company.MyActivity"
    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>
note

The android:exported attribute is required if your app compile SDK version is API 31 (Android 12) or later.

important

Your app's URL scheme must begin with your app's package ID and end with .braintree. For example, if the Package ID is com.your-company.your-app, then your URL scheme should be com.your-company.your-app.braintree. ${applicationId} is automatically applied with your app's package when using Gradle.

Initializationanchor

Each payment method type requires authorization in the form of a Tokenization Key or Client Token.

Authorizationanchor

A tokenization key can be passed to the payment method Client class constructors.

The example below shows a PayPalClient initialization with a tokenization key authorization:

  1. Kotlin
val payPalClient = PayPalClient(
    context = this,
    authorization = "[TOKENIZATION_KEY]",
    appLinkReturnUrl = Uri.parse("https://merchant-app.com") // Merchant App Link
)

Client Token Provideranchor

Below is an example of ClientTokenProvider implementation using Retrofit 2.x. This example assumes that you have a server that supports GET https://www.my-api.com/client_token and receives the following json response:

  1. JSON
{
    "value": "<CLIENT_TOKEN>"
}
  1. Kotlin
  2. Java
// In ClientToken.kt file taht you create
class ClientToken {
    val value = null
}

// In Api.kt file that you create
interface Api {

    @GET("/client_token")
    fun getClientToken(): Call<ClientToken>
}

// In ExampleClientTokenProvider.kt file that you create
internal class ExampleClientTokenProvider : ClientTokenProvider {
    override fun getClientToken(callback: ClientTokenCallback) {
        val call: Call<ClientToken> = createService().getClientToken()
        call.enqueue(object : Callback<ClientToken?> {
            override fun onResponse(call: Call<ClientToken?>?, response: Response<ClientToken?>?) {
                response?.body()?.value?.let { callback.onSuccess(it) }
            }

            override fun onFailure(call: Call<ClientToken?>?, t: Throwable?) {
                callback.onFailure(Exception(t))
            }
        })
    }

    companion object {
        private val builder = Retrofit.Builder()
            .baseUrl("https://my-api.com")
            .addConverterFactory(GsonConverterFactory.create())
        private val httpClient = OkHttpClient.Builder()
        fun createService(): Api {
            builder.client(httpClient.build())
            val retrofit = builder.build()
            return retrofit.create(Api::class.java)
        }
    }
}

You can then pass the fecthed Client Token to the payment method Client constructors.

The example below shows a PayPalClient initialization with a Client Token:

  1. Kotlin
val payPalClient = PayPalClient(
    context = this,
    authorization = "[CLIENT_TOKEN]",
    appLinkReturnUrl = Uri.parse("https://merchant-app.com") // Merchant App Link
)

ProGuardanchor

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