Updated 11 October 2021
In this blog, we are going to learn about how to implement Klarna payment gateway integration in android with SDK approach.
Before starting integration first, we need to know what a payment gateway is.
A payment gateway is a merchant service provided by an e-commerce application service provider. Which authorizes card or direct payment processing.
Klarna payment gateway provides quick and secure online transactions. If you are planing on the integration of the Klarna payment by using SDK then this blog will help you to do so.
Klarna payment gateway work on the below steps:
1 2 3 4 5 |
repositories { maven { url 'https://x.klarnacdn.net/mobile-sdk/' } } |
1 2 3 |
dependencies { implementation 'com.klarna.mobile:sdk:2.0.20' } |
SDK PaymentForm provides the UI interface for payment. So you have to create or initialize the view.
First, start with initializing the credentials like:
1 |
OrderClient.initializeCredentials("klarnaMerchantId", "sharedSecret", "yourProductionApiUrl") |
Now we have to create a view like below:
1 2 3 4 |
val klarnaPaymentView = KlarnaPaymentView(context = context , category = PAY_NOW , callback = this) mContentViewBinding.mainContainer.addView(klarnaPaymentView) |
After creating the view Initialize the payment gateway with client token(received from the backend session) as:
1 2 3 4 |
klarnaPaymentView.initialize( "clientToken", "returnUrl" ) |
It’s time to handle the payment callback. For that, you have to implement KlarnaPaymentViewCallback during the initialization like :
1 |
class CheckoutActivity : BaseActivity(), KlarnaPaymentViewCallback{ } |
For getting the result by SDK override the KlarnaPaymentViewCallback methods like:
Perform the actions after SDK initialization with the below method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
override fun onInitialized(view: KlarnaPaymentView) { view.load(null) } //view loading callback is handled with onLoaded() //enable the authorization after the payment view is loaded override fun onLoaded(view: KlarnaPaymentView) { klarnaPaymentView.authorize(true, null) } //Request authorization callback is handled with onAuthorized() //Once the request is authorized Finalize the request override fun onAuthorized( view: KlarnaPaymentView, approved: Boolean, authToken: String?, finalizedRequired: Boolean? ) { klarnaPaymentView.finalize(null) } //For re authorization handle the callback with onReauthorized() override fun onReauthorized(view: KlarnaPaymentView, approved: Boolean, authToken: String?) { klarnaPaymentView.finalize(null) } //check payment status with onFinalized() override fun onFinalized(view: KlarnaPaymentView, approved: Boolean, authToken: String?) { /*payment request is finalized with authToken and with approved value you can check for approval and dispapproval status */ } //Load the payment review with onLoadPaymentReview() override fun onLoadPaymentReview(view: KlarnaPaymentView, showForm: Boolean) { } //If any error occures in the above steps onErrorOccurred() will be called override fun onErrorOccurred(view: KlarnaPaymentView, error: KlarnaPaymentsSDKError) { //error occured with error.name and error.message } |
In this blog, you have learned about the implementation of the Klarna payment gateway integration by using SDK.
For more information regarding the Klarna payment gateway follow the link.
Thanks for reading this blog. You can also check other blogs from here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.