Set up Native Checkout SDK for iOS

SDKCurrentLast updated: June 16th 2021, @ 10:53:28 am


Overview

The Native Checkout SDK for iOS 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 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 your bundle identifier of your app, suffixed with ://paypalpay. For example, if your bundle identifier is com.paypal.app, input com.paypal.app://paypalpay.
    • Select the Native Checkout SDK checkbox, found within the Advanced options of the Accept payments option.
    • Select the Log in with PayPal checkbox and the Full name and Email checkboxes found within the Advanced options.

1. Add the SDK to your app

Install the SDK.

Step result

The Native Checkout SDK is now in your app.

2. 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.

3. Add payment buttons

To render payment buttons on your app, add the 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.

4. 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 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. Use SDK with server-side integration.

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.

Next steps

Customize payment buttons

See also