Use SDK with server-side integration


Overview

You can integrate the Native Checkout SDK for Android with your existing server-side PayPal integration. This enables greater level control so you can ensure compatibility with your other integrations. It also enables you to generate your own tokens.

Know before you code

Complete steps one through four in Set up Android SDK.

1. Launch the payment sheet

Similar to the client-side integration, you can launch the payment sheet by passing a CreateOrder callback into the setup function or your PaymentButton. The only difference is that instead of creating an Order, you provide the Order ID through the provided set function.

    payPalButton.setup(
        createOrder = CreateOrder { createOrderActions ->
            val orderId: String // Retrieve your Order ID from your server-side integration
            createOrderActions.set(orderId)
        }
    )
    paymentButton.setup(
        new CreateOrder() {
            @Override
            public void create(@NotNull CreateOrderActions createOrderActions) {
                String orderId; // Retrieve your Order ID from your server-side integration
                createOrderActions.set(orderId);
            }
        }
    );

Ensure the payment sheet launches as soon as the buyer selects the PaymentButton by making your server-side request from within the CreateOrder callback.

Note: The CreateOrder callback invokes from the main thread. Ensure all server-side requests execute from a background thread.

2. Capture or authorize an order

The Android SDK supports supports capturing and authorizing orders created through both client-side or server-side integrations using PayPal-generated tokens. However, if you create an order through a server-side integration, you must capture or authorize the order using your own server-side integration.

Sample code

This sample code uses yourAppsCheckoutRepository as an example. To capture or authorize through a server-side integration, use the following to register the OnApprove callback from the setup function of the PaymentButton:

    payPalButton.setup(
        onApprove = OnApprove { approval ->
            // Send the Order ID to your own endpoint to capture or authorize the order
            yourAppsCheckoutRepository.captureOrder(approval.data.orderId)
        }
    )
     paymentButton.setup(
        new OnApprove() {
            @Override
            public void onApprove(@NotNull Approval approval) {
                // Send the Order ID to your own endpoint to capture or authorize the order
                yourAppsCheckoutRepository.captureOrder(approval.getData().getOrderId());
            }
        }
    );

Next steps