Integration guide

Android / Hybrid Approach 

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

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

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 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 web view to this object.

KlarnaCheckout checkout = new KlarnaCheckout(myCurrentActivity);
checkout.setWebView(myWebView);

 

Step 5. Present your web page

From the web view, load the URL to the web page where you have integrated Klarna Checkout.

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

 

Step 6. Configure event listeners

To handle the signals received from the SDK you should set up an observer listening to the KCOSignalNotification. To make sure the checkout shows the succesful screen you need to redirect your webview to the completion url upon recieveing the complete message.

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

 

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.

 

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