iOS implementation guide
This guide walks you through integrating the iOS SDK: install and configure it, create checkouts on your server, present checkout in your app, and complete the order.
When the customer taps your payment button, the SDK shows a loading screen and asks your app for a checkout token. Your app gets the token from your server and passes it to the SDK, which opens Afterpay checkout in a web view. The customer stays in your app, and the SDK receives the result directly from checkout, so your app doesn’t handle redirect URLs. You can offer standard checkout or express checkout, where the customer chooses a shipping address and shipping option in checkout.
This guide uses the SDK’s Checkout V2 API, which is available in Swift with UIKit.
The SDK also includes Checkout V1, which is deprecated and supports standard checkout only. Use Checkout V2 unless your app needs the SwiftUI or Objective-C API that only Checkout V1 provides.
To accept Cash App Pay, see Cash App Pay. For a front-end-only integration that returns a one-time virtual card, see the Afterpay Button, available in the US and Canada.
How it works
Your server creates the checkout with your merchant credentials, the SDK presents checkout to the customer, and your server completes the payment. Your merchant credentials never leave your server.
Cash App Pay uses the same server flow, but Pay Kit sends the customer to Cash App instead of the SDK presenting checkout. For the steps, see Cash App Pay.
The Afterpay Button works differently: the SDK creates the checkout itself and returns a virtual card, which you process like any other card payment.
Step 1: Install the SDK
Swift Package Manager
CocoaPods
Carthage
Swift Package Manager is the recommended installation method. In Xcode, choose File > Add Package Dependencies, enter https://github.com/afterpay/sdk-ios.git, and add the Afterpay library to your app target. In a Swift package, add the dependency to Package.swift:
To integrate the SDK manually, see Integration in the SDK documentation.
Step 2: Configure the SDK
The SDK uses your merchant configuration to show UI components with the right branding, terms, and currency format. Checkout and Cash App Pay also require it. The Afterpay Button uses its own configuration, described in Afterpay Button.
Your server fetches the configuration from the Get Configuration endpoint and passes it to your app:
- Set the configuration before you present a checkout or display any UI component.
- Use
.sandboxfor development and.productionfor release builds. The environment must match the environment your server uses to create checkouts. - Cache the configuration and refresh it about once a day. Bundle a default configuration in case the first request fails.
- If you haven’t set a configuration, or the customer’s language (the device language, or
consumerLocale) isn’t supported in the configured region,Afterpay.enabledisfalse. The lockup, price breakdown, and widget then hide themselves, butPaymentButtonstays visible with blank artwork. Check this property to hide the payment button and your own Afterpay views.
Step 3: Create a checkout on your server
When the SDK asks for a token, your app calls your server. Your server calls Create Checkout and returns the token from the response:
Set merchant.popupOriginUrl to https://static.afterpay.com. The SDK opens checkout from this origin and receives the result from it, so checkout doesn’t work correctly without it.
For express checkout, also set mode to express. For the other express fields, see Express checkout.
Step 4: Present the checkout
Start checkout when the customer taps your payment button.
Call presentCheckoutV2Modally from the view controller that shows your cart. Set the SDK configuration first. Otherwise, debug builds trigger an assertion failure, and release builds return without presenting checkout or calling the completion handler.
If you pass an error to the didCommenceCheckout completion, the SDK shows a dialog that lets the customer retry or close checkout. If the customer’s language isn’t supported in your region, Afterpay.enabled is false and the SDK doesn’t present checkout.
Checkout options
Pass CheckoutV2Options to change the customer’s experience in checkout:
Step 5: Provide shipping options (express checkout)
For express checkout, the SDK calls your app when the customer chooses a shipping address and when they choose a shipping option. Return the shipping options for the address, or a ShippingOptionsError:
orderAmount is the order total including shipping and tax. The ShippingOptionsError cases are shippingAddressUnrecognized, shippingAddressUnsupported, serviceUnavailable, and badResponse. Checkout shows an error message to the customer for each one.
Use a checkout handler
To handle these callbacks outside the presenting view controller, implement CheckoutV2Handler and register it with Afterpay.setCheckoutV2Handler(_:). The protocol has the same three methods: didCommenceCheckout(completion:), shippingAddressDidChange(address:completion:), and shippingOptionDidChange(shippingOption:completion:). The SDK holds the handler weakly, so keep a strong reference to it.
Step 6: Complete the order
A successful result contains the order token. A canceled result contains a CancellationReason, such as userInitiated.
Send the order token to your server. To capture the full amount immediately, your server calls Capture Full Payment with the token. To capture later, call Auth and then Capture Payment.
Security
To add certificate pinning to the checkout and widget web views, register an authentication challenge handler. Return true if you handled the challenge, or false to use the default handling. This example uses TrustKit:
Test with the example app
The example app uses the example server, which creates sandbox checkouts with your merchant credentials.
-
Start the example server. It requires Node.js and listens on
http://localhost:3000. In.env, setAFTERPAY_MERCHANT_IDandAFTERPAY_SECRET_KEYto your sandbox credentials, and setAFTERPAY_REGION, for exampleen-US. -
Clone the SDK and open
Afterpay.xcworkspacein Xcode. Open the workspace rather than the example project so that Xcode resolves the SDK’s dependencies. -
Select the Example scheme and run it on a simulator.
-
To use a different server host or port, change the values on the app’s Settings tab.
-
Add items to the cart and check out. Log in with a sandbox customer account. For details, see Test environments.