Integrate Klarna Payments Gateway SDK in iOS Application
Updated 1 July 2020
Save
Adding the SDK via Cocoapods
If you’re using Cocoapods, you can add the SDK by adding the dependency to your Podfile
1
pod"KlarnaMobileSDK"
Followed by performing:
1
pod install
Importing the SDK
Both the Klarna Payments native integration and the hybrid integration are available under the KlarnaMobileSDK module.
1
#import KlarnaMobileSDK
RENDERING A PAYMENT VIEW
A Payment View in iOS is called a KlarnaPaymentView. Rendering a payment view consists of creating an instance of the view, passing it an event listener and, adding it to your view hierarchy.
Creating the View
You can initialize the KlarnaPaymentView by providing a return URL and the event listener.
Param
Type
Description
category
String
The payment method category that should be rendered in the view.
eventListener
KlarnaPaymentEventListener
The listener that receives events during the payment process.
1
2
3
4
5
6
// Create the view
let paymentView=KlarnaPaymentView(category:"pay_over_time",eventListener:self)
// Add as subview paymentView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(paymentView)
// Create a height constraint that we'll update as its height changes.
The SDK will notify you of events via an event listener that you’ll need to implement. It acts as a conventional delegate on iOS, however, unlike a delegate, it’s not optional.
Handling Height Changes
The payment view does not define any height internally. This is because we want to allow you to handle height changes in whichever way is most convenient to you.
The payment view instead notifies you through the event listener you provide it.
Once you’ve initialized the view and you’re ready to display SDK of the KlarnaPaymentView.
Loading
Simply call load(), supplying an optional string with updated order data to update the session. This string should be formatted as a valid JSON.
Param
Type
Description
jsonData
String?
An optional string with the order data to update the session. Formatted as JSON.
1
klarnaPaymentView.load()
Result
If successful klarnaLoaded()will be called in your listener. If anything went wrong, klarnaFailed()it will be called.
And 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.
Param
Type
Description
paymentView
KlarnaPaymentView
The payment view that was loaded.
1
2
3
func klarnaLoaded(paymentView:KlarnaPaymentView){
// Content has finished loading and if you have any loader you could hide it here.
}
3. Authorizing the Session
When the payment view SDK 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.
Param
Type
Description
autoFinalize
Boolean?
An optional flag used to turn off auto-finalization for direct bank transfer.
jsonData
String?
An optional string to update the session. Formatted as JSON.
1
paymentView.authorize()
Result
If successful klarnaAuthrized() will be called in your listener. If not, klarnaFailed() it will be called.
Param
Type
Description
paymentView
KlarnaPaymentView
The payment view that was authorized.
approved
Bool
Whether the authorization was approved or not.
authToken
String?
If the session was authorized, the token will not be null.
finalizedRequired
Bool
Will be true if autoFinalize was false and this payment method needs a second confirmation step.
// user is not approved or might require finalization
}
iflet token=authToken{
// authorization is successful, backend may create order
}
iffinalizedRequired==true{
// app needs to call finalize()
}
}
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
Param
Type
Description
jsonData
String?
An optional string to update the session. Formatted as JSON
1
paymentView.reauthorize()
Result
If successful, klarnaReauthorized() will be called, and if not, klarnaFailed() will be called instead.
Param
Type
Description
paymentView
KlarnaPaymentView
The payment view that was reauthorized.
approved
Bool
Whether the reauthorization was approved or not.
authToken
String?
If the session was previously authorized, the token will not be null.
// user is not approved or might require finalization
}
iflet token=authToken{
// reauthorization is successful, backend may create order
}
}
5. 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 finalise() in order to get the token.
While we tend to name all finalize operations “finalize” with a “z”, all classes subclassing fromNSObjectalready have a conflicting finalize()method. In this specific case, we use alternative British-English spelling.
Param
Type
Description
sessionData
String?
An optional string to update the session. Formatted as JSON
1
paymentView.finalise()
Result
If successfulklarnaFinalized()will be called in your listener. If not successfulklarnaFailed()will be called.
Param
Type
Description
paymentView
KlarnaPaymentView
The payment view that was finalized.
approved
Bool
Whether the user was approved on finalizing or not.
authToken
String?
If the finalization went through, the token will not be null.
This will handle all the errors from the previous implementations. KlarnaPaymentError will contain the information needed to show to the user and also to handle all the states of the listener.
Param
Type
Description
paymentView
KlarnaPaymentView
The payment view that had the error.
error
KlarnaPaymentError?
The class that contains the information (name, message, isFatal and debugDescription) about the error
So please follow the above step to integrate Klarna Payment Gateway, and if you have any issue orsuggestion, you can leave your query/suggestion in the comment section.
If you want to know more about the klarna payment gateway please visit the below link:-
Be the first to comment.