This section contains instructions for upgrading the Klarna add-on to v4.7.106
An upgrade to the Klarna add-on requires that both Litium.AddOns.Klarna and Litium.Studio.AddOns.Klarna are upgraded to 4.7.106 using NuGet
The Klarna KCO V3 in add-on version v4.7.106 is upgraded to use the Klarna API library Klarna.Rest.Core 3.1.9 version. This version is significantly different from the Klarna.Rest library, which results in braking changes in the Litium Accelerator code. They are handled as follows:
- Uninstall the package Klarna.Rest from the Accelerator.MVC project.
- Remove statements that are using Klarna.Rest.
- Use the CheckoutOrder object from the Klarna.Rest.Core library, instead of CheckoutOrderData from the old Klarna.Rest library.
- Use CheckoutOptions from the Klarna.Rest.Core library, instead of Options from the old Klarna.Rest library.
- LitiumKcoOrder no longer has the CheckoutOrderData object from Klarna.Rest library, but the CheckoutOrder object from the Klarna.Rest.Core library.
- Note that the settings file has changed in Litium.AddOns.Klarna.config, where the new attribute isKlarnaPaymentsHppEnabled has been added.
Code example for Litium 7
Changes in class KlarnaPaymentConfigV3:
1. Replace the method UpdateDataSentToKlarna:
public void UpdateDataSentToKlarna(UrlHelper urlHelper, OrderCarrier orderCarrier, KlarnaPaymentArgs paymentArgs, CheckoutOrder klarnaCheckoutOrder)
{
//if the project has specific data to be sent to Klarna, outside the order carrier,
//or has them as additional order info, and is not handled by the Klarna addOn, modify the klarnaCheckoutOrder parameter.
//it is the klarnaCheckoutOrder object that will be sent to Klarna checkout api at Klarna.
//the format of klarnaCheckoutOrder parameter is described in Klarna API documentation https://developers.klarna.com.
//Set the checkout options here.
klarnaCheckoutOrder.CheckoutOptions = new CheckoutOptions
{
AllowSeparateShippingAddress = true,
ColorButton = "#ff69b4",
DateOfBirthMandatory = true
};
//External payment methods should be configured for each Merchant account by Klarna before they are used.
AddCashOnDeliveryExternalPaymentMethod(urlHelper, orderCarrier, klarnaCheckoutOrder);
}
2. Replace the method AddCashOnDeliveryExternalPaymentMethod:
private void AddCashOnDeliveryExternalPaymentMethod(UrlHelper urlHelper, OrderCarrier orderCarrier, CheckoutOrder klarnaCheckoutOrder)
{
var checkoutPage = _websiteService.Get(orderCarrier.WebSiteID)?.Fields.GetValue<PointerPageItem>(AcceleratorWebsiteFieldNameConstants.CheckoutPage)?.EntitySystemId.MapTo<Page>();
var channel = _channelService.Get(orderCarrier.ChannelID);
if (checkoutPage == null || channel == null)
{
return;
}
var redirectUrl = urlHelper.Action(checkoutPage, routeValues: new { action = nameof(CheckoutController.PlaceOrderDirect) }, channel: channel);
var routeValues = new
{
PaymentProvider = "DirectPay",
PaymentMethod = "DirectPayment",
RedirectUrl = redirectUrl
};
var changePaymentProviderUrl = new Uri(urlHelper.Action("ChangePaymentMethod", "KlarnaPayment", routeValues, Uri.UriSchemeHttps)).AbsoluteUri;
var cashOnDeliveryExternalPayment = new PaymentProvider
{
Name = "Cash on delivery",
RedirectUrl = changePaymentProviderUrl,
Fee = 0
};
klarnaCheckoutOrder.ExternalPaymentMethods = new List<PaymentProvider>
{
cashOnDeliveryExternalPayment
};
}
3. Replace method ValidateCheckoutOrder:
public ValidationResult ValidateCheckoutOrder(ILitiumKcoOrder order)
{
var kcoOrder = order as LitiumKcoOrder;
if (!string.IsNullOrEmpty(kcoOrder?.CheckoutOrder?.CheckoutCustomer?.DateOfBirth))
{
AddOrUpdateAdditionalOrderInfo(order.OrderCarrier, "DateOfBirth", kcoOrder.CheckoutOrder.CheckoutCustomer.DateOfBirth);
}
var channel = _channelService.Get(order.OrderCarrier.ChannelID);
var language = channel.WebsiteLanguageSystemId.HasValue ? _languageService.Get(channel.WebsiteLanguageSystemId.Value) : null;
CultureInfo.CurrentUICulture = language != null ? language.CultureInfo : CultureInfo.CurrentUICulture;
var routeRequestLookupInfo = new RouteRequestLookupInfo()
{
Channel = channel,
IsSecureConnection = true,
};
_errorPageResolver.TryGet(routeRequestLookupInfo, out var routeRequestInfo);
// following result is used by Klarna AddOn to send the validation result back to Klarna.
// ReSharper disable once ConvertToLambdaExpression
return new ValidationResult
{
IsOrderValid = true,
RedirectToUrlOnValidationFailure = routeRequestInfo.DataPath,
};
}