Integration guide for how to integrate Klarna Checkout in your iOS app, using the hybrid integration.
This guide will teach you how to:
The components in the hybrid approach are:
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.
The communication between your mobile app, Klarna’s SDK, your servers and Klarna’s servers.
You are now ready to render Klarna Checkout in your mobile app. You should use a KCOKlarnaCheckout object and instantiate it with your current view controller and the webview:
Param | Type | Description |
---|---|---|
initWithViewController | UIViewController | The View Controller that will contain the checkout view |
returnURL | NSURL | URL schema as defined in your info.plist to return from external applications. Learn more about it here |
self.checkout = [[KCOKlarnaCheckout alloc] initWithViewController:self redirectURI:<YOUR-URL>];
[self.checkout attachWebView:self.webview];
The SDK will handle the checkout flow and return signals on certain events. The SDK will keep a weak reference of the view controller, and we will never override the webview delegate, so you can keep using it as normal.
From your webview, load the URL to the web page where you have integrated Klarna Checkout. The SDK will automatically run when it detects the Checkout snippet in the webview:
NSURL *checkoutURL = [NSURL URLWithString:@"https://www.example.store/checkout"];
[self.webView loadRequest:[NSURLRequest requestWithURL:checkoutURL]];
To handle the signals received from the SDK you should set up an observer listening to the KCOSignalNotification. To ensure the checkout shows the successful screen, you need to redirect your webview to the completion URL upon receiving the complete message:
- (void)handleNotification:(NSNotification *)notification
{
NSString *name = notification.userInfo[KCOSignalNameKey];
NSDictionary *data = notification.userInfo[KCOSignalDataKey];
if ([name isEqualToString:@"complete"]) {
if (uri && [uri isKindOfClass:[NSString class]] && uri.length > 0) {
NSURL *confirmationURL = [NSURL URLWithString:confirmationURI];
[self.webView loadRequest:[NSURLRequest requestWithURL:confirmationURL]];
}
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.
When a server-side call to Klarna is being processed, you can trigger the 'suspend' action to prevent the user/customer from altering the state of their Checkout order. The suspend action will turn the Klarna Checkout unresponsive to user interactions.
[self.checkout suspend];
After the server-side call to Klarna has been processed, you should trigger the 'resume' action to once again let the user interact with Klarna Checkout.
[self.checkout resume];
If you wish to handle external payment methods (such as PayPal, Google Pay, etc), you can turn on the following feature flag in the SDK. By doing so, the Klarna Checkout will not handle the external payment methods automatically:
self.checkout.merchantHandlesEpm = YES;
Note: This flag should be set before starting the Checkout process.
Whenever the SDK reaches an external payment method selected by the customer, you will receive an event named 'external' in your callback with a 'uri' parameter pointing to the external payment’s URL:
- (void)handleNotification:(NSNotification *)notification
{
NSString *name = notification.userInfo[KCOSignalNameKey];
NSDictionary *data = notification.userInfo[KCOSignalDataKey];
if ([name isEqualToString:@"external"]) {
NSString *externalPaymentUrl = [data objectForKey:@"uri"];
[self openExternalPayment:externalPaymentUrl];
}
}
If you wish to handle validation errors and you don’t want Klarna Checkout to show a pop-up window showing the errors, you can turn on the following feature flag in the SDK:
self.checkout.merchantHandlesValidationErrors = YES;
Note: This flag should be set before starting the Checkout process.
Whenever the SDK detects a validation error, you will receive an event named 'validation_error' in your callback with 'error_type' and 'error_text' parameters:
- (void)handleNotifications:(NSNotification *)notification {
NSString *name = notification.userInfo[KCOSignalNameKey];
NSDictionary *data = notification.userInfo[KCOSignalDataKey];
if ([name isEqualToString:@"validation_error"]) {
NSString *errorType = [data objectForKey:@"error_type"];
NSString *errorText = [data objectForKey:@"error_text"];
[self handleValidationError:errorType errorText:errorText];
}
}
Some payment methods require authorization through third-party applications. These can return to your application upon completion, but to do that, you need to supply a URL that should be used for returning. There do not need to be any special handlers on application load for that URL. Our only requirement is that the user is returned to your application from the third-party application.
In cases where the user needs to authenticate with their bank for credit card payments, the bank itself might open a third-party app such as Bank ID. Since the SDK does not create these sessions, the user would have to return to the app manually, and then you will get the completion signal from the checkout.
Success! You now have Klarna Checkout up and running in your mobile app.
Check out our git-hub page where you can