Ejemplo n.º 1
0
        private CheckoutOrderData GetCheckoutOrderData(
            ICart cart, IMarket market, PaymentMethodDto paymentMethodDto)
        {
            var totals        = _orderGroupCalculator.GetOrderGroupTotals(cart);
            var shipment      = cart.GetFirstShipment();
            var marketCountry = CountryCodeHelper.GetTwoLetterCountryCode(market.Countries.FirstOrDefault());

            if (string.IsNullOrWhiteSpace(marketCountry))
            {
                throw new ConfigurationException($"Please select a country in CM for market {cart.MarketId}");
            }
            var checkoutConfiguration = GetCheckoutConfiguration(market);

            var orderData = new PatchedCheckoutOrderData
            {
                PurchaseCountry  = marketCountry,
                PurchaseCurrency = cart.Currency.CurrencyCode,
                Locale           = ContentLanguage.PreferredCulture.Name,
                // Non-negative, minor units. Total amount of the order, including tax and any discounts.
                OrderAmount = AmountHelper.GetAmount(totals.Total),
                // Non-negative, minor units. The total tax amount of the order.
                OrderTaxAmount = AmountHelper.GetAmount(totals.TaxTotal),
                MerchantUrls   = GetMerchantUrls(cart),
                OrderLines     = GetOrderLines(cart, totals, checkoutConfiguration.SendProductAndImageUrl)
            };

            if (checkoutConfiguration.SendShippingCountries)
            {
                orderData.ShippingCountries = GetCountries().ToList();
            }

            // KCO_6 Setting to let the user select shipping options in the iframe
            if (checkoutConfiguration.SendShippingOptionsPriorAddresses)
            {
                if (checkoutConfiguration.ShippingOptionsInIFrame)
                {
                    orderData.ShippingOptions = GetShippingOptions(cart, cart.Currency, ContentLanguage.PreferredCulture);
                }
                else
                {
                    if (shipment != null)
                    {
                        orderData.SelectedShippingOption = ShippingManager.GetShippingMethod(shipment.ShippingMethodId)
                                                           ?.ShippingMethod?.FirstOrDefault()
                                                           ?.ToShippingOption();
                    }
                }
            }

            if (paymentMethodDto != null)
            {
                orderData.Options = GetOptions(cart.MarketId);
            }
            return(orderData);
        }
Ejemplo n.º 2
0
 public bool Compare(PatchedCheckoutOrderData checkoutData, PatchedCheckoutOrderData otherCheckoutOrderData)
 {
     return(checkoutData.OrderAmount.Equals(otherCheckoutOrderData.OrderAmount) &&
            checkoutData.OrderTaxAmount.Equals(otherCheckoutOrderData.OrderTaxAmount) &&
            checkoutData.ShippingAddress != null &&
            otherCheckoutOrderData.ShippingAddress != null &&
            checkoutData.ShippingAddress.PostalCode.Equals(otherCheckoutOrderData.ShippingAddress.PostalCode, StringComparison.InvariantCultureIgnoreCase) &&
            (checkoutData.ShippingAddress.Region == null && otherCheckoutOrderData.ShippingAddress.Region == null ||
             checkoutData.ShippingAddress.Region.Equals(otherCheckoutOrderData.ShippingAddress.Region, StringComparison.InvariantCultureIgnoreCase)) &&
            checkoutData.ShippingAddress.Country.Equals(otherCheckoutOrderData.ShippingAddress.Country, StringComparison.InvariantCultureIgnoreCase));
 }
Ejemplo n.º 3
0
        public bool ValidateOrder(ICart cart, PatchedCheckoutOrderData checkoutData)
        {
            // Compare the current cart state to the Klarna order state (totals, shipping selection, discounts, and gift cards). If they don't match there is an issue.
            var localCheckoutOrderData = (PatchedCheckoutOrderData)GetCheckoutOrderData(cart, PaymentMethodDto);

            localCheckoutOrderData.ShippingAddress = cart.GetFirstShipment().ShippingAddress.ToAddress();
            if (!_klarnaOrderValidator.Compare(checkoutData, localCheckoutOrderData))
            {
                return(false);
            }

            // Order is valid, create on hold cart in epi
            cart.Name = OrderStatus.OnHold.ToString();
            _orderRepository.Save(cart);

            // Create new default cart
            var newCart = _orderRepository.Create <ICart>(cart.CustomerId, "Default");

            _orderRepository.Save(newCart);

            return(true);
        }
Ejemplo n.º 4
0
        public IHttpActionResult OrderValidation(int orderGroupId, [FromBody] PatchedCheckoutOrderData checkoutData)
        {
            var cart = _orderRepository.Load <ICart>(orderGroupId);

            // Validate cart lineitems
            var validationIssues = _cartService.ValidateCart(cart);

            if (validationIssues.Any())
            {
                // check validation issues and redirect to a page to display the error
                var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.RedirectMethod);
                httpResponseMessage.Headers.Location = new Uri("http://klarna.geta.no/en/error-pages/checkout-something-went-wrong/");
                return(ResponseMessage(httpResponseMessage));
            }

            // Validate billing address if necessary (this is just an example)
            // To return an error like this you need require_validate_callback_success set to true
            if (checkoutData.BillingAddress.PostalCode.Equals("94108-2704"))
            {
                var errorResult = new ErrorResult
                {
                    ErrorType = ErrorType.address_error,
                    ErrorText = "Can't ship to postalcode 94108-2704"
                };
                return(ResponseMessage(Request.CreateResponse(HttpStatusCode.BadRequest, errorResult)));
            }

            // Validate order amount, shipping address
            if (!_klarnaCheckoutService.ValidateOrder(cart, checkoutData))
            {
                var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.RedirectMethod);
                httpResponseMessage.Headers.Location = new Uri("http://klarna.geta.no/en/error-pages/checkout-something-went-wrong/");
                return(ResponseMessage(httpResponseMessage));
            }

            return(Ok());
        }