Klarna Docs - Adding Klarna Checkout SDK to Your App - Native Integration

Adding Klarna Checkout SDK to Your App - Native Integration

Integration guide for how to integrate Klarna Checkout in your Android app, using the native integration.

This guide will teach you how to:

  • Initialize the Checkout SDK
  • Present the checkout view in your app
  • Listen to SDK events

COMPONENTS

The components in the native approach are:

  • An integration of Klarna Checkout from your servers to Klarna’s servers
  • ​An end-point on your servers, where Klarna Checkout is fetched from the mobile SDK
  • Klarna Checkout SDK for mobile apps
  • Your mobile app

In your mobile app you will need to fetch a Klarna Checkout HTML snippet from your web server. This integration guide will not go through the Klarna Checkout integration in detail, but assumes it has been set up already and is available at an API endpoint YOUR-URL on your servers. If needed, you can learn how to do it in the regular Klarna Checkout Integration guide here.

The communication between your mobile app, Klarna’s SDK, your servers and Klarna’s servers.

WORKING WITH THE CHECKOUT SDK

Initialize SDK with the Checkout snippet

To initialize the Klarna Checkout in your native flow, create a KlarnaCheckout object and supply a Checkout HTML snippet hosted at YOUR-URL :

ParamTypeDescription
currentActivityActivityThe Activity that will contain the checkout view
returnURLStringURL schema as defined in yourAndroidManifest.xmlto return from external applications. Learn more about it here

Present Klarna Checkout view

To display the checkout, simply retrieve an instance of the checkout view and place it in a ViewGroup:

Configure event listeners

The only event you need to listen to is the “complete event”. When the complete event is triggered, redirect the customer to the confirmation page within your app.

Following is a code example of how this is done:

Optionally, you may want to set up event listeners to track field changes in the Klarna Checkout before the order is placed. See the reference guide for what events are available.

Suspend and resume Checkout

When a server-side call to Klarna is being processed, you can trigger the 'suspend' action to prevent the user/customer from altering the state of their Checkout order. The suspend action will turn the Klarna Checkout unresponsive to user interactions.

JAVA
checkout.suspend()

After the server-side call to Klarna has been processed, you should trigger the 'resume' action to once again let the user interact with Klarna Checkout.

JAVA
checkout.resume()

Handling External Payment Methods

If you wish to handle external payment methods (such as PayPal, Google Pay, etc), you can turn on the following feature flag in the SDK. By doing so, the Klarna Checkout will not handle the external payment methods automatically:

JAVA
checkout.merchantHandlesEPM = true

Note: This flag should be set before starting the Checkout process.

Whenever the SDK reaches an external payment method selected by the customer, you will receive an event named 'external' in your callback with a 'uri' parameter pointing to the external payment’s URL:

KOTLIN
override fun onSignal(eventName: String, jsonObject: JSONObject) {
   if (eventName == "external") {
      val externalPaymentUrl = jsonObject.getString("uri")
      openExternalPayment(externalPaymentUrl)
   }
}
JAVA
@Override
public void onSignal(String eventName, JSONObject jsonObject) {
   if (eventName.equals("external")) {
      String externalPaymentUrl = jsonObject.getString("uri");
      openExternalPayment(externalPaymentUrl);
   }
}

Handling Validation Errors

If you wish to handle validation errors and you don’t want Klarna Checkout to show a pop-up window showing the errors, you can turn on the following feature flag in the SDK:

JAVA
checkout.merchantHandlesValidationErrors = true

Note: This flag should be set before starting the Checkout process.

Whenever the SDK detects a validation error, you will receive an event named 'validation_error' in your callback with 'error_type' and 'error_text' parameters:

KOTLIN
override fun onSignal(eventName: String, jsonObject: JSONObject) {
   if (eventName == "validation_error") {
      val errorType = jsonObject.getString("error_type")
      val errorText = jsonObject.getString(“error_text”)
      handleValidationError(errorType, errorText)
   }
}
JAVA
@Override
public void onSignal(String eventName, JSONObject jsonObject) {
   if (eventName.equals("validation_error")) {
      String errorType = jsonObject.getString("error_type");
      String errorText = jsonObject.getString("error_text");
      handleValidationError(errorType, errorText);
   }
}

Cleanup

When you are done with the checkout, make sure to call destroy() to release resources.

Return URL

Some payment methods require authorization through third-party applications. These can return to your application upon completion, but to do that, you need to supply a URL that should be used for returning. There do not need to be any special handlers on application load for that URL. Our only requirement is that the user is returned to your application from the third-party application.

Note:

In cases where the user needs to authenticate with their bank for credit card payments, the bank itself might open a third-party app such as Bank ID. Since the SDK does not create these sessions, the user would have to return to the app manually, and then you will get the completion signal from the checkout.

Next steps

Success! You now have Klarna Checkout up and running in your mobile app.

Check out our git-hub page where you can

  • Read API documentation
  • Look at an example app
  • Report an issue