Klarna: Headless Guide
Integrate Klarna on your website or mobile application and fully customize your checkout experience using your own UI.
Before you begin
This guide assumes that you know how to:
Accept payments with Klarna
Prepare the client session
Klarna requires the following data to process a payment successfully. Pass the following data in the client session, or in the payment request (for manual payment creation).
Parameter Name | Required | Description |
---|---|---|
currencyCode | ✓ | 3-letter currency code in ISO 4217 format, e.g. USD for US dollars |
order ↳ lineItems | ✓ | Details of the line items of the order |
order ↳ countryCode | ✓ | The country code of the user.Learn more about the supported countries and currencies by visiting Klarna Documentation. |
customer ↳ emailAddress | Customer’s email address. Pass this value to pre-fill the Klarna payment form. | |
customer ↳ mobileNumber | Customer’s mobile number (make sure it is the correct format for the country code). Pass this value to pre-fill the Klarna payment form. | |
customer ↳ shippingAddress | Customer’s shipping address. Pass this value to pre-fill the Klarna payment form. | |
customer ↳ billingAddress | Customer’s billing address. Pass this value to pre-fill the Klarna payment form. When passing customer.billingAddress in the client session, ensure that Klarna has payment method categories configured for the given address. If Klarna is not supported for the given billing address, no Klarna button will be rendered in the checkout | |
paymentMethod ↳ options ↳ KLARNA ↳ extraMerchantData | Extra Merchant Data (EMD) package supported by Klarna can be passed via the client session. Any JSON object that will be accepted by Klarna can be sent. Available schemas are documented here |
Prepare the SDK for payments
Handle payment method
Integration
The KlarnaPaymentMethodMethodManager is an SDK manager designed to streamline the headless integrations of the Klarna payment method that is comprised of an initial component/form for selecting a payment category and a redirect flow.
To integrate KlarnaPaymentMethodMethodManager
, follow these steps:
-
The PrimerHeadlessCheckout will help you configure the headless payment method and then start the payment process
-
When calling configure provide a callback function for
onAvailablePaymentMethodsLoaded
as described here -
For the Klarna payment method,configure a handler for the
KLARNA
payment method manager type and create a payment method manager as described here ` -
When the payment categories for the Klarna payment method have been retrieved,
onPaymentMethodCategoriesChange
callback will be called with a payload that will have a format as seen here -
You can use the payment categories retrieved through the
onPaymentMethodCategoriesChange
callback to create the custom component/form -
When the user has selected a payment category you should call the
renderCategory
function available in the manager. TherenderCategory
function requires, as parameters, the selected category id, a container id where to render the details about the payment category and anonHeightChange
callback that will be called when the payment category height has changed. You can use the height returned by theonHeightChange
callback to update the height of the container in which the payment category details will be displayed. -
After the user has selected a category and the details about this category have been displayed you can call the
start
function provided by the payment method manager created at step 3
Create the PrimerHeadlessCheckout in order to interact with the Primer API
You can follow the steps described here in order to configure the PrimerHeadlessCheckout.
Provide a custom handler for the KLARNA
payment method manager
As described here the onAvailablePaymentMethodsLoad
callback will be called with all available payment methods. Use this function and create a custom configuration for the KLARNA
payment method manager type and afterwards start the payment process.
Create the KlarnaPaymentMethodManager
Call the start function in order to start the flow of the component:
The onPaymentMethodCategoriesChange
callback received a payload as described here The options array can be used to configure a custom component/form.
Handle data collection
As mentioned previously, the component allows you to collect the payment categories, and to request payment finalization by calling the start
function from the payment method manager. You can use the result of the start function to handle any errors
Handle payment method
Integration
The KlarnaPaymentMethodMethodManager is an SDK manager designed to streamline the headless integrations of the Klarna payment method that is comprised of an initial component/form for selecting a payment category and a redirect flow.
To integrate KlarnaPaymentMethodMethodManager
, follow these steps:
-
The PrimerHeadlessCheckout will help you configure the headless payment method and then start the payment process
-
When calling configure provide a callback function for
onAvailablePaymentMethodsLoaded
as described here -
For the Klarna payment method,configure a handler for the
KLARNA
payment method manager type and create a payment method manager as described here ` -
When the payment categories for the Klarna payment method have been retrieved,
onPaymentMethodCategoriesChange
callback will be called with a payload that will have a format as seen here -
You can use the payment categories retrieved through the
onPaymentMethodCategoriesChange
callback to create the custom component/form -
When the user has selected a payment category you should call the
renderCategory
function available in the manager. TherenderCategory
function requires, as parameters, the selected category id, a container id where to render the details about the payment category and anonHeightChange
callback that will be called when the payment category height has changed. You can use the height returned by theonHeightChange
callback to update the height of the container in which the payment category details will be displayed. -
After the user has selected a category and the details about this category have been displayed you can call the
start
function provided by the payment method manager created at step 3
Create the PrimerHeadlessCheckout in order to interact with the Primer API
You can follow the steps described here in order to configure the PrimerHeadlessCheckout.
Provide a custom handler for the KLARNA
payment method manager
As described here the onAvailablePaymentMethodsLoad
callback will be called with all available payment methods. Use this function and create a custom configuration for the KLARNA
payment method manager type and afterwards start the payment process.
Create the KlarnaPaymentMethodManager
Call the start function in order to start the flow of the component:
The onPaymentMethodCategoriesChange
callback received a payload as described here The options array can be used to configure a custom component/form.
Handle data collection
As mentioned previously, the component allows you to collect the payment categories, and to request payment finalization by calling the start
function from the payment method manager. You can use the result of the start function to handle any errors
Pre-requisites
Some payment methods like Klarna Direct Debit (part of Klarna’s Pay Now), require manual enablement for the available markets (eg: DE, SE, NL). This is done by Klarna upon merchant request.
Install Primer Klarna SDK We support the Klarna In-App SDK (from version 2.7.0).
In order to be able to use Klarna you will have to install the PrimerKlarnaSDK
, currently available on CocoaPods.
CocoaPods
Add the line below in you Podfile
Configure Primer’s settings
Add a url scheme for your project and pass it in the urlScheme
of the paymentMethodOptions
of your settings
.
Handle payment method
Integration
[PrimerHeadlessUniversalCheckout.KlarnaManager](/sdk/ios/v2.x.x/primer-headless-checkout/klarna-headless-manager/init)
is an SDK manager designed to streamline the headless integration with Klarna. To integrate PrimerHeadlessUniversalCheckout.KlarnaManager
, follow these steps:
Create a new instance of KlarnaComponent
For the primer session intent, pass either PrimerSessionIntent.CHECKOUT
or PrimerSessionIntent.VAULT
, depending on the session type you’d like to create:
Listen for the emitted data
Conform to the stepDelegate in order to be notified about the state of the payment process:
Listen to the data validation statuse
It’s worth mentioning that the data validation is designed to operate in real-time. This means that the accuracy and completeness of the collected data can be confirmed even while users are actively inputting their information. This real-time validation feature ensures that your data remains accurate and complete throughout the data entry process, enhancing the user experience and the reliability of the collected data. Conform to the validationDelegate to handle any validation statuses that may occur.
Start the flow of the component
Call the start method in order to start the flow of the component:
Once called, the Klarna session will be created and the paymentSessionCreated
step will be emitted. As mentioned previously, the paymentSessionCreated
step contains information about the available Klarna categories. Use these categories to build a category selection user interface. Clicking on any such category should update the payment options via updateCollectedData, as described below.
Handle data collection
The component allows you to collect the payment options (category), and to request payment finalization.
Payment options should be set before submitting via the submit method:
Payment finalization should be requested after the payment is authorized and only if finalization is required, otherwise you’ll get validation errors. That is, when paymentSessionFinalizationRequired
is emitted as a step.
When the collected payment options are deemed valid, as determined by the validationDelegate, make the submit method call to initiate the payment authorization process. Calling submit will display the Klarna payment window. Successfully confirming the payment via this window will lead to the emission of the KlarnaStep.paymentSessionAuthorized
step.
Handle errors
You can conform to the errorDelegate to handle any errors that may occur during the payment flow:
The errorDelegate
is a protocol
that emits PrimerError
objects, allowing you to identify and respond to any errors that occur during the payment process.
Troubleshooting
Flaky BankId
The Klarna direct debit flow for Sweden will try to redirect the user to the Mobile BankID app. This action is flaky when in sandbox mode & can sometimes fail
Pre-requisites
Some payment methods like Klarna Direct Debit (part of Klarna’s Pay Now), require manual enablement for the available markets (eg: DE, SE, NL). This is done by Klarna upon merchant request.
Configuration
We support the Klarna In-App SDK (from version 2.7.0).
Create a PrimerKlarnaOptions
object, then initialise the SDK as usual.
ℹ️ Starting from version 1.0.3
onward, we are transitioning our SDK artifact distribution to Maven Central. This means you no longer need to reference the private Artifactory URL (PRIMER_ANDROID_ARTIFACTORY_URL) for future updates.
Please ensure that you remove any references to the Artifactory URL previously used for our SDK.
- Add the URL to Klarna’s CDN to your
gradle.properties
:
Finally, amend the dependencies section of your app’s build.gradle
to include the Primer’s klarna-android
library:
Handle payment method
Integration
PrimerHeadlessUniversalCheckoutKlarnaManager is an SDK manager designed to streamline the headless integration with Klarna. To integrate PrimerHeadlessUniversalCheckoutKlarnaManager
, follow these steps:
Create a new instance of KlarnaComponent
For the primer session intent, pass either PrimerSessionIntent.CHECKOUT
or PrimerSessionIntent.VAULT
, depending on the session type you’d like to create:
Listen for the emitted data Subscribe to the componentStep Flow in order to be notified about the state of the payment process:
Listen to the data validation statuses
This component allows you to collect the payment options (category and return URL), and to request payment finalization. It’s worth mentioning that the data validation is designed to operate in real-time. This means that the accuracy and completeness of the collected data can be confirmed even while users are actively inputting their information. This real-time validation feature ensures that your data remains accurate and complete throughout the data entry process, enhancing the user experience and the reliability of the collected data. Listen to the componentValidationStatus to handle any validation statuses that may occur.
Start the flow of the component Call the start function in order to start the flow of the component:
Once called, the Klarna session will be created and the PaymentSessionCreated
step will be emitted. As mentioned previously, the PaymentSessionCreated
step contains information about the available Klarna categories. Use these categories to build a category selection user interface. Clicking on any such category should update the payment options via updateCollectedData, as described below.
Handle data collection
As mentioned previously, the component allows you to collect the payment options (category and return URL), and to request payment finalization.
Create a deeplink activity and define it in your AndroidManifest.xml
with the appropriate intent filter:
Payment options should be set before submitting the component via the submit function:
Payment finalization should be requested after the payment is authorized and if and only if finalization is required, otherwise you’ll get validation errors. That is, when PaymentSessionAuthorized
is emitted as as step, and PaymentSessionAuthorized.isFinalized
returns false
:
When the collected payment options are deemed valid, as determined by the componentValidationStatus, call the submit function to initiate the payment authorization process. Calling submit will display the Klarna payment window. Successfully confirming the payment via this window will lead to the emission of the KlarnaPaymentStep.PaymentSessionAuthorized
step.
Handle errors
You can listen to the componentError to handle any errors that may occur during the payment flow:
The componentError
is a Flow
that emits PrimerError
objects, allowing you to identify and respond to any errors that occur during the payment process.
Troubleshooting
Flaky BankId
The Klarna direct debit flow for Sweden will try to redirect the user to the Mobile BankID app. This action is flaky when in sandbox mode & can sometimes fail
Pre-requisites
Some payment methods like Klarna Direct Debit (part of Klarna’s Pay Now), require manual enablement for the available markets (eg: DE, SE, NL). This is done by Klarna upon merchant request.
Handle payment method
Integration
KlarnaManager is an SDK manager designed to streamline the headless integration with Klarna. To integrate KlarnaManager
, follow these steps:
Create a new instance of KlarnaManagerProps
Create a new instance of KlarnaComponent
Listen for the emitted data
Make use of KlarnaManagerProps.onStep in order to be notified about the Klarna session state.
Rendering the Klarna payment view
As mentioned before, the Klarna payment view must be redender when receiving the paymentViewLoaded
step. This payment view contains information about the selected payment category, and it must be rendered before submit()
is called on the component.
Listen to the data validation statuses
This component allows you to collect the payment options (category and return URL (Android only)), and to request payment finalization. It’s worth mentioning that the data validation is designed to operate in real-time. This means that the accuracy and completeness of the collected data can be confirmed even while users are actively inputting their information. This real-time validation feature ensures that your data remains accurate and complete throughout the data entry process, enhancing the user experience and the reliability of the collected data. Use KlarnaManagerProps validation callbacks to handle any validation statuses that may occur.
Start the flow of the component Call the start function in order to start the flow of the component.
Once called, the Klarna session will be created and the PaymentSessionCreated
step will be emitted. As mentioned previously, the PaymentSessionCreated
step contains information about the available Klarna categories. Use these categories to build a category selection user interface. Clicking on any such category should update the payment options via onSetPaymentOptions, as described below.
Handle data collection
As mentioned previously, the component allows you to collect the payment options (category and return URL (Android only)), and to request payment finalization.
Update your Android module by creating a deeplink activity and defining it in your AndroidManifest.xml
with the appropriate intent filter:
Define your deeplink return activity implementation with the desired behavior, such as navigating to your app’s main activity:
Payment options should be set before submitting the component via the submit function:
Payment finalization should be requested after the payment is authorized and if and only if finalization is required, otherwise you’ll get validation errors. That is, when PaymentSessionAuthorized
is emitted as as step, and PaymentSessionAuthorized.isFinalized
returns false
:
When the collected payment options are deemed valid, as determined by a call to KlarnaManagerProps.onValid, call the submit will display the Klarna payment window. Successfully confirming the payment via this window will lead to the emission of the PaymentSessionAuthorized
step.
Handle errors
Make use of KlarnaManagerProps.onError to handle any errors that may occur during the payment flow:
The componentError
is a Flow
that emits PrimerError
objects, allowing you to identify and respond to any errors that occur during the payment process.
Troubleshooting
Flaky BankId
The Klarna direct debit flow for Sweden will try to redirect the user to the Mobile BankID app. This action is flaky when in sandbox mode & can sometimes fail
Vaulting
In order to vault Klarna accounts, pass the following data in the Client Session:
Parameter Name | Required | Description |
---|---|---|
customerId | ✓ | A unique identifier for your customer |
Prepare the SDK for vaulting
Klarna requires a Native UI Manager in order to be presented to the user.
Pass PrimerSessionItent.VAULT
for Klarna to be vaulted.
Klarna requires a Native UI Manager in order to be presented to the user.
Pass PrimerSessionItent.VAULT
for Klarna to be vaulted.
Test
Visit Klarna Sample Data Page for testing the integration against the Klarna Playground environment.
Go live
You don’t need to do anything particular to go live — just make sure to use production credentials.