A major facelift for the Geta Klarna Checkout module

29.02.2016

This blog post is part one of several blog posts describing new features added to Geta's Klarna checkout module. The module is a library which helps to integrate Klarna Checkout as one of the payment options in your EPiServer Commerce site.

In this first blog post I'll go through how the module can be integrated on your checkout page. In the next upcoming blog post I will cover order management processes such as complete shipment with capture payment, cancel reservation and handling of returns.

What's new?

Here are some of the new features:

Upgraded to latest Klarna API version (v3.0)

Upgraded to EPiServer v9 and removed dependency on log4net

Added settings as Commerce provider settings

Added KlarnaCheckoutGateway in order to support standard commerce payment workflow

Added api support for order management

Capture on complete shipment (package is shipped)

Cancel reservation if no items shipped

Credit for handling returns

Initializable module that adds necessary commerce meta fields

IPostProcessPayment interface to support custom processing after ProcessPayment has completed

How to use it

Please note that in order to focus on the Klarna implementation common commerce parts, error handling and logging have been left out, see code comments for clearity.

[HttpGet] public ActionResult Index(CheckoutPage currentPage) { // This is where you do your "normal" checkout stuff like // initialize your model with shipment options, payment and cart items // Run CartValidateActivityFlow once to calculate all taxes, charges and get the correct total amounts }

The next step is the KlarnaCheckout action - this is where you have to call Klarna Checkout and provide all Klarna's cart items including shipping information, locale of Klarna Checkout, URL's of all endponts mentioned here. Klarna API will return HTML snippet which you have to render on your page where user will fill all required details for Klarna. Here is an example:

It's important to verify that the cart hasn't changed after the user pressed "go to payment", hence the call to IsPaymentValidForCart. If the cart has changed the user is sent back to Index view to verify the items in the cart before proceeding to payment once more.

In the RenameCartAndSaveChanges I am renaming the cart using the Klarna order id, so that I can load it later when I get the Push call from Klarna.

Push is called from Klarna when order is confirmed, but status not updated to created. In the sample code for the Push method I have left out the "heavy stuff" which is creating the order in commerce, i.e. converting the cart to a purchase order. The ConfirmResponse object will contain all the data you need, data such as billing address and more. It's important that the payment object contains meta data for klarna order id and reservation number (see code below). The reservation number will be used for capturing payment when the order is shipped.

The last Klarna endpoint, Terms, is needed to display terms of your site in Klarna Checkout. It can be some MVC view or even static HTML file.

About taxes

In order to calulate tax percent, the implementation will look for a meta field "VatPercent" (MetadataConstants.VatPercent) on each line item. Tax for each line item is not calculated by default, so you will have to set the meta field value in a suitable work flow activity. Please see commerce tax documentation and the following blog post for more information. The custom meta field is automatically added to LineItemEx class as part of Commerce initialization, see MetadataInitialization.cs

What's next?

When the order is created (payment is authorized), a reservation is made in Klarnas system. This reservation will be made into an invoice when the order is shipped. The module also includes a payment gateway for handeling order management processes such as complete shipment with capture payment, cancel reservation and handling of returns. I will cover this in detail in an upcoming blog post.