Set Up Virtual Card on Android
Learn how to set up Affirm Virtual Card on your Android mobile app.
Prerequisites
- Review About Affirm Virtual Card.
Steps
1. Set up Affirm on Android
Note: The Android SDK is open-source and fully documented.
With Gradle
To install the SDK, add com.affirm:affirm-android-sdk 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.36'
}With Maven
Add the Affirm dependency to your pom.xml file:
<dependency>
<groupId>com.affirm</groupId>
<artifactId>affirm-android-sdk</artifactId>
<version>2.0.36</version>
</dependency>
SDK ReleasesFor 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 so that it can make requests to the Affirm server:
Affirm.initialize(new Affirm.Configuration.Builder("public key")
.setEnvironment(Affirm.Environment.SANDBOX)
.setMerchantName("merchant name")
.setReceiveReasonCodes("true")
.setLogLevel(Affirm.LOG_LEVEL_DEBUG)
.setCheckoutRequestCode(8001)
.setVcnCheckoutRequestCode(8002)
.setPrequalRequestCode(8003)
.build());3. Render Affirm Checkout
Checkout creation is the process by which a customer uses Affirm to pay for an in-app purchase. Create a checkout object and launch the Affirm checkout using this Checkout function:
The example assumes items and shipping already contain the order details. Import java.math.BigDecimal and com.affirm.android.model.Billing alongside the other SDK types. This example uses the shipping address for billing; supply the customer's billing details when they differ.
final Billing billing = Billing.builder()
.setAddress(shipping.address())
.setName(shipping.name())
.build();
final Checkout checkout = Checkout.builder()
.setOrderId("order id")
.setItems(items)
.setBilling(billing)
.setShipping(shipping)
.setShippingAmount(BigDecimal.ZERO)
.setTaxAmount(new BigDecimal("100.00"))
.setTotal(new BigDecimal("1100.00"))
.build();
Affirm.startCheckout(this, checkout, true);
AmountsPass dollar amounts as
BigDecimalvalues. Keep the total consistent with your order items, shipping, and tax.
Optional VCN screens
The SDK also provides a VCN flow with a loan-amount screen and a Virtual Card display screen. Start it with Affirm.startNewVcnCheckoutFlow(activity, checkout). See the VCN flow guide for details.
4. Handle Callbacks
Override onActivityResult and then call the handleVcnCheckoutData method. Implement Affirm.VcnCheckoutCallbacks on your activity and import com.affirm.android.model.VcnReason for the cancellation-reason callback. This lets your activity receive the CardDetails object in onAffirmVcnCheckoutSuccess. If you have PCI concerns, you can also decide to only retrieve card details from your server using the checkout_token returned by CardDetails.checkoutToken(). To prevent card details from being included in the client-side callback, contact Affirm to configure your account.
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (Affirm.handleVcnCheckoutData(this, requestCode, resultCode, data)) {
return;
}
super.onActivityResult(requestCode, resultCode, data);
}Implement VCN callbacks
@Override
public void onAffirmVcnCheckoutCancelled() {
Toast.makeText(this, "Vcn Checkout Cancelled", Toast.LENGTH_LONG).show();
}
@Override
public void onAffirmVcnCheckoutCancelledReason(@NonNull VcnReason vcnReason) {
Toast.makeText(this, "Vcn Checkout Cancelled: " + vcnReason.toString(), 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
onAffirmVcnCheckoutSuccesscallback receives aCardDetailsobject. The card details are then forwarded to your server to be used with your existing card payment rails.
What's next?
Updated 1 day ago