Client-side integration

Follow these steps to add the Native Checkout SDK to your iOS app

SDKCurrentLast updated: October 5th 2021, @ 2:54:38 pm


Follow these steps to add PayPal checkout to your Android app:

  1. Enable the SDK
  2. Add the SDK to your app
  3. Configure the SDK
  4. Add payment buttons
  5. Create and capture orders

Know before you code

  • Complete the steps in Get started to set up your PayPal account, client ID, and sandbox emails for testing.
  • Client-side and server-side integrations share many of the same integration steps. If you want to integrate the SDK with your server-side integration, continue with the steps on this page, until directed to the specific steps for a Server-side integration.

Enable the SDK

  1. Select your app from the My Apps & Credentials page on the Developer Dashboard.
  2. 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.
  3. Select the Native Checkout SDK checkbox, found within the Advanced options of the Accept payments option.
  4. Select the Log in with PayPal checkbox and the Full name and Email checkboxes found within the Advanced options.

Step result

The SDK is enabled.

Add the SDK to your app

Add the SDK to your app using your preferred installation method. View all available installation methods at Install the SDK.

Step result

The SDK is now in your app.

Configure the SDK

Create an instance of CheckoutConfig, and pass it to the top level Checkout type of the SDK.

Sample code

func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: AppDelegateLaunchOpts?
) -> Bool {

    let config = CheckoutConfig(
        clientID: "YOUR_CLIENT_ID",
        returnUrl: "YOUR_RETURN_URL",
        environment: .sandbox
    )

    Checkout.set(config: config)

    return true
}

Step result

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

Add payment buttons

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

override func viewDidLoad() {
    super.viewDidLoad()

    let paymentButton = PayPalButton()
    view.addSubview(paymentButton)

    NSLayoutConstraint.activate(
        [
            paymentButton.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            paymentButton.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ]
    )

}

Step result

Payment buttons display on your app.

Create and capture orders

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 backend 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 backend infrastructure.

Sample Native Checkout SDK code for iOS

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.

import PayPalCheckout

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let paymentButton = PayPalButton()
        view.addSubview(paymentButton)

        NSLayoutConstraint.activate(
            [
                paymentButton.centerYAnchor.constraint(equalTo: view.centerYAnchor),
                paymentButton.centerXAnchor.constraint(equalTo: view.centerXAnchor)
            ]
        )
        configurePayPalCheckout()
    }

    func configurePayPalCheckout() {
        Checkout.setCreateOrderCallback { createOrderAction in
            let amount = PurchaseUnit.Amount(currencyCode: .usd, value: "10.00")
            let purchaseUnit = PurchaseUnit(amount: amount)
            let order = OrderRequest(intent: .capture, purchaseUnits: [purchaseUnit])

            createOrderAction.create(order: order)
        }

        Checkout.setOnApproveCallback { approval in
             approval.actions.capture { (response, error) in
                print("Order successfully captured: \(response?.data)")
            }
        }

    }
}

Add and modify the code

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

    Checkout.setOnCancelCallback {
        // User has cancelled the payment experience
    }
    
  2. (Optional) Add the OnError callback to be notified if the SDK encounters an error, resulting in the dismissal of the payment sheet.

    Checkout.setOnErrorCallback { error in
        // Handle the error generated by the SDK
    }
    
  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.
  4. If you want to use something other than the PayPal payment buttons, you can Programmatically start the SDK.

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.

Test and go live

Use your client ID when adding the Native Checkout SDK to your app. Use your sandbox accounts when testing the SDK.

Next steps

See also