Skip to main content

 

Affirm Merchant Help

Android SDK

Upgrade (from 1.x.x to 2.0.1)

If you are using an older version of the SDK, you can refer to this upgrade document. We recommend that you install the latest version to access the most up-to-date features and experience.

  • Refactored code
  • HTTP Networking
  • Debug logging
  • Event tracker
  • SDK configuration and init
  • Prequalify Flow
  • Checkout Flow (direct + vcn)
  • Exceptional Flows
  • Activity indicator
  • Track order confirmed
  • Support for Kotlin
  • Page Types
  • Session Cookie

Overview

You can use the Affirm Android SDK to add the Affirm payment option to your Android application. The SDK allows you to:

  • Offer Affirm as a payment option at checkout
  • Display Affirm promotional messaging

The process to add Affirm to your Android app is:

  1. Install the Android SDK
  2. Add Affirm promotional messaging
  3. Configure Affirm as a payment method
  4. Authorize a test charge
  5. Test your integration
  6. Deploy to production

The Affirm SDK supports Android API Level 15 and above. Please contact us using this form or through your Affirm contacts if you have any questions or problems implementing the Android SDK.

Sandbox development

You should have received an email inviting you to create an Affirm account. Click here for information about accessing your account.

Develop and test the Affirm integration in your development environment connected to our sandbox. To use our sandbox, retrieve your sandbox API keys at https://sandbox.affirm.com/dashboard/#/apikeys for use during integration.

After development and testing, you'll need to update your integration to use your live API keys, which you can find at https://affirm.com/dashboard/#/apikeys.

1. Install the Android SDK

Installation

  1. Download via Gradle:

    implementation "com.affirm:affirm-android-sdk:2.0.1"

    or Maven:

    <dependency>
      <groupId>com.affirm</groupId>
      <artifactId>affirm-android-sdk</artifactId>
      <version>2.0.1</version>
    </dependency>

    Snapshots of the development version are available in Sonatype's snapshots repository.

    Usage Overview

    Before you can start the initialization of Affirm SDK, you must first set the AffirmSDK with your public API key from your sandbox Merchant Dashboard. You must set this key as follows:

    Affirm.initialize(new Affirm.Configuration.Builder("public key", Affirm.Environment.SANDBOX)
            .setName("merchant name")
            .setLogLevel(Affirm.LOG_LEVEL_DEBUG)
            .build()

2. Configure Affirm as a payment method

  1. 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.
    • Note: Amounts should be dollar amounts represented as Floats.
final Checkout checkout = Checkout.builder()
        .setItems(items)
        .setBilling(shipping)
        .setShipping(shipping)
        .setShippingAmount(0f)
        .setTaxAmount(100f)
        .setTotal(1100f) //the grand total (subtotal + tax + shipping)
        .build();

Affirm.startCheckout(this, checkout, false);
  1. Checkout object contains details about the order

  2. UseVCN (boolean) determines whether the checkout flow should use virtual card network to handle the checkout

    • If true, it will return card info from VcnCheckoutCallbacks. Be sure to override onActivityResult, then call the handleVcnCheckoutData method. Please check out the example project for more information.
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (Affirm.handleVcnCheckoutData(this, requestCode, resultCode, data)) {
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    public void onAffirmVcnCheckoutCancelled() {
        Toast.makeText(this, "Vcn Checkout Cancelled", Toast.LENGTH_LONG).show();
    }
    
    @Override
    public void onAffirmVcnCheckoutError(@Nullable String message) {
        Toast.makeText(this, "Vcn Checkout Error: " + message, Toast.LENGTH_LONG).show();
    }
    
    @Override
    public void onAffirmVcnCheckoutSuccess(@NonNull CardDetails cardDetails) {
        Toast.makeText(this, "Vcn Checkout Card: " + cardDetails.toString(), Toast.LENGTH_LONG).show();
    }
    • if false, it will return checkout token from CheckoutCallbacks. Be sure to override onActivityResult, then call the handleCheckoutData method. Please refer to the example project for more information.
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (Affirm.handleCheckoutData(this, requestCode, resultCode, data)) {
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    public void onAffirmCheckoutSuccess(@NonNull String token) {
        Toast.makeText(this, "Checkout token: " + token, Toast.LENGTH_LONG).show();
    }
    
    @Override
    public void onAffirmCheckoutCancelled() {
        Toast.makeText(this, "Checkout Cancelled", Toast.LENGTH_LONG).show();
    }
    
    @Override
    public void onAffirmCheckoutError(String message) {
        Toast.makeText(this, "Checkout Error: " + message, Toast.LENGTH_LONG).show();
    }

3. Authorize a test charge

Follow these steps to get the checkout token during development:

  1. Initiate checkout to access the account creation screen
  2. If you have an existing account, click Sign In
  3. If you don’t have an existing account, create one with the following:
  • Any first and last name
  • An email address with a valid format
  • A valid US cell phone number (you do not need access to this number) that you will use in all subsequent checkout attempts
  • A birth date older than 18 years old
  • Any four digits
  1. Enter 1234 for the verification code
  2. Click Confirm

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 then use the token to authorize a charge on the user's account. For more details about the server integration, see our API documentation.

Note - For VCN Checkout, all actions should be done using your existing payment gateway and debit card processor using the virtual card number returned after a successful checkout.

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, SDK provides a AffirmPromotionButton class. AffirmPromotionButton is implemented as follows:

<com.affirm.android.AffirmPromotionButton
     android:id="@+id/promo"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@id/price"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="20dp"
     android:textSize="16sp"
     app:affirmColor="AffirmColorTypeBlue"
     app:affirmLogoType="AffirmDisplayTypeLogo"/>

Use one of the following implementation methods: 

// Option1 - Load via findViewById
AffirmPromotionButton affirmPromotionButton1 = findViewById(R.id.promo);
Affirm.configureWithAmount(affirmPromotionButton1, null, PromoPageType.PRODUCT, 1100, true);

or

// Option2 - Initialize by new
AffirmPromotionButton affirmPromotionButton2 = new AffirmPromotionButton(this);
affirmPromotionButton2.setAffirmColor(AffirmColor.AFFIRM_COLOR_TYPE_BLUE);
affirmPromotionButton2.setAffirmLogoType(AffirmLogoType.AFFIRM_DISPLAY_TYPE_LOGO);
((FrameLayout)findViewById(R.id.promo_container)).addView(affirmPromotionButton2);
Affirm.configureWithAmount(affirmPromotionButton2, null, PromoPageType.PRODUCT, 1100, true);

Tapping on the AffirmPromotionButton automatically start prequalification flow.

(Optional) If you want to handle errors, override onActivityResult so that affirm can handle the result.

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (Affirm.handlePrequalData(this, requestCode, resultCode, data)) {
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onAffirmPrequalError(String message) {
    Toast.makeText(this, "Prequal Error: " + message, Toast.LENGTH_LONG).show();
}

Track Order Confirmed

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

final AffirmTrack affirmTrack = AffirmTrack.builder()
        .setAffirmTrackOrder(affirmTrackOrder)
        .setAffirmTrackProducts(affirmTrackProducts)
        .build();

Affirm.trackOrderConfirmed(MainActivity.this, trackModel());
  • Since there is no callback, it will return success after 10 seconds timeout
  • We will replace using the HTTP API after the API is done

5. Example

  1. Copy the content of the gradle.properties.template to affirm/gradle.properties. This step is optional. There is a step inside affirm/build.gradle to do this automatically.
  2. Run the samples-java or samples-kotlin within Android Studio.

6. Test your integration

After completing your integration, do a thorough testing of both your front-end and order management functions in our sandbox to ensure that everything works as expected. Click here for our recommended test plan. However, you’ll need to tailor your testing plan to your specific systems, processes, and integration.

7. Deploy to production

Coordinate testing with Affirm

Before deploying the Affirm integration to your production site, Affirm will need to test it in your development or staging environment connected to our live environment. Contact your Integration Consultant or merchanthelp@affirm.com to coordinate this test.

Connect to the live Affirm environment

  1. Retrieve your live API keys at https://affirm.com/dashboard/#/apikeys
  2. Update the Affirm instance and set setMerchantPublicKey to your live API key and set setEnvironment to Affirm.Environment.PRODUCTION

Launch to production

After you have connected your integration to our live environment and we have tested it, you are ready to release your application in your production environment and offer Affirm as a payment option to your customers.

  • Was this article helpful?