Client-Side Implementation

SetupAnchorIcon

Before you can add PayPal, you will need to:

Get the SDKAnchorIcon

Add the following in your app-level build.gradle:

  1. Kotlin
  2. Groovy
dependencies {
    implementation("com.braintreepayments.api:paypal:5.8.0")
}

InitializationAnchorIcon

Create a PayPalLauncher inside of your Activity's onCreate(). Then, create a PayPalClient with a Tokenization Key or Client Token and an appLinkReturnUrl that is used to return to your app from the PayPal payment flows.

  1. Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // PayPalLauncher must be initialized in onCreate
    payPalLauncher = PayPalLauncher()

    // can initialize the PayPalClient outside of onCreate if desired
    payPalClient = PayPalClient(
        context = this,
        authorization = "[TOKENIZATION_KEY or CLIENT_TOKEN]",
        appLinkReturnUrl = Uri.parse("https://merchant-app.com") // Merchant App Link
    )
}

Use our Payment ButtonsAnchorIcon

Alternatively, you can add PayPal payment buttons in your app through the Braintree SDK. The SDK will handle the loading and disable state of the button, call the tokenize methods with your request, and allow you a seamless branded experience in your mobile apps.

This SDK module handles the complete payment flow internally, from launching the PayPal authentication to tokenizing the result, so you only need to provide your request configuration and handle the final nonce.

Get Buttons ModuleAnchorIcon

Add the UIComponents module to your app's build.gradle:

  1. Groovy
dependencies {
    implementation("com.braintreepayments.api:ui-components:5.25.0") // replace version with latest version
}

Add PayPal button to layout:

  1. XML
<com.braintreepayments.api.uicomponents.PayPalButton                                                                                                                                                                                                                                                                                                                     
      android:id="@+id/payPalButton"                                                                                                                                                                                                                                                                                                                                       
      android:layout_width="wrap_content"                                                                                                                                                                                                                                                                                                                                  
      android:layout_height="wrap_content"                                                                                                                                                                                                                                                                                                                                 
      android:layout_gravity="center"                                                                                                                                                                                                                                                                                                                                      
      app:paymentButtonColor="blue" />

Add PayPal button integration to the Fragment or Activity:

  1. Kotlin
class ExampleFragment : Fragment() {
    private lateinit var payPalButton: PayPalButton
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        payPalButton = view.findViewById<PayPalButton>(R.id.payPalButton)
        payPalButton.initialize(
            activityResultCaller = this,
            authorization = "[TOKENIZATION_KEY or CLIENT_TOKEN]",
            appLinkReturnUrl = Uri.parse("https://merchant-app.com"),
            deepLinkFallbackUrlScheme = "com.merchant.app.payments"
        )
        payPalButton.updatePayPalRequest(PayPalRequest(...))
        paypalButton.setLaunchCallback = { launchResult ->
            when (launchResult) {
                is PayPalPendingRequest.Started-> {
                    // store pending request to disk
                }
                is PayPalPendingRequest.Failure -> {
                    // handle error
                }
            }
        }
    }

    // handle return to your app
    override fun onResume() {
        super.onResume()
        val pendingRequest = fetchPendingRequest()
        if (pendingRequest != null) {
            payPalButton.handleReturnToApp(pendingRequest, intent) { payPalResult ->
                when (payPalResult) {
                    is PayPalResult.Success -> {
                        // handle success
                    }
                    is PayPalResult.Failure -> {
                        // handle failure
                    }
                    is PayPalResult.Cancel -> {
                        // handle canceled
                    }
                }
            }
        }
        // clear pending request
    }
}

PayPal button supports three colors, that you can set programmatically:

  1. Kotlin
payPalButton.setButtonColor(PayPalButtonColor.Blue)   // Default                                                                                                                                                                                                                                                                                                         
payPalButton.setButtonColor(PayPalButtonColor.Black)                                                                                                                                                                                                                                                                                                                     
payPalButton.setButtonColor(PayPalButtonColor.White)

Or via XML:

  1. XML
app:paymentButtonColor="blue"
app:paymentButtonColor="black"                                                                                                                                                                                                                                                                                                                                           
app:paymentButtonColor="white"

Jetpack Compose buttons

In addition to the XML inflated buttons above, we now offer support for Jetpack Compose buttons.

Similar to the XML buttons, the Braintree Android SDK now allows merchants to draw and render both PayPal and Venmo payment buttons using a discrete set of parameters. The SDK will handle the loading and disable state of the button and allow you to display and offer buttons meeting the current brand guidelines versus maintaining responsibility on your own. We will call the tokenize() methods with your request and allow you a seamless branded experience in your mobile apps. The call to handleReturnToApp() when the control is returned to the merchant app is handled within the SDK. You are no longer required to add this logic to your code for Jetpack Compose integration.

For PayPal button, you should invoke the PayPalButton composable like this:

  1. Kotlin
private val paypalTokenizeCallback = PayPalTokenizeCallback { payPalResult ->
    when (payPalResult) {
        is PayPalResult.Success -> {
            handlePayPalResult(payPalResult.nonce)
        }

        is PayPalResult.Cancel -> {
            handleError(Exception("User did not complete PayPal payment flow"))
        }

        is PayPalResult.Failure -> {
            handleError(payPalResult.error)
        }
    }
    // clear intent data
    // requireActivity().intent.data = null
}

PayPalButton(
    style = paypalStyle,
    payPalRequest = paypalRequest,
    authorization = authStringArg,
    appLinkReturnUrl = "https://merchant-app.com".toUri(),
    deepLinkFallbackUrlScheme = "com.merchant.app.payments",
    paypalTokenizeCallback = paypalTokenizeCallback
)

Next: Choose your integrationAnchorIcon

The rest of your configuration will be determined by how you'd like to use PayPal.