Integration guide

Android / Native Approach

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

Components

The components in the Native approach are

  • An integration from your system to Klarna Checkout
  • ​An end-point 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 backend endpoint to fetch a Checkout snippet

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 .

Step 4. Initialize SDK with the Checkout snippet

To initialize the Klarna Checkout in your native flow, you supply a Checkout HTML snippet from YOUR-URL

1
2
final KlarnaCheckout checkout = new KlarnaCheckout(this, returnURL);
checkout.setSnippet(snippet);

Step 5. Present Klarna Checkout view

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

1
2
3
final View checkoutView = checkout.getView();
final ViewGroup container = findViewById(R.id.checkoutContainer);
container.addView(checkoutView);

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);  
            }  
        }  
    }  
);

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.

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