private ILitiumKcoApi CreateKlarnaCheckoutApi(string merchantId)
 {
     return(KlarnaV2.StudioKlarnaCheckoutApi.CreateFrom(merchantId, _paymentConfigv2.UpdateDataSentToKlarna)
            ?? LitiumKcoApi.CreateFrom(
                merchantId,
                (order, payment, kcoOrder) => _paymentConfigv3.UpdateDataSentToKlarna(Url, order, payment, kcoOrder)));
 }
Esempio n. 2
0
            public PaymentWidgetResult GetWidget(OrderCarrier order, PaymentInfoCarrier paymentInfo)
            {
                var urlHelper        = new UrlHelper(HttpContext.Current.Request.RequestContext);
                var paymentAccountId = _paymentWidgetService.GetPaymentAccountId(paymentInfo.PaymentMethod);
                var klarnaCheckout   = LitiumKcoApi.CreateFrom(
                    paymentAccountId,
                    (orderCarrier, payment, kcoOrder) => _paymentConfig.UpdateDataSentToKlarna(urlHelper, orderCarrier, payment, kcoOrder));

                var checkoutOrder = string.IsNullOrEmpty(paymentInfo.TransactionNumber) ? null : klarnaCheckout.FetchKcoOrder(order);

                if (checkoutOrder == null || checkoutOrder.KlarnaOrderStatus == KlarnaOrderStatus.Incomplete)
                {
                    using (_distributedLockService.AcquireLock($"{nameof(PaymentWidgetService)}:{order.ID}", TimeSpan.FromMinutes(1)))
                    {
                        _paymentInfoCalculator.CalculateFromCarrier(order, _securityToken);
                        var args = CreatePaymentArgs(order, paymentAccountId);

                        checkoutOrder = klarnaCheckout.CreateOrUpdateKcoOrder(order, args);
                    }
                }

                switch (checkoutOrder.KlarnaOrderStatus)
                {
                case KlarnaOrderStatus.Incomplete:
                    return(new PaymentWidgetResult
                    {
                        Id = nameof(PaymentMethod.KlarnaCheckout),
                        IsChangeable = true,
                        ResponseString = checkoutOrder.HtmlSnippet,
                    });

                case KlarnaOrderStatus.Error:
                    throw new Exception(checkoutOrder.HtmlSnippet);

                case KlarnaOrderStatus.Authorized:
                case KlarnaOrderStatus.Cancelled:
                case KlarnaOrderStatus.Captured:
                case KlarnaOrderStatus.Complete:
                case KlarnaOrderStatus.Created:
                    _cartAccessor.Cart.Clear();
                    return(new PaymentWidgetResult
                    {
                        Id = nameof(PaymentMethod.KlarnaCheckout),
                        IsChangeable = false,
                        ResponseString = checkoutOrder.HtmlSnippet,
                    });
                }

                throw new Exception(checkoutOrder.HtmlSnippet);
            }