Native View - React Native

Adding Klarna payments to your application is as easy as adding a view and performing the payment operations on it.

Klarna’s Mobile SDK is the official toolkit for integrating Klarna products into native iOS apps. It enables you to offer Klarna’s payment methods with a seamless in-app user experience. The SDK is designed to provide the optimal integration and, under the hood, the SDK handles web-based flows in a mobile-friendly way to reduce friction and ensures that all the features listed below are fully supported for a superior customer experience across all mobile platforms.

Feature support with Klarna’s Mobile SDK:

  • Remember Returning Customers: enable faster checkouts and improved conversion by reducing friction for returning users.
  • Device-Wide Login Experience: simplify repeat purchases by enabling customers to remain logged in to Klarna across merchant apps on the same device.
  • Passkey Support: offer biometric-based login for enhanced security and a frictionless user authentication experience.
  • Application Redirects: seamlessly redirect users to external apps (e.g. bank or identity verification apps) and ensure a smooth return back into your app to complete payments without disruption.
  • Camera Access for ID Verification: improve acceptance by enabling secure and instant identity verification within the app.
  • Secure Browser Context: provide a secure, embedded browser environment for displaying Klarna-hosted content (payment pages or terms).
  • File Sharing and Download for T&Cs: Let customers download or share terms and conditions directly from the app for full transparency.

React native checkout view

For a Mobile SDK integration your Payment Server and Mobile App must work together to complete the purchase flow:

sequenceDiagram participant Consumer participant Merchant App participant Mobile SDK participant Merchant Server participant Klarna API Consumer->>Merchant App: Navigate to checkout screen Merchant App->>Merchant Server: Create payment session Merchant Server->>Klarna API: Create Klarna Payment session Note over Merchant Server, Klarna API: "intent" should be set in relevance to the desired use case Klarna API-->>Merchant Server: Klarna payment session response Merchant Server-->>Merchant App: Klarna payment session client_token Merchant App->>Mobile SDK: Create the Payment View Note over Merchant App: Includes 'Klarna' category and an eventListener Merchant App->>Mobile SDK: Initialize the payment session using client_token Merchant App->>Mobile SDK: Load the Payment View Consumer->>Merchant App: Select Klarna in Payment selector Merchant App->>Merchant App: Display the KlarnaPaymentView Consumer->>Merchant App: Click "Continue with Klarna" Merchant App->>Mobile SDK: Authorize the session Note over Merchant App: Consumer completes to purchase flow. Mobile SDK-->>Merchant App: Provide authorization token (valid for 60 minutes) Merchant App->>Merchant Server: Sends the authorization token for creating order Merchant Server->>Klarna API: Create Order POST (setup/payments/v1/authorizations/{authorizationToken}/order) Klarna API-->>Merchant Server: Provide order_id and redirect_url Merchant Server-->>Merchant App: Provide order details Merchant App->>Consumer: Redirected to order confirmation screen
  1. Prepare: Make sure you have the credentials for Klarna Payments.
  2. Initiate payment (Server-side): Create a payment session from your backend and pass client_token to your app.
  3. Set up your app (Mobile App): Install the Klarna SDK using your package manager.
  4. Render payment view (Mobile App): Present Klarna’s native view to display Klarna in your checkout.
  5. Authorize the session (Mobile App): Trigger the authorization through the SDK and handle the callback.
  6. Create an order (Server-side): Pass the resulting authToken to your backend to finalize the payment and create an order.

Presenting Klarna in your mobile checkout

To maximize conversion and usability, you must dynamically retrieve Klarna’s branding, payment descriptors, and payment widget via Klarna’s API and SDK to present accurate, localized, and up-to-date payment options.

  1. Payment Descriptor: Dynamically populated in Klarna Payment session response, in the payment_method_categories.name field.
  2. Payment Sub header: Added by merchant.
  3. Widget: Provided with the KlarnaPaymentView from the Mobile SDK.
  4. Klarna Badge: Dynamically populated in Klarna Payment session response, in the payment_method_categories.asset_urls.standard field.

When you integrate Klarna payments into your online store, your customers see Klarna as an option when they select a payment method for their purchases. If your customers select Klarna for their purchase, they are redirected to log into their Klarna account.

Your customers select their preferred Klarna payment option (pay now, pay later, pay in parts) once they're logged into their Klarna account. We handle the Klarna account user flow, so you don't have to worry about it.

Before you start integrating Klarna payments, there are a few things you need to prepare in advance:

When your customer wants to pay with Klarna, you have to open a payment session and share the shopping cart details in a POST request to the {apiURL}/payments/v1/sessions endpoint. In that request, you also specify if the payment is one-time or recurring.

Once you start a payment session, it stays open for 48 hours or until you place an order. You can also send a separate POST request to cancel the session.

Authentication

Klarna payments API uses HTTP basic authentication. To authenticate, use your API credentials that consist of:

  • A username linked to your Klarna merchant ID (MID)
  • A password associated with your username

If you're using an API platform that lets you store your credentials, you can add them in relevant fields. Otherwise, make sure to include the Base64-encoded username:password in the Authorization header field of each API request, as shown below.

JSON
Authorization: Basic pwhcueUff0MmwLShJiBE9JHA==

A sample authorization request header with Base64-encoded credentials.

Common Parameters

To get a success response, include the following required parameters in your POST {apiURL}/payments/v1/sessions request.

ParameterDescription
localeThe language of information presented on the Klarna widget. Learn more about using locale in API calls.
purchase_country requiredThe country where the purchase is made. Learn more about supported countries.
purchase_currency requiredThe currency in which the customer is charged. Learn more about supported currencies.
order_amount requiredThe total price of the order, including tax and discounts.
order_lines requiredThe details of order lines in the purchase.
intentThe purpose of the payment session.
merchant_urls.authorizationGet a callback once the customer has completed the flow and you can create an order.

Success Response

In response to a create session call, you receive:

  • session_id, a payment session identifier you can use to [ update the session] and [ retrieve session] details
  • client_token, a token you pass to the JavaScript SDK or Mobile SDK(Android, iOS and React Native) to launch the Klarna widget
  • payment_method_categories, an array that lists the grouped Klarna payment methods available for the session. We can respond with one or more categories depending on the market and account configuration.
JSON
{
"session_id": "068df369-13a7-4d47-a564-62f8408bb760",
"client_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjAwMDAwMDAwMDAtMDAwMDAtMDAwMC0wMDAwMDAwMC0wMDAwIiwidXJsIjoiaHR0cHM6Ly9jcmVkaXQtZXUua2xhcm5hLmNvbSJ9.A_rHWMSXQN2NRNGYTREBTkGwYwtm-sulkSDMvlJL87M",
"payment_method_categories": [
{
"identifier": "klarna"
"name" : "Pay with Klarna",
"asset_urls" : {
"descriptive" : "https://x.klarnacdn.net/payment-method/assets/badges/generic/klarna.svg",
"standard" : "https://x.klarnacdn.net/payment-method/assets/badges/generic/klarna.svg"

A sample success response to the create session call.

Error Response

If your request doesn't pass our validation, you'll receive an error response.

JSON
{
"correlation_id": "6a9b1cb1-73a3-4936-a030-481ba4bb203b",
"error_code": "BAD_VALUE",
"error_messages": [
"Bad value: order_lines"
]
}

A sample error response caused by incorrect order line details.

​Go to Error Handling to learn more about common errors and troubleshooting suggestions. You can use the value in correlation_id to find entries related to the request under Logs in the Merchant portal.

NPM

If you want to add the SDK via npm use the following to add library dependency:

BASH
npm install react-native-klarna-inapp-sdk --save

Yarn

If you are using yarn, then use the following to add library dependency:

SHELL
yarn add react-native-klarna-inapp-sdk

For installing native dependencies for iOS, go to your ios directory and run pod install.

You need to add a reference to the repository in your own app’s build.gradle which can be done by adding the lines between the comments below:

GROOVY
allprojects {
    repositories {
        ...
        // Required configuration to fetch native Android SDK
        maven {
            url 'https://x.klarnacdn.net/mobile-sdk/'
        }
    }
}

Return URL

Mobile SDK integrations might, at some point, open third-party applications. To automatically return the user, these third-party applications need to know how to build a return intent or URL.

To do that, you’ll need to provide the SDK with what we call the “Return URL” parameter. If you haven’t done so already, follow this documentation for Android and this documentation for iOS.

Return URL

Klarna purchase flows might require authorizations in other applications (e.g. bank apps) or do a handover to the Klarna app. In such cases, a return URL to your application ensures seamless return to the flow in your app, hence setting up a return URL is required. It is expected that redirects to this URL should only open your application without any changes in the UI state, ensuring the customer can continue the flow prior to external navigation.

You can set up a Return URL app scheme to your application by configuring a custom URL scheme.

Important: The return URL string passed to Klarna must include :// after the scheme name. For example, if you defined myApp as the scheme, you must use "myApp://" as the return URL argument to Klarna.

To avoid a Klarna specific app scheme, you can use a host in a common scheme for Klarna redirects, e.g. myApp://klarna-redirect , this can allow you to differentiate and handle these redirect in your handler.

Considering the return URL is a constant value in Constants.klarnaReturnUrl, you can handle redirects to your return URL as such:

Klarna App URL Queries

Klarna flows on mobile utilize Application Queries for Klarna app schemes to offer seamless app handover experience to customers. In order for the SDK to check availability of the Klarna app, we need you to enable querying Klarna app on the device by adding Klarna app schemes to LSApplicationQueriesSchemes.

This can be configured easily in XCode by going to your project setting and under "Info"(alternatively this is also available in your Info.plist file) you should see an entry list for Queried URL Schemes, this list should contain the klarna and klarnaconsent schemes:

In order to use the SDK from your React Native application import it by:

JAVASCRIPT
import KlarnaPaymentView from 'react-native-klarna-inapp-sdk';

You can import the KlarnaPaymentView from the library. You’ll then be able to add it as a component to your app. This component exposes callbacks as props and methods you can call via the component’s ref.

The view will auto-size height-wise and grow to fill it’s containing view’s width.

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

TYPESCRIPT
const paymentViewRef = useRef<KlarnaPaymentView>(null);

const createPaymentView = () => {
    return (
        <KlarnaPaymentView
            ref={paymentViewRef}
            style={styles.paymentView}
            category={"klarna"}
            returnUrl={'returnUrl://'}
            onInitialized={() => {
NameTypeDescription
returnUrlStringApp scheme URL as defined in set up to return from external applications.
categoryStringShould always be set to "klarna" as it's the preferred payment category.
onInitialized() => {}The initialize call succeeded.
onLoaded() => {}The load call succeeded.
onLoadedPaymentReview() => {}The load payment review call succeeded.
onAuthorized({}) => {}The authorize call succeeded.
onReauthorized({}) => {}The reauthorize call succeeded.
onFinalized({}) => {}The finalize call succeeded.
onError({}) => {}An error occurred.

Before content is rendered into a payment view or an authorization, payment session with it's client token needs to be initialized. This can be done by calling initialize and handling result in klarnaInitialized.

TYPESCRIPT
// initilize with clientToken for session created from server-side
paymentViewRef.current?.initialize(props.clientToken);
ParamTypeDescription
clientTokenStringThe client token you get from Klarna Payments API session response.

If successful, onInitialized will be called in the property you supplied. If it’s not successful, onError will be called instead.

TYPESCRIPT
onInitialized={() => {
    paymentViewRed.current?.load() // optionally load payment widget upon initialize 
}

If successful, klarnaInitialized from KlarnaPaymentEventListener will be called in the listener you supplied. If it’s not successful, klarnaFailed will be called instead.

Load payment widget

Once you’ve initialized the view and you’re ready to display the payment widget, simply by calling load.

TYPESCRIPT
// load optional payment widget
paymentViewRef.current?.load();
ParamTypeDescription
sessionDataString undefinedAn optional string to update the session. Formatted as JSON.

If successful, onFinalized will be called in property you supplied and for errors onError will be called instead.

TYPESCRIPT
onFinalized={(approved, authToken) => {
    if (authToken && approved) {
        // finalize is successful, backend may create order
    }
}

To continue with the purchase, you have to create an order in Klarna's system. This step takes place in the server side through the Klarna payments API.

To create an order for a one-time payment, send a POST request to the {apiUrl}/payments/v1/authorizations/{authorizationToken}/order endpoint and include authorization_token in the path.

For example, if the authorization_token is b4bd3423-24e3, send your request to the {apiUrl}/payments/v1/authorizations/b4bd3423-24e3/order endpoint.

JSON
{
"purchase_country": "US",
"purchase_currency": "USD",
"billing_address": {
"given_name": "John",
"family_name": "Doe",
"email": "john@doe.com",
"title": "Mr",
"street_address": "Lombard St 10",
"street_address2": "Apt 214",

A POST request to create an order for a one-time payment.

Success response

When you receive a success response, the customer gets charged and the Klarna payments session is closed.

As part of the response, you receive the following details:

  • order_id, an order identifier that you can later use to capture or refund the order using the Order management API
  • redirect_url, a URL to which you redirect the customer. This isn't included in the response received if you didn't include the URL when initiating a payment
  • fraud_status, an indicator of whether the transaction is suspected to be legitimate or fraudulent
  • authorized_payment_method, the payment method selected by your customer for this purchase
JSON
{
"order_id": "3eaeb557-5e30-47f8-b840-b8d987f5945d",
"redirect_url": "https://payments.klarna.com/redirect/...",
"fraud_status": "ACCEPTED",
"authorized_payment_method": "invoice"
}

A success response to the order creation request for a one-time payment.

Send the customer browser to redirect_url provided in the response. Klarna places a cookie in the browser and redirects the customer back to the confirmation URL you provided when creating the session. This makes the checkout faster the next time the customer chooses to pay with Klarna.

Error response

If your request doesn't pass our validation, you'll receive an error response. The most common reasons why creating an order fails are:

  • placing the order more than 60 minutes after authorization
  • modifying purchase details after authorization without updating the payment session
JSON
{
"correlation_id": "6a9b1cb1-73a3-4936-a030-481ba4bb203b",
"error_code": "ERROR_CODE",
"error_messages": [
"ERROR_MESSAGE"
]
}

An error response to the order creation request for a one-time payment.

Here are examples of common errors with troubleshooting suggestions. You can use the value in correlation_id to find entries related to the request under Logs in the Merchant portal.

Error codeError messageDescription
NOT_FOUNDInvalid authorization tokenThe authorization token has expired because the order was placed more than 60 minutes after authorization. To fix the error, request a new authorization_token and use it to place the order.
BAD_VALUENot matching fields: [billing_address.postal_code]The data shared with Klarna in a previous step (create_session, load(), or authorize()) have been modified causing the validation to fail.
BAD_VALUENot matching fields: [Incorrect number of items in the cart. Expected: 2, Actual: 3]The order lines or the cart were updated after the authorize() call. Please ensure that the cart is kept as-is or send a new authorization request.
REJECTEDRejectedThis is an edge case reason, but can be triggered in case the merchant is configured with being allowed to update the cart. This could be updated from the authorize to the place order in such a way that a new authorize is triggered. In this case this is rejected.

Klarna Mobile SDK provides a full suite of mobile-first integrations, including Klarna products like:

Sign in with Klarna
On-site messaging
Express Checkout
Sign in with KlarnaOn-site MessagingExpress Checkout
  • On-site Messaging: Show contextual messaging let your customers know about the available payment options in pre-checkout: click here to learn more.
  • Sign in with Klarna: Seamlessly identify and let users login via their Klarna account: click here to learn more.
  • Express Checkout: Accelerate your checkout process and boost conversion by offering a one-click checkout, click here to learn more (Mobile SDK support available soon).

Complete your integration with