## Overview

Starting a payment with Affirm on an Android client consists of the following: creating a checkout form, tokenizing customer information to transmit it securely to your server, and using that `checkout_token` to initiate a transaction.

This guide walks you through setting up the Android client on your checkout pages.

The [Android SDK](🔗) is available on Github.

## 1. Set up Affirm on Android

### _With Gradle_

To install the SDK, add `affirm-merchant-sdk-android` to the dependencies block of your [app/build.gradle](🔗) file:

### _With Maven_

Add the [Affirm dependency](🔗) to your [pom.xml](🔗) file:

For details on the latest SDK release and past versions, see the [Releases page](🔗) on GitHub. To receive notifications when a new release is published, watch [releases for the repository](🔗).

## 2. Initialize Affirm

Configure the SDK with your Affirm **public API key** to send requests to the Affirm server.

## 3. Render Affirm Checkout

Checkout creation is the process in which a customer uses Affirm to pay for a purchase in your app. You can create a `checkout` object and launch the Affirm checkout using the `Checkout` function.

The checkout object contains details about the order. Amounts should be in dollar amounts represented as Floats.

## 4. Add Affirm promotional messaging

Affirm promotional messaging components---**monthly payment messaging and educational modals**---show customers how they can use Affirm to finance their purchases. Properly placed promotional messaging helps drive increased AOV and conversion.

### Add monthly payment messaging

To display promotional messaging, our SDK provides a `AffirmPromotionButton` class. To implement the `AffirmPromotionButton`:

Use one of the following implementation methods: 

To style the promotional messaging button we have two options:

**Option 1:** Use `configWithLocalStyling` to use the local styles on the string within the button

**Option 2:** `configWithHtmlStyling` will use html style from Affirm server. You can add fonts by following the steps below, so you can customize the fonts in html.

1\. Add a font file in the `/res/font/ `directory. Such as [lacquer_regular.ttf](🔗). 2\. Add a declaration for the font file. You can check the detail in [typeface](🔗). 3\. Use the font in the css file. You can check the detail in [remote_promo.css](🔗).

Tapping on the `AffirmPromotionButton` automatically starts the prequalification flow. If you want to handle errors, override `onActivityResult` so that Affirm can handle the result.

### Fetch promotional message, then display it with your own `TextView`.

  • You can get the promotional message via `fetchPromotion`, a `SpannableString` object is returned after the request is successful.

  • `onPromotionClick`:  This method handles events that click on the promotional message

Calling the `create` method will initiate the request, and call `cancel` method to cancel the request.

### Track Order Confirmed

The `trackOrderConfirmed` event triggers when a customer completes their purchase. Our SDK provides the `AffirmTrack` object to trigger the tracking.

  • Since there is no callback, it will return success after a 10 second timeout.

  • We will replace it using the HTTP API after the API is done.

## 5. Handle callbacks

Be sure to override `onActivityResult`, then call the `handleCheckoutData` method. It will return the `checkout_token` required to initiate the transaction on your server.

We also have some additional handler functions available:

Once the checkout has been successfully confirmed by the user, the `AffirmCheckoutDelegate` object will receive a `checkout_token`. This token should be forwarded to your server, which should be used to [authorize a charge](🔗) on the user's account.

## 6. Create a charge

When a customer successfully completes a checkout, it is recorded as a new purchase attempt. This needs to be handled on your server-side to be fulfilled via our [Charges API](🔗).