public void Acknowledge(global::Nop.Core.Domain.Orders.Order order)
        {
            try
            {
                var entity = _klarnaRepository.Table.First(x => x.OrderGuid == order.OrderGuid);
                var resourceUri = new Uri(entity.KlarnaResourceUri);
                var apiOrder = Fetch(resourceUri);
                var klarnaOrder = KlarnaCheckoutOrder.FromApiOrder(apiOrder);

                if (klarnaOrder.Status == KlarnaCheckoutOrder.StatusCheckoutComplete)
                {
                    var updateData = new KlarnaCheckoutOrder
                    {
                        Status = KlarnaCheckoutOrder.StatusCreated,
                        MerchantReference = new MerchantReference
                        {
                            OrderId1 = order.Id.ToString(CultureInfo.InvariantCulture),
                            OrderId2 = order.OrderGuid.ToString()
                        }
                    };

                    var dictData = updateData.ToDictionary();

                    apiOrder.Update(dictData);

                    order.AuthorizationTransactionId = klarnaOrder.Reservation;
                    _orderService.UpdateOrder(order);
                }
            }
            catch (Exception ex)
            {
                throw new KlarnaCheckoutException("Error acknowledging Klarna order. Order Id: " + order.Id, ex);
            }
        }
        public Uri Create()
        {
            var cart = _klarnaCheckoutUtils.GetCart();
            var merchant = _klarnaCheckoutUtils.GetMerchant();
            var supportedLocale = _klarnaCheckoutUtils.GetSupportedLocale();
            var gui = _klarnaCheckoutUtils.GetGui();
            var options = _klarnaCheckoutUtils.GetOptions();
            var shippingAddress = _klarnaCheckoutUtils.GetShippingAddress();

            var klarnaOrder = new KlarnaCheckoutOrder
            {
                Cart = cart,
                Merchant = merchant,
                Gui = gui,
                Options = options,
                ShippingAddress = shippingAddress,
                Locale = supportedLocale.Locale,
                PurchaseCountry = supportedLocale.PurchaseCountry,
                PurchaseCurrency = supportedLocale.PurchaseCurrency
            };

            var dictData = klarnaOrder.ToDictionary();
            var connector = Connector.Create(_klarnaSettings.SharedSecret, BaseUri);
            var order = new Klarna.Checkout.Order(connector);

            order.Create(dictData);

            var location = order.Location;

            var kcoOrderRequest = GetKcoOrderRequest(_workContext.CurrentCustomer, location);
            _klarnaRepository.Insert(kcoOrderRequest);

            return location;
        }
        public void SyncBillingAndShippingAddress(global::Nop.Core.Domain.Customers.Customer customer, KlarnaCheckoutOrder klarnaCheckoutOrder)
        {
            try
            {
                var billingAddress = klarnaCheckoutOrder.BillingAddress;
                var shippingAddress = klarnaCheckoutOrder.ShippingAddress;

                var nopBillingAddress = customer.Addresses.FirstOrDefault(billingAddress.RepresentsAddress);
                if (nopBillingAddress == null)
                {
                    nopBillingAddress = new global::Nop.Core.Domain.Common.Address { CreatedOnUtc = DateTime.UtcNow };
                    customer.Addresses.Add(nopBillingAddress);
                }

                customer.BillingAddress = nopBillingAddress;
                billingAddress.CopyTo(nopBillingAddress);

                var nopShippingAddress = customer.Addresses.FirstOrDefault(shippingAddress.RepresentsAddress);
                if (nopShippingAddress == null)
                {
                    nopShippingAddress = new global::Nop.Core.Domain.Common.Address { CreatedOnUtc = DateTime.UtcNow };
                    customer.Addresses.Add(nopShippingAddress);
                }

                customer.ShippingAddress = nopShippingAddress;
                shippingAddress.CopyTo(nopShippingAddress);

                _customerService.UpdateCustomer(customer);
            }
            catch (Exception ex)
            {
                var billing = JsonConvert.SerializeObject(klarnaCheckoutOrder.BillingAddress);
                var shipping = JsonConvert.SerializeObject(klarnaCheckoutOrder.ShippingAddress);
                throw new KlarnaCheckoutException(string.Format(CultureInfo.CurrentCulture, "Error syncing addresses. Billing: {0}, Shipping: {1}", billing, shipping), ex);
            }
        }
        public bool Update(Uri resourceUri)
        {
            try
            {
                var klarnaOrderId = _klarnaCheckoutUtils.GetOrderIdFromUri(resourceUri);
                var cart = _klarnaCheckoutUtils.GetCart();
                var options = _klarnaCheckoutUtils.GetOptions();
                var connector = Connector.Create(_klarnaSettings.SharedSecret, BaseUri);
                var supportedLocale = _klarnaCheckoutUtils.GetSupportedLocale();

                var klarnaOrder = new KlarnaCheckoutOrder
                {
                    Cart = cart,
                    Options = options,
                    Locale = supportedLocale.Locale,
                    PurchaseCountry = supportedLocale.PurchaseCountry,
                    PurchaseCurrency = supportedLocale.PurchaseCurrency
                };

                var order = new Order(connector, klarnaOrderId);
                var dictData = klarnaOrder.ToDictionary();

                order.Update(dictData);

                return true;
            }
            catch (Exception ex)
            {
                var exceptionJson = JsonConvert.SerializeObject(ex.Data);

                _logger.Warning(string.Format(CultureInfo.CurrentCulture, "KlarnaCheckout: Error updating Klarna order. Will try to create a new one. ResourceURI: {0}, Data: {1}",
                    resourceUri, exceptionJson), exception: ex);
            }

            return false;
        }