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

Either use Maven, or manually install the Klarna Checkout SDK for mobile apps in your environment.

Maven

compile 'com.klarna.checkout.sdk:klarna-checkout-sdk:1.5.0'

 

Manual installation

If you don't use Maven, then manually download the Android SDK aar-library from github and define it in your build.gradle

repositories {
        flatDir {
            dirs 'libs'
        }
    }
compile (name: 'klarna-checkout', ext: 'aar')

 

Step 2. Prepare your app

Add permissions in your Android manifest file

 <!-- Standard permissions required by the SDK -->
    <permission android:name="android.permission.INTERNET" />
    <permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <permission android:name="android.permission.CHANGE_WIFI_STATE" />

 

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

KlarnaCheckout checkout = new KlarnaCheckout(myCurrentActivity);

Setting the snippet will start preloading the checkout, which ensures a better experience for the users.

checkout.setSnippet("<div id='klarna-checkout-container'><script>...</script></div>");

 

Step 5. Present Klarna Checkout view

Create a view and add it to a view group, to present the Klarna Checkout

View view = checkout.getView();
ViewGroup placeholder = ((ViewGroup) findViewById(R.id.kcoView));
placeholder.addView(view);

 

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

checkout.setSignalListener(new SignalListener() {
            @Override
            public void onSignal(String eventName, JSONObject jsonObject) {
                if (eventName.equals("complete")) {
                    try {
                        String url = jsonObject.getString("uri");
                        loadConfirmationSnippet(url);
                    } catch (JSONException e) {
                        Log.e(e.getMessage(), e.toString());
                    }
                }
            }
);

 

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.

 

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