Headless checkout with Hpp
This section details how to use Klarna HPP payment method in a Headless scenario with Litium.
Klarna HPP payment method can be used in a Litium headless commerce scenario, where an external CMS is using Litium as the commerce engine. The easiest way for this is to use the Litium Headless API addon as a start-up solution.
Following example shows how the ExecutePayment method is called in CheckoutService in Litium Headless API addon.
Note how the CheckoutFlowInfo object is initialized.
public AuthorizePaymentResult ExecutePayment(string externalOrderId, string paymentReferenceId, AuthorizePaymentArgs authorizePaymentArgs)
{
if (string.IsNullOrEmpty(externalOrderId))
throw new Exception("ExternalOrderID was null or empty. It is a required parameter");
if (string.IsNullOrEmpty(paymentReferenceId))
throw new Exception("paymentReferenceId is null or empty. It is a required parameter");
var order = ModuleECommerce.Instance.Orders[externalOrderId, _adminToken];
if (order == null)
throw new DoesNotExistException($"order {externalOrderId} not found.");
var payment = order.PaymentInfo.FirstOrDefault(x => x.ReferenceID == paymentReferenceId);
if (payment == null)
throw new DoesNotExistException($"Payment {paymentReferenceId} is not found in order {externalOrderId}.");
var result = new AuthorizePaymentResult();
CheckoutFlowInfo checkoutFlowInfo;
checkoutFlowInfo = new CheckoutFlowInfo()
{
ExecuteScript = (string scriptArgs, bool redirect) =>
{
result.RedirectUrl = scriptArgs;
result.Redirect = redirect;
},
CancelUrl = authorizePaymentArgs.CancelUrl,
ResponseUrl = authorizePaymentArgs.ResponseUrl,
ExecutePaymentMode = (authorizePaymentArgs.CaptureImmediately) ? ExecutePaymentMode.Charge : ExecutePaymentMode.Reserve
};
checkoutFlowInfo.SetValue("ClientTwoLetterISOLanguageName", authorizePaymentArgs.Culture);
checkoutFlowInfo.SetValue("ClientLanguage", authorizePaymentArgs.Culture);
checkoutFlowInfo.SetValue("PrivacyPolicyUrl", authorizePaymentArgs.PrivacyPolicyUrl);
checkoutFlowInfo.SetValue("TermsUrl", authorizePaymentArgs.TermsUrl);
var existingMessage = payment.ExternalMessage;
var paymentResult = payment.ExecutePayment(checkoutFlowInfo, _adminToken);
result.Success = paymentResult?.Success ?? false;
result.ExternalOrderId = payment.Order.ExternalOrderID;
result.PaymentReferenceId = payment.ReferenceID;
payment.PopulatePaymentInfoResult(result, existingMessage);
switch (paymentResult)
{
//..
//..Other payment providers
//..
case KlarnaPaymentResult klarnaPaymentResult:
HandleKlarnaPaymentResult(klarnaPaymentResult, result);
break;
default:
throw new NotImplementedException("Payment provider should implement handling headless results");
}
return result;
}
Setting server to server push notification url
The push notification Url is set by taking the domain name from serverCommunicationUrl attribute in the Litium.AddOns.Klarna.dll.config file. Set this value to the domain name you would want to receive server communication.
Following is an example for Litium.AddOns.Klarna.dll.config section which shows serverCommunicationUrl attribute.
<pluginSettings
traceMode="true"
serverCommunicationDelayInMinutes="2"
serverCommunicationUrl="https://yourdomain.com
>
Server communication is received at:
https://yourdomain.com/PaymentProviderResult.axd/Report/Klarna