Virtual Card on Android

Learn how to integrate Affirm on Android with our virtual card.

Starting a payment with Affirm with a virtual card on an Android client consists of creating a checkout form, tokenizing customer information and generating a card. The card can then be used with your existing card payment rails.

This guide shows you how to include an Android client on your checkout page.

The Android SDK is open-source and fully documented.

Step 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:

apply plugin: 'com.android.application'

android { ... }

dependencies {
  // ...

  // Affirm Android SDK
  implementation 'com.affirm:affirm-android-sdk:2.0.7'
}

With Maven

Add the Affirm dependency to your pom.xml file:

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

🚧

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.

Step 2: Initialize Affirm

Configure the SDK with your Affirm public API key so that it can make requests to the Affirm server.

Affirm.initialize(new Affirm.Configuration.Builder("public key")
      .setEnvironment(Affirm.Environment.SANDBOX)
      .setName("merchant name")
      .setUseVcn("true")
      .setReceiveReasonCodes("true")
      .setLogLevel(Affirm.LOG_LEVEL_DEBUG)
      .setCheckoutRequestCode(8001)
      .setVcnCheckoutRequestCode(8002)
      .setPrequalRequestCode(8003)
      .build()

Step 3: Render Affirm Checkout

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

final Checkout checkout = Checkout.builder()
        .setOrderId("order id")
        .setItems(items)
        .setBilling(shipping)
        .setShipping(shipping)
        .setShippingAmount(0f)
        .setTaxAmount(100f)
        .setTotal(1100f)
        .build();
 
Affirm.startCheckout(this, checkout, false);

🚧

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

Step 4: Handle callbacks

Be sure to override onActivityResult, then call the handleCheckoutData method. it will return CardDetails object containing the Affirm card details. You can also decide to only retrieve card details from your server-side if you have PCI concerns, via the checkout_id.

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (Affirm.handleVcnCheckoutData(this, requestCode, resultCode, data)) {
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Here are additional handler functions available:

@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();
}

👍

Once the checkout has been successfully confirmed by the user, the AffirmCheckoutDelegate object will receive a CardDetails object. The card details should be forwarded to your server to be used with your existing card payment rails.