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 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>
SDK Releases
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 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()
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:
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);
Floats
The checkout object contains details about the order. Amounts should be dollar amounts represented as Floats.
4. Handle Callbacks
Override onActivityResult
and then call the handleCheckoutData method. This returns the CardDetails
object, which contains the Affirm card details. If you have PCI concerns, you can also decide to only retrieve card details from your server-side 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);
}
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 receives aCardDetails
object. The card details are then forwarded to your server to be used with your existing card payment rails.
Recommended Topics
Updated 6 days ago