Mobile SDK quickstart
This quickstart walks you through adding Afterpay to an iOS or Android app, from your first server request to your first captured sandbox payment. You set up two server endpoints, add the Afterpay SDK to your app, present checkout, and capture the payment.
The quickstart uses Checkout V2, the recommended way to present checkout when your server creates the checkout. For the other ways to present checkout, see Step 5.
Before you begin
Make sure that you have the following:
- An Afterpay sandbox merchant ID and secret key. Your Afterpay account manager provides them. For details, see Test environments.
- A backend server that you control. Your server calls the Afterpay API with your secret key. Never put your secret key in your app.
- For iOS: Xcode 14.0 or later and an app that targets iOS 12.0 or later.
- For Android: Android Studio and an app with a minimum SDK of API level 24 (Android 7.0) or later.
Afterpay checkout runs in a web view. On iOS, it requires iOS 13.4 or later. On Android, it depends on the WebView version installed on the device. For details, see the iOS SDK and Android SDK overviews.
How it works
Your server holds your Afterpay credentials and makes every Afterpay API call. Your app gets the merchant configuration and a checkout token from your server, and the SDK presents checkout to the customer.
Step 1: Fetch the merchant configuration
The SDK needs your merchant configuration to show checkout and its UI components with the correct currency and order limits. Your server fetches the configuration from the Get Configuration endpoint.
The response contains your order limits. The response omits minimumAmount if your account has no minimum order amount.
Expose an endpoint on your server that returns this response to your app, for example GET /afterpay/configuration. Your configuration doesn’t change often, so call Get Configuration on a schedule, about once a day, and serve the stored result.
Step 2: Create a checkout
When the customer starts checkout, your app asks your server for a checkout. Your server calls the Create Checkout endpoint and returns the checkout token.
Set merchant.popupOriginUrl to https://static.afterpay.com. The SDK opens checkout from that origin and receives the result as a message from checkout, so checkout doesn’t behave as expected without it. Create Checkout still requires redirectConfirmUrl and redirectCancelUrl, but checkout doesn’t redirect to them.
- Set
amountfrom the order on your server. Afterpay rejects the checkout if the amount is outside your configured limits or if the currency differs from your account currency. - Set
merchantReferenceto your order ID so that you can match the payment to the order.
The response contains the checkout token, its expiry time, and a checkout URL that Checkout V2 doesn’t use:
Expose an endpoint on your server that creates the checkout and returns the token to your app, for example POST /afterpay/checkout.
Step 3: Install the SDK
Swift (iOS)
Kotlin (Android)
Add the SDK with Swift Package Manager. 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 install with CocoaPods or Carthage, see the iOS SDK overview.
Step 4: Configure the SDK
Request the configuration from your server and pass it to the SDK. Set the configuration before you present checkout. Do this when your app starts, and again when you refresh the configuration.
Swift (iOS)
Kotlin (Android)
The Configuration initializer throws a ConfigurationError if an amount, the currency code, or the locale isn’t valid.
- Set the locale to match your merchant account’s region, for example
en_USfor an account in the United States. The SDK uses it for terms and currency formatting. - The SDK supports only the customer languages available in your region. For example, the
en_USlocale supports English. The SDK uses the device language unless you pass the optionalconsumerLocaleparameter. - Use the sandbox environment for development. The environment must match the Afterpay API host that your server uses.
- Store the configuration and pass it to the SDK each time your app starts, before the customer can start a checkout.
Step 5: Present checkout
The SDK can present checkout in three ways:
The SDK also supports Checkout V1, which is deprecated. For details, see Checkout V1 for iOS or Android.
When the customer taps your payment button, start checkout right away. The SDK shows a loading screen and asks your app for a checkout token. Call your server’s checkout endpoint from Step 2 and pass the token to the SDK. The SDK opens Afterpay checkout in the app and returns a result when the customer finishes. A successful result contains the order token, which your server uses to complete the payment.
Swift (iOS)
Kotlin (Android)
Call presentCheckoutV2Modally from the view controller that shows your cart:
- If you pass an error to the
didCommenceCheckoutcompletion, the SDK shows a dialog that lets the customer retry or close checkout. - If you haven’t set the configuration, debug builds trigger an assertion failure, and release builds return without presenting checkout or calling the completion handler.
- If the customer’s language isn’t supported in your region,
Afterpay.enabledisfalse, and the SDK doesn’t present checkout.
Checkout V2 has a Swift API for UIKit. For express checkout and checkout options, see iOS implementation guide.
Step 6: Capture the payment
Your app sends the order token to your server. To capture the full order amount immediately, your server calls the Capture Full Payment endpoint with the token. Look up the order by the checkout token that your server stored in Step 2, rather than trusting order details sent by the app.
The response is a payment object. Fulfill the order only if its status is APPROVED. If the status is DECLINED, don’t fulfill the order. Then return the result to your app.
To capture later instead, for example when you ship the order, call Auth with the token. Then call Capture Payment with the id from the Auth response. An authorization expires after 13 days.
Step 7: Test in the sandbox
- Run your server with your sandbox credentials and the sandbox host
https://global-api-sandbox.afterpay.com. - Run your app with the sandbox environment and start a checkout.
- In checkout, create a sandbox customer account with a unique email address and phone number. The sandbox doesn’t send SMS messages, so enter
111111as the verification code. - Pay with a test card, such as Visa
4111 1111 1111 1111with a future expiry date. Use CVV000for an approved payment and051for a declined payment. - Confirm that your server captures the payment and that your app shows the result.
For more test cards and sandbox accounts, see Test environments.
To see a complete working integration, run the iOS example app or the Android sample app with the Afterpay example server.
Go live
Before you release your app, complete this checklist:
- Complete certification. Afterpay issues your production credentials after you pass.
- On your server, use your production merchant ID and secret key, and the production API host
https://global-api.afterpay.com. - Update your
User-Agentheader with your production merchant ID. - In your app, set the environment to production:
.productionon iOS orAfterpayEnvironment.PRODUCTIONon Android. - Make sure that your app and your server use the same environment. The SDK environment must match the Afterpay API host that your server uses to create checkouts.