Last updated

KCO Android migration guide


This document provides a guide for migrating from the legacy Klarna Checkout SDK to the new Klarna Mobile SDK for Android.

Getting started

Klarna Checkout SDK is now part of the Klarna Mobile SDK. To integrate, change the Gradle dependency.

android {
   ...
   compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8
   }
   ...
}

Standalone Integration

Initialization Steps

Before:

Use the legacy Klarna Checkout SDK to initialize, get the view, and add it to your view hierarchy:

val klarnaCheckout = KlarnaCheckout(this, returnURL)
val checkoutView = klarnaCheckout.view
val container: ViewGroup = findViewById(R.id.checkoutContainer)
container.addView(checkoutView)

After:

In the Klarna Mobile SDK, you can initialize via XML or code.

Via XML:

<com.klarna.mobile.sdk.api.checkout.KlarnaCheckoutView
    ...
android:id="@+id/checkoutView"/>

Via code:

val checkoutView = KlarnaCheckoutView(
    context = this,
    returnURL = returnUrl,
    eventHandler = eventHandler
)
val container: ViewGroup = findViewById(R.id.checkoutContainer)
container.addView(checkoutView)

Setting the Return URL

Before:

Provide the return URL in the constructor.

val klarnaCheckout = KlarnaCheckout(this, returnURL)

After:

Set the return URL in XML or in the constructor.

XML:

<com.klarna.mobile.sdk.api.checkout.KlarnaCheckoutView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   app:klarnaReturnUrl="test://"/>

Code:

val checkoutView = KlarnaCheckoutView(
    context = this,
    returnURL = returnUrl,
    eventHandler = eventHandler
)

Handling Signals/Events

Before:

Use SignalListener to handle events.

klarnaCheckout.setSignalListener { eventName, jsonObject ->
   Log.d(TAG, "Received event: $eventName")
   val value = jsonObject.getString("param")
}

After:

Use KlarnaEventHandler for events and errors.

checkoutView.eventHandler = object : KlarnaEventHandler {
   override fun onEvent(klarnaComponent: KlarnaComponent, event: KlarnaProductEvent) {
      Log.d(TAG, "Received event: ${event.action}")
      val value = event.params["param"]
   }
   override fun onError(klarnaComponent: KlarnaComponent, error: KlarnaMobileSDKError) {
      Log.e(TAG, "Received error: ${error.message}")
   }
}

Setting the HTML Snippet

In both SDKs, use setSnippet(snippet).

Suspend and Resume the Checkout

In both SDKs, use checkout.resume() and checkout.suspend().

Handling External Payment Methods

Before:

Set merchantHandlesEPM to true.

checkout.merchantHandlesEPM = true

After:

Set merchantHandlesEPM in checkoutOptions.

checkout.checkoutOptions.merchantHandlesEPM = true

Handling Validation Errors

Before:

Set merchantHandlesValidationErrors to true.

checkout.merchantHandlesValidationErrors = true

After:

Set merchantHandlesValidationErrors in checkoutOptions.

checkout.checkoutOptions.merchantHandlesValidationErrors = true

Hybrid Integration

Before:

The legacy SDK integrated with WebView.

After:

Use KlarnaHybridSDK with WebView.

val klarnaHybridSDK = KlarnaHybridSDK(returnUrl, eventCallback.fullscreenEventCallback)
klarnaHybridSDK.addWebView(myWebView)
myWebView.loadUrl("https://www.example.store/checkout")

(Additional code provided for handling shouldOverrideUrlLoading).

For more details on hybrid integration and callbacks, see the linked page.