Klarna Docs - Klarna Payments (Native)

Klarna Payments (Native)

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:

JSON
<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:

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
contextContextThe context of the Activity the Payment View is in.
categoryStringThe payment method category that will be rendered in the view.
callbackKlarnaPaymentViewCallbackCallback interface that receives events during the payment process.

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
clientTokenStringToken that was returned when you created the session.
returnUrlStringURL schema as defined in your AndroidManifest.xml to return from external applications.

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
viewKlarnaPaymentViewThe payment view that was initialized.

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
argsString?An optional string with the order data to update the session. Formatted as JSON.

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
viewKlarnaPaymentViewThe payment view that was loaded.

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
autoFinalizeBoolean?An optional flag used to turn off auto-finalization for direct bank transfer.
sessionDataString?An optional string to update the session. Formatted as JSON.

Result

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

ParamTypeDescription
viewKlarnaPaymentViewThe payment view that was authorized.
approvedBooleanWhether the authorization was approved or not.
authTokenString?If the session was authorized, the token will not be null.
finalizedRequiredBoolean?Will be true if autoFinalizewas false and this payment method needs a second confirmation step.

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
sessionDataString?An optional string to update the session. Formatted as JSON

Result

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

ParamTypeDescription
viewKlarnaPaymentViewThe payment view that was reauthorized.
approvedBooleanWhether the reauthorization was approved or not.
authTokenString?If the session was previously authorized, the token will not be null.

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
sessionDataString?An optional string to update the session. Formatted as JSON

Result

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

ParamTypeDescription
viewKlarnaPaymentViewThe payment view that was finalized.
approvedBooleanWhether the user was approved on finalize or not.
authTokenString?If the finalization went through, the token will not be null.

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:
    undefinedundefinedundefined
  2. Render it in the existing payment view:
    undefined

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.

Result

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

ParamTypeDescription
viewKlarnaPaymentViewThe payment view that dsplayed payment review.