Use SDK with server-side integration

SDKCurrentLast updated: June 7th 2021, @ 11:35:12 am


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.

Server-side integrations offer greater flexibility than client-side integrations. Before integrating, it's important to determine which server-side integration you want to use. The Native Checkout SDK supports the following server-side integrations:

Note: If you're integrating for the first time, we recommend using the REST v2 server-side integration.

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 generate the order ID from your own server-side integration and then 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 ->
            // Optional -- retrieve order details first
            yourAppsCheckoutRepository.getEC(approval.data.orderId)

            // 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) {
                // Optional -- retrieve order details first
                yourAppsCheckoutRepository.getEC(approval.getData().getOrderId());

                // Send the order ID to your own endpoint to capture or authorize the order
                yourAppsCheckoutRepository.captureOrder(approval.getData().getOrderId());
            }
        }
    );

Next steps