Android SDK
The Afterpay Android SDK presents Afterpay checkout in your Android 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 sample app. For the complete SDK reference, see the Android SDK documentation.
Requirements
- Android 7.0 (API level 24) or later
Afterpay checkout runs in a WebView that requires specific browser features. The SDK runs on API level 24, but checkout depends on the WebView version installed on the device, which is independent of the Android version. If your app knows the WebView version, show Afterpay only on supported versions.
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.
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:
- 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.
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. In the US, they show Cash App Afterpay branding.
- Payment button:
AfterpayPaymentButtonstarts an Afterpay checkout. - Logo: Shows the Afterpay logo.
- Price breakdown:
AfterpayPriceBreakdownshows the installment amounts for a price. - Checkout widget:
AfterpayWidgetViewshows the payment schedule and keeps it in sync with the order total.
For branding, color schemes, and accessibility, see Android UI components.
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 separate screens for Built-in UI, 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 Built-in UI 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.