Set up Native Checkout SDK for Android


Overview

The Native Checkout SDK for Android enables you to provide a secure and safe payment solution for your mobile app. You can add a Pay Now checkout experience that captures funds as soon as the buyer approves the cart total and customization options. The SDK supports client-side and server-side integrations.

Know before you code

  • Complete the steps in Get started to get the following sandbox account information from the Developer Dashboard:

    • Your client ID
    • Your personal and business sandbox accounts
  • Use your client ID when adding the Native Checkout SDK to your to your app. Use your sandbox accounts when testing the SDK.
  • Select your app from the My Apps & Credentials page on the Developer Dashboard and:

    • Enter a Return URL. You can use an Android App Link registered within the Developer Console to handle SDK redirects. Alternatively, you can use your Application ID (typically referenced via BuildConfig.APPLICATION_ID) and register your Application ID with ://paypalpay as the suffix. For example, if your Application ID is com.paypal.app, input com.paypal.app://paypalpay in the Developer Console.
    • Select the Native Checkout SDK checkbox, found within the Advanced options of the Accept payments option.
    • Select the Connect with PayPal (formerly Log In with PayPal) checkbox and the Full name and Email checkboxes found within the Advanced options.
  • You must have a minSdkVersion of API 21 (Android 5.0 Lollipop) or later.

1. Prepare your app

Define the android.permission.INTERNET permission in the AndroidManifest.xml file of your application as follows:

  <manifest xmlns:android="http://schemas.android.com/apk/res/android">

      <uses-permission android:name="android.permission.INTERNET" />
      ...

  </manifest>

Step result

Your app is ready to install the Native Checkout SDK.

2. Add the SDK to your app

The Native Checkout SDK is available through Maven Central. Add the SDK to your app through build.gradle updates.

  1. Add the required repositories to the build.gradle file of your project root.

    allprojects {
        repositories {
            mavenCentral()
        }
    }
    
  2. Add Java 8 compatibility to the build.gradle file of your app module.

    android {
        ...
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
    
  3. Add the Native Checkout SDK dependency to the build.gradle file of your app module.

    dependencies {
        implementation('com.paypal.checkout:android-sdk:0.1.0')
    }
    

Step result

The Native Checkout SDK is now in your app.

3. Configure the SDK

Configure the Native Checkout SDK in the onCreate function of your app.

Sample code

This sample code includes these optional properties:

  • currencyCode
  • userAction
  • settingsConfig

Providing currencyCode and userAction can help with funding eligibility for payment buttons. The settingsConfig provides additional properties that are useful for development builds. We also set loggingEnabled to be true to enable logging from the SDK.

class YourApp : Application() {
    override fun onCreate() {
        super.onCreate()
        val config = CheckoutConfig(
            application = this,
            clientId = YOUR_CLIENT_ID,
            environment = Environment.SANDBOX,
            returnUrl = "${BuildConfig.APPLICATION_ID}://paypalpay",
            currencyCode = CurrencyCode.USD,
            userAction = UserAction.PAY_NOW,
            settingsConfig = SettingsConfig(
                loggingEnabled = true
            )
        )
        PayPalCheckout.setConfig(config)
    }
}
public class YourApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        CheckoutConfig checkoutConfig = new CheckoutConfig(
            this,
            YOUR_CLIENT_ID,
            Environment.SANDBOX,
            String.format("%s://paypalpay", BuildConfig.APPLICATION_ID),
            CurrencyCode.USD,
            UserAction.PAY_NOW,
            new SettingsConfig(
                true,
                false
            )
        );
    }
}

Step result

The SDK is is configured and your app is ready to add payment buttons.

4. Add payment buttons

To render the payment buttons on your app, add the following code to your checkout page:

<com.paypal.checkout.paymentbutton.PayPalButton
    android:id="@+id/payPalButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Step result

Payment buttons display on your app.

5. Create and capture an order

You have two options to complete your integration:

Integration type Use case
Client-side integration If want the simplest integration, continue with the sample code below for a client-side integration. Client-side integrations don't require you to create your own server infrastructure.
Server-side integration Chose a server-side integration if you want more control over your integration. Server-side integrations require you to have your own server infrastructure. Use SDK with server-side integration.

Sample Native Checkout SDK code for Android

This sample code creates an order of a single item for $10.00 USD. When the buyer selects Pay Now on a payment sheet, the OnApprove callback invokes and the funds are ready for immediate capture.

class CheckoutActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // ...
        payPalButton.setup(
            createOrder = CreateOrder { createOrderActions ->
                val order = Order(
                    intent = OrderIntent.CAPTURE,
                    appContext = AppContext(
                        userAction = UserAction.PAY_NOW
                    ),
                    purchaseUnitList = listOf(
                        PurchaseUnit(
                            amount = Amount(
                                currencyCode = CurrencyCode.USD,
                                value = "10.00"
                            )
                        )
                    )
                )

                createOrderActions.create(order)
            },
            onApprove = OnApprove { approval ->
                approval.orderActions.capture { captureOrderResult ->
                    Log.i("CaptureOrder", "CaptureOrderResult: $captureOrderResult")
                }
            }
        )
    }
}
public class CheckoutActivity extends AppCompatActivity {
    PaymentButton payPalButton;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ...
        payPalButton.setup(
            new CreateOrder() {
                @Override
                public void create(@NotNull CreateOrderActions createOrderActions) {
                    ArrayList purchaseUnits = new ArrayList<>();
                    purchaseUnits.add(
                        new PurchaseUnit.Builder()
                            .amount(
                                new Amount.Builder()
                                    .currencyCode(CurrencyCode.USD)
                                    .value("10.00")
                                    .build()
                            )
                            .build()
                    );
                    Order order = new Order(
                        OrderIntent.CAPTURE,
                        new AppContext.Builder()
                            .userAction(UserAction.PAY_NOW)
                            .build(),
                        purchaseUnits
                    );
                    createOrderActions.create(order, (CreateOrderActions.OnOrderCreated) null);
                }
            },
            new OnApprove() {
                @Override
                public void onApprove(@NotNull Approval approval) {
                    approval.getOrderActions().capture(new OnCaptureComplete() {
                        @Override
                        public void onCaptureComplete(@NotNull CaptureOrderResult result) {
                            Log.i("CaptureOrder", String.format("CaptureOrderResult: %s", result));
                        }
                    });
                }
            }
        );
    }
}

Add and modify the code

  1. (Optional) Add the OnCancel callback to be notified if the buyer cancels the order.

        paymentButton.setup(
            onCancel = OnCancel {
                Log.d("OnCancel", "Buyer canceled the PayPal experience.")
            }
        )
    
        paymentButton.setup(
            // ...
            new OnCancel() {
                @Override
                public void onCancel() {
                    Log.d("OnCancel", "Buyer cancelled the PayPal experience.");
                }
            }
        )
    
  2. (Optional) Add the OnError callback to be notified if the SDK encounters an error, resulting in the dismissal of the payment sheet.

        paymentButton.setup(
            onError = OnError { errorInfo ->
                Log.d("OnError", "Error: $errorInfo")
            }
        )
    
        paymentButton.setup(
            // ...
            new OnError() {
                @Override
                public void onError(@NotNull ErrorInfo errorInfo) {
                    Log.d("OnError", String.format("Error: %s", errorInfo))
                }
            }
        )
    
  3. (Optional) Configure the following messages to display to the buyer:

    • Success message when funds capture successfully.
    • Cancellation confirmation when the buyer selects to cancel the order.
    • Error message when the capture is unsuccessful.

Note: For more information about creating orders, including additional parameters that can be submitted, view Orders REST API.

Step result

You can now test purchases.

Next steps

Customize payment buttons

See also