PrimerKlarnaSDK is is a wrapper of the KlarnaMobileSDK that exposes its functionality source. It can be used within the PrimerSDK as a separate module. Our goal is to provide binary universal framework as Swift Package and Cocoapod for further integraton into main Primer SDK.
- Framework:
- PrimerKlarnaSDK Xcode project. This project contains implementation of wrapper around KlarnaMobileSDK and creates framework.
- Scripts:
- prepare.sh - does some preparations related to removing previous xcframework
- archive.sh - does archivation of framework project for both iOS and Simulators
- build.sh - does creation of xcframework using archives from previous step
- finalize.sh - removes archives, removes previous zip with xcframeworks and creates new one
- make.sh - calls all previously described scripts in correct order
- KlarnaMobileSDK.xcframework - binary universal framework for KlarnaMobileSDK
- PrimerKlarnaSDK.xcframework - binary universal framework for PrimerKlarnaSDK
- XCFrameworks.zip - archive with xcframeworks (required for CocoaPod integration)
The framework contains implementation of wrapper classes and protocols for KlarnaMobileSDK, which is integrated inside it as a universal framework (xcframework).
If any error is occurring during payment process you will get it as an object of PrimerKlarnaError
. It implements PrimerKlarnaErrorProtocol
and contains following properties:
public protocol PrimerKlarnaErrorProtocol: CustomNSError, LocalizedError {
var errorId: String { get }
var exposedError: Error { get }
var info: [String: String]? { get }
var diagnosticsId: String { get }
}
Using the information it is possible to get more details about exact error, which occured and handle it in specific way.
The framework contains already predefined PrimerKlarnaViewController
, which contains continue button for controling payment process and KlarnaPaymentView
, which is UI component provided by Klarna and available as a part of KlarnaMobileSDK
. It is responsible for representation of different stages, which user need to pass through during payment process.
In order to initialize object of PrimerKlarnaViewController
you need to do next:
let viewController = PrimerKlarnaViewController(
delegate: self,
paymentCategory: .payNow,
clientToken: "CLIENT_TOKEN",
urlScheme: "URL_SCHEME"
)
As a mechanism for getting callbacks from PrimerKlarnaViewController
you can use PrimerKlarnaViewControllerDelegate
, which contains:
-
func primerKlarnaViewDidLoad()
- called whenKlarnaPaymentView
is loaded and available for user for further interactions. -
func primerKlarnaPaymentSessionCompleted(authorizationToken: String?, error: PrimerKlarnaError?)
- called when payment session completed either with error or successfully.
Next parameter, which should be passed is paymentCategory
. It should be case from the following enum:
public enum KlarnaPaymentCategory: String {
case payNow = "pay_now"
case payLater = "pay_later"
case payOverTime = "pay_over_time"
}
After this you need to pass client token, which is a token for Klarna payment session and can be retrieved from Primer back-end.
The last parameter, is urlScheme
and it is optional. It should be your apps custom URL scheme CFBundleURLSchemes
, which is required to return from external applications.
Another option for integration of Klarna payments, which is available within the framework, is PrimerKlarnaProvider
.
This approach should be used if you want to get full control over user interface and user experience during Klarna payment process and handle it inside your app.
PrimerKlarnaProvider
implement PrimerKlarnaProviding
protocol, which contains following functions and properties:
public protocol PrimerKlarnaProviding: AnyObject {
// MARK: - Properties
/**
Can be used for getting access to KlarnaPaymentView for representation it inside the app
*/
var paymentView: KlarnaPaymentView? { get }
// MARK: - Funcs
/**
Create KlarnaPaymentView using predefined payment category and setting up Klarna payment events listener
*/
func createPaymentView()
/**
Initializes payment view using client token and url schema if it is available
*/
func initializePaymentView()
/**
Gives ability to load payment review UI inside KlarnaPaymentView if it is required
*/
func loadPaymentReview()
/**
Loads payment view (json data can be added if it is required)
*/
func loadPaymentView(jsonData: String?)
/**
Removes payment view from superview and deinitializes it
*/
func removePaymentView()
/**
Authorizes payment session and gives ability to enable automatic finalization for payment
*/
func authorize(autoFinalize: Bool, jsonData: String?)
/**
Gives ability to reauthorize payment session if it is required
*/
func reauthorize(jsonData: String?)
/**
FInalizes payment session if it was not setup as the session to be automatically finalized on authorization stage
*/
func finalise(jsonData: String?)
}
In order to create instance of PrimerKlarnaProvider
you need to add following code:
let provider: PrimerKlarnaProviding = PrimerKlarnaProvider(
clientToken: "CLIENT_TOKEN",
paymentCategory: "PAYMENT_CATEGORY",
urlScheme: "URL_SCHEME",
delegate: self
)
Here client token should be fetched from Primer server along with list of payments categories. After user selects appropriate category you can create instance of PrimerKlarnaProvider
as an object of PrimerKlarnaProviding
protocol. URL schema is optional.
In order to get callbacks from payment session you need to implement PrimerKlarnaProviderDelegate
, which contains following functions:
-
func primerKlarnaWrapperInitialized()
- called whenKlarnaPaymentView
is initialized -
func primerKlarnaWrapperResized(to newHeight: CGFloat)
- called wheneverKlarnaPaymentView
changes its size and can be used for layout upates -
func primerKlarnaWrapperLoaded()
- called wheneverKlarnaPaymentView
is loaded -
func primerKlarnaWrapperReviewLoaded()
- called wheneverKlarnaPaymentView
loaded inreview
state -
func primerKlarnaWrapperAuthorized(approved: Bool, authToken: String?, finalizeRequired: Bool)
- called whenever payment session is authorized -
func primerKlarnaWrapperReauthorized(approved: Bool, authToken: String?)
- called whenever payment session is reauthorized -
func primerKlarnaWrapperFinalized(approved: Bool, authToken: String?)
- called whenever payment session is finalized -
func primerKlarnaWrapperFailed(with error: PrimerKlarnaError)
- calledif any error occurs during payment process
Steps:
-
Open Xcode project
Framework/PrimerKlarnaSDK.xcodeproj
-
If you need update dependencies and/or make changes in source code
-
Make new universal framework (xcframework). Run terminal and execute script:
cd Scripts
sh make.sh
As a result PrimerKlarnaSDK.xcframework
will be built and placed in root
folder. Also, the script will produce new XCFrameworks.zip
, which is required for CocoaPod.
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Alamofire into your Xcode project using CocoaPods, specify it in your Podfile
:
pod 'PrimerKlarnaSDK'
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
Once you have your Swift package set up, adding Alamofire as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/primer-io/primer-klarna-sdk-ios.git", .upToNextMajor(from: "1.0.4"))
]
Platform | Minimum Swift Version |
---|---|
iOS 14.0+ | 5.5 |
- KlarnaMobileSDK (included as xcframework) https://docs.klarna.com/mobile-sdk/ios/get-started/