iOS SDK
The Afterpay iOS SDK presents Afterpay checkout in your iOS app and provides prebuilt UI components, such as the payment button and price breakdown. It also lets you accept Cash App Pay through your Afterpay merchant account.
This guide covers installation, configuration, the available checkout integrations, and how to test with the example app. For the complete SDK reference, see the iOS SDK documentation.
Requirements
- iOS 12.0 or later
- Swift 5.7 or later
- Xcode 14.0 or later
Afterpay checkout runs in a WKWebView that requires iOS Safari 13.4 or later. The SDK runs on iOS 12, but your app must hide Afterpay from customers on earlier versions. For example, wrap your Afterpay views in an if #available(iOS 13.4, *) check.
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.
Configure the SDK
The SDK uses your merchant configuration to render UI components with the correct branding, terms, and currency formatting. Web Redirect, Built-in UI, and Cash App Pay 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, butPaymentButtononly blanks its artwork. Use this property to hide the payment button and your own Afterpay views.
Present checkout
After you configure the SDK, present checkout in one of four ways:
- Web Redirect: your server creates a checkout, and the SDK loads its redirect URL. The SDK calls this Checkout V1.
- Built-in UI: the SDK opens Afterpay’s checkout right away and asks your app for a token. Supports express checkout. The SDK calls this Checkout V2.
- Afterpay Button: the SDK creates the checkout from your app and returns a one-time virtual card. No server integration. The SDK calls this Checkout V3.
- Cash App Pay: the customer approves the payment in Cash App.
To compare the options, see Present checkout.
UI components
The SDK includes prebuilt components that follow Afterpay brand guidelines. Outside the US, they show Afterpay branding, or Clearpay branding for UK merchants. US merchants see Cash App Afterpay branding.
- Payment button:
PaymentButtonstarts an Afterpay checkout. - Lockup:
LockupViewshows the Afterpay logo. - Price breakdown:
PriceBreakdownViewshows the installment amounts for a price. - Checkout widget:
WidgetViewshows the payment schedule and updates when you send it a new total.
For branding, color schemes, and accessibility, see iOS UI components.
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.