Adding Klarna Payments to Your App


Adding Klarna Payments to your application is as easy as adding a view and performing a few operations on it. You can read more about the flow here .

This guide will teach you how to:

  • Render a Payment View
  • Operate on a payment method via its Payment View.

Rendering a Payment View

A Payment View in Android is called a KlarnaPaymentView. You can initialize the KlarnaPaymentView in your activity either using XML or by creating it programmatically:

Creating the View from XML

You can add the view to your layout as shown below:

1
2
3
<com.klarna.mobile.sdk.api.payments.KlarnaPaymentView
    ...
    android:id="@+id/paymentView"/>

You should then be able to set up the view in code by setting a category and registering a callback:

1
2
3
val paymentView: KlarnaPaymentView = findViewById(R.id.paymentView)
paymentView.category = KlarnaPaymentCategory.PAY_LATER 
paymentView.registerPaymentViewCallback(callback)
1
2
3
final KlarnaPaymentView paymentView = findViewById(R.id.paymentView);
paymentView.setCategory(KlarnaPaymentCategory.PAY_LATER);
paymentView.registerPaymentViewCallback(callback);

Creating the View from Code

If you instead want to create the view in code, you can use its constructor to set it up with a single line.

ParamTypeDescription
Param
context
Type
Context
Description
The context of the Activity the Payment View is in.
Param
category
Type
String
Description
The payment method category that will be rendered in the view.
Param
callback
Type
KlarnaPaymentViewCallback
Description
Callback interface that receives events during the payment process.
1
val paymentView = KlarnaPaymentView(context = this, category = KlarnaPaymentCategory.PAY_LATER, callback = callback)
1
final KlarnaPaymentView paymentView = new KlarnaPaymentView(context, KlarnaPaymentCategory.PAY_LATER, callback);

The Payment View Callback

The SDK will notify you of events via a callback object that you’ll need to implement. It acts like a listener on Android, however, unlike a listener, it’s not optional.

Working with a Payment View

We expose six methods on the payment view that you can use to interact with the payment method it’s presenting:




Initializing the View

Before content is rendered into a payment view, it needs to be configured. You can do this by initializing the view.

Initialization

After the view has been added and set up, you’ll need to call initialize() with the clientToken you received from the back-end and a returnURL.

ParamTypeDescription
Param
clientToken
Type
String
Description
Token that was returned when you created the session.
Param
returnUrl
Type
String
Description
URL schema as defined in your AndroidManifest.xml to return from external applications.
1
paymentView.initialize(clientToken, returnUrl)
1
paymentView.initialize(clientToken, returnUrl);

Result

If successful, onInitialized() will be called in the callback you supplied (in e.g registerPaymentViewCallback). If it’s not successful onErrorOccurred() will be called instead.

ParamTypeDescription
Param
view
Type
KlarnaPaymentView
Description
The payment view that was initialized.
1
2
3
override fun onInitialized(view: KlarnaPaymentView) {
    view.load(null)
}
1
2
3
4
@Override
public void onInitialized(@NotNull KlarnaPaymentView view) {
    view.load(null);
}


Loading the View

Once you’ve initialized the view and you’re ready to display the KlarnaPaymentView.

Loading

Simply call load(), supplying an optional string with updated order data to update the session. This string should be formatted as valid JSON.

ParamTypeDescription
Param
args
Type
String?
Description
An optional string with the order data to update the session. Formatted as JSON.
1
paymentView.load(null)
1
paymentView.load(null);

Result

If successful, onLoaded() will be called in the callback you supplied. If anything went wrong, onErrorOccurred() will be called. If you’ve loaded several views, you should keep track of which view your user selected to know what to authorize in the next step.

ParamTypeDescription
Param
view
Type
KlarnaPaymentView
Description
The payment view that was loaded.
1
2
3
override fun onLoaded(view: KlarnaPaymentView) {
    // content has finished loading and if you have any progress bar you could hide it here
}
1
2
3
4
@Override
public void onLoaded(@NotNull KlarnaPaymentView view) {
    // content has finished loading and if you have any progress bar you could hide it here
}


Authorizing the Session

When the payment view is loaded, and the user has confirmed that they want to pay with one of Klarna’s selected payment methods, it’s time to authorize the session.

Authorize

When you are ready to authorize the session, call authorize(). As with load, you can supply an optional string to update the session. You can also specify whether auto-finalization should be turned off; if it is, the user may need to be prompted a second time to input data.

ParamTypeDescription
Param
autoFinalize
Type
Boolean?
Description
An optional flag used to turn off auto-finalization for direct bank transfer.
Param
sessionData
Type
String?
Description
An optional string to update the session. Formatted as JSON.
1
paymentView.authorize(false, null)
1
paymentView.authorize(false, null);

Result

If successful, onAuthorized() will be called in the callback. If not, onErrorOccurred() will be called.

ParamTypeDescription
Param
view
Type
KlarnaPaymentView
Description
The payment view that was authorized.
Param
approved
Type
Boolean
Description
Whether the authorization was approved or not.
Param
authToken
Type
String?
Description
If the session was authorized, the token will not be null.
Param
finalizedRequired
Type
Boolean?
Description
Will be true if autoFinalize was false and this payment method needs a second confirmation step.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
override fun onAuthorized(
    view: KlarnaPaymentView,
    approved: Boolean,
    authToken: String?,
    finalizedRequired: Boolean?
) {
    if (approved) {
        // the authorization was successful
    } else {
        // user is not approved or might require finalization
    }
    
    if (finalizedRequired) {
        // app needs to call finalize()
    } 

    if (authToken != null) {
        // authorization is successful, backend may create order
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Override
public void onAuthorized(@NotNull KlarnaPaymentView view, boolean approved, @org.jetbrains.annotations.Nullable String authToken, @org.jetbrains.annotations.Nullable Boolean finalizedRequired) {
    if (approved) {
        // the authorization was successful
    } else {
        // user is not approved or might require finalization
    }
    
    if (finalizedRequired) {
        // app needs to call finalize()
    } 

    if (authToken != null) {
        // authorization is successful, backend may create order
    }
}



Reauthorizing the Session

There are cases when you might want to allow your customer to change their order after it has been authorized (e.g. in some form of order/summary view). In these cases, if the order or customer details have changed, you’ll need to reauthorize the session.

Reauthorize

ParamTypeDescription
Param
sessionData
Type
String?
Description
An optional string to update the session. Formatted as JSON
1
paymentView.reauthorize(null)
1
paymentView.reauthorize(null);

Result

If successful, onReauthorized() will be called, and if not, onErrorOccurred() will be called instead.

ParamTypeDescription
Param
view
Type
KlarnaPaymentView
Description
The payment view that was reauthorized.
Param
approved
Type
Boolean
Description
Whether the reauthorization was approved or not.
Param
authToken
Type
String?
Description
If the session was previously authorized, the token will not be null.
1
2
3
4
5
6
7
8
9
10
11
override fun onReauthorized(view: KlarnaPaymentView, approved: Boolean, authToken: String?) {
    if (approved) {
        // the authorization was successful
    } else {
        // user is not approved or might require finalization
    }

    if (authToken != null) {
        // authorization is successful, backend may create order
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
@Override
public void onReauthorized(@NotNull KlarnaPaymentView view, boolean approved, @org.jetbrains.annotations.Nullable String authToken) {
    if (approved) {
        // the authorization was successful
    } else {
        // user is not approved or might require finalization
    }

    if (authToken != null) {
        // authorization is successful, backend may create order
    }
}


Finalizing the Session

If the session needs to be finalized, you’ll need to perform this last step to get an authorization token.

Finalize

Call finalize() in order to get the token.

ParamTypeDescription
Param
sessionData
Type
String?
Description
An optional string to update the session. Formatted as JSON
1
paymentView.finalize(null)
1
paymentView.finalize(null);

Result

If successful, onFinalized() will be called in the callback. If not successful, onErrorOccurred() will be called.

ParamTypeDescription
Param
view
Type
KlarnaPaymentView
Description
The payment view that was finalized.
Param
approved
Type
Boolean
Description
Whether the user was approved on finalize or not.
Param
authToken
Type
String?
Description
If the finalization went through, the token will not be null.
1
2
3
4
5
6
7
8
9
10
11
override fun onFinalized(view: KlarnaPaymentView, approved: Boolean, authToken: String?) {
    if (approved) {
        // the authorization was successful
    } else {
        // user is not approved or might require finalization
    }

    if (authToken != null) {
        // authorization is successful, backend may create order
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
@Override
public void onFinalized(@NotNull KlarnaPaymentView view, boolean approved, @org.jetbrains.annotations.Nullable String authToken) {
    if (approved) {
        // the authorization was successful
    } else {
        // user is not approved or might require finalization
    }

    if (authToken != null) {
        // authorization is successful, backend may create order
    }
}


Loading a Payment Review

If you’d like to allow the user to review their payment after it’s authorized, this can be done in two ways:

  1. Render it in a new view:

    • Create a new KlarnaPaymentView with the same payment method
    • Call initialize()
    • Call then loadPaymentReview()
  2. Render it in the existing payment view:

    • Call loadPaymentReview() in the existing view.

Only specific payment method categories and countries are currently supported. Contact us to make sure that you can call this method.

Load the Payment Review

This will load/replace the view’s content with a description of how your customer will have to perform payment.

1
paymentView.loadPaymentReview()
1
paymentView.loadPaymentReview();

Result

If successful onLoadPaymentReview() will be called in your callback. If not, onErrorOccurred() will be called instead.

ParamTypeDescription
Param
view
Type
KlarnaPaymentView
Description
The payment view that dsplayed payment review.
1
2
3
4
5
6
7
override fun onLoadPaymentReview(view: KlarnaPaymentView, showForm: Boolean) {
    if (showForm) {
        // successfully loaded the content in the payment view and the content is visible
    } else {
        // something went wrong when loading the content and the content is not visible
    }
}
1
2
3
4
5
6
7
8
@Override
public void onLoadPaymentReview(@NotNull KlarnaPaymentView view, boolean showForm) {
    if (showForm) {
        // successfully loaded the content in the payment view and the content is visible
    } else {
        // something went wrong when loading the content and the content is not visible
    }
}