Integration guide

Android / Hybrid Approach

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

❗️❗️Important ❗️❗️

Please note that this mode will will be deprecated in the future. If you are currently using Hybrid mode, please migrate from Hybrid mode as soon as possible. If you can’t, don’t hesitate to contact us and we’ll investigate a solution that will work with your integration needs.

Components

The components in the Hybrid approach are

  • An integration of Klarna Checkout from your servers to Klarna’s servers
  • A web page on your servers, where Klarna Checkout is fetched from the mobile SDK
  • Klarna Checkout SDK for mobile apps
  • Your mobile app

Step 1. Installation

The Payments SDK is available on our Maven Repository, perform the following steps to add it to your application: Edit your application level build.gradle file and add the following repository to your repositories section:

1
2
3
4
5
6
    repositories {
        ....
        maven {
            url 'https://x.klarnacdn.net/mobile-sdk/'
        }
    }

Add Maven dependency

1
implementation 'com.klarna.checkout:sdk:1.6.5'

Step 2. Prepare your app

Register an intent-filter for the Activity which will be hosting the checkout in your AndroidManifest.xml to support return URLs:

1
2
3
4
5
6
7
8
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data android:scheme="<your-custom-scheme>" />
        <data android:host="<your-custom-host>" />
    </intent-filter>

Note:

The Activity should be using launchMode singleTask or singleTop to prevent a new instance from being created when returning from an external application.

1
<activity android:launchMode="singleTask|singleTop">

Step 3. Prepare your web page with Klarna Checkout integrated

You will need Klarna Checkout integrated on a web page on your server. This integration guide will not go through the Klarna Checkout integration in detail, but assumes it has been set up already. If needed, you can learn how to do it in the regular Klarna Checkout Integration guide here.

Step 4. Initialize SDK with your WebView

Initialize the SDK with your web view. You first create a KlarnaCheckout object, and set the WebView to this object.

1
2
final KlarnaCheckout checkout = new KlarnaCheckout(this, returnURL);
checkout.setWebView(myWebView);

Step 5. Present your web page

From your WebView, load the URL to the web page where you have integrated Klarna Checkout. The SDK will automatically run when it detects the Checkout snippet in the WebView:

1
myWebView.loadUrl("https://www.example.store/checkout");

Step 6. 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

1
2
3
4
5
6
7
8
9
10
11
12
13
checkout.setSignalListener(new SignalListener() {  
    @Override  
    public void onSignal(String eventName, JSONObject jsonObject) {  
        if (eventName.equals("complete")) {  
            try {  
                final String confirmationURL = jsonObject.getString("uri");  
                loadConfirmationSnippet(confirmationURL);
            } catch (JSONException e) {  
                Log.e(TAG, e.getMessage(), e);  
            }  
        }  
    }  
);

You may also 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.

Step 7. Cleanup

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

1
checkout.destroy();

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