Android implementation guide
This guide walks you through integrating the Android 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.
The SDK also includes Checkout V1, which is deprecated and supports standard checkout only. Use Checkout V2 instead.
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
Add the SDK to your module’s build.gradle.kts or build.gradle file. The SDK uses Java 8+ APIs, so also enable core library desugaring:
Kotlin DSL
Groovy
R8 applies the SDK’s shrinking and obfuscation rules automatically. If you use ProGuard, apply the rules in consumer-rules.pro manually.
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:
- Call
setConfigurationon the main thread, before you start a checkout or initialize any UI component. - Use
AfterpayEnvironment.SANDBOXfor development andAfterpayEnvironment.PRODUCTIONfor release builds. The environment must match the environment your server uses to create checkouts. - Refresh the configuration about once a day, because your merchant configuration can change.
- A UK locale displays Clearpay branding, terms, and currency formatting.
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: Set a checkout handler
The SDK calls your AfterpayCheckoutV2Handler to get the checkout token and, for express checkout, the shipping options. Register it with Afterpay.setCheckoutV2Handler before you start checkout. The SDK holds the handler in a global reference until you replace it or pass null.
If you pass a failure to onTokenLoaded, the SDK shows a dialog that lets the customer retry or close checkout. Call onTokenLoaded on the main thread, because the SDK shows the error dialog on the calling thread.
Step 5: Start the checkout
Start checkout when the customer taps your payment button.
Launch the intent from createCheckoutV2Intent with an activity result launcher:
If you haven’t set the SDK configuration, or the customer’s language isn’t supported in your region, checkout closes with the cancellation status LANGUAGE_NOT_SUPPORTED. If you haven’t set the handler, it closes with NO_CHECKOUT_HANDLER. If the customer closes checkout, the status is USER_INITIATED.
Checkout options
Pass AfterpayCheckoutV2Options to change the customer’s experience in checkout:
Step 6: Provide shipping options (express checkout)
For express checkout, the SDK calls your handler when the customer chooses a shipping address and when they choose a shipping option. Return the shipping options for the address, or an error:
- The SDK calls both methods on a background thread. Call your server from them, and pass the result to the callback from any thread.
orderAmountis the order total including shipping and tax.- To reject an address, pass
ShippingOptionsErrorResultwithSHIPPING_ADDRESS_UNRECOGNIZED,SHIPPING_ADDRESS_UNSUPPORTED,SERVICE_UNAVAILABLE, orBAD_RESPONSE. - To update the selected option, pass
ShippingOptionUpdateSuccessResultwith aShippingOptionUpdate. To report an error, passShippingOptionUpdateErrorResultwithSERVICE_UNAVAILABLEorBAD_RESPONSE.
Step 7: Complete the order
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 reduce the risk of a man-in-the-middle attack during checkout, pin the certificate for the Afterpay portal with an Android network security configuration. Add a domain configuration to res/xml/network_security_config.xml, and reference the file from your manifest with <application android:networkSecurityConfig="@xml/network_security_config">:
Keep the pins and the expiration date up to date. When the certificate rotates, outdated pins block checkout until the expiration date passes.
Test with the sample app
The sample app uses the example server, which creates sandbox checkouts with your merchant credentials. It includes screens for Checkout V2, the Afterpay Button, Cash App Pay with the Afterpay Button, and the UI components.
-
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 add your merchant ID to
local.propertiesin the project root. The sample uses it as theshopDirectoryMerchantIdfor the Afterpay Button. -
Run the
samplemodule on an Android emulator. The sample connects to the example server athttp://10.0.2.2:3000, which is the emulator’s address for your computer. To use a different address, changeMERCHANT_API_BASE_URLinsample/src/main/java/com/example/api/MerchantApi.kt. -
Start a checkout and log in with a sandbox customer account. For details, see Test environments.
The sample builds the SDK from source. In debug builds, the SDK replaces the checkout page and the widget scripts with local mocks. The mock checkout immediately returns a test order token, so the checkout closes right after it opens. To test against the sandbox, build the sample’s release variant with a signing configuration, or depend on the published com.afterpay:afterpay-android artifact instead of the local module.