private void SetCustomerDetails(CheckoutViewModel model)
        {
            var orderCarrier = _requestModelAccessor.RequestModel.Cart.OrderCarrier;

            orderCarrier.Comments = model.OrderNote;

            //add customer information.
            var customerAddress = new AddressCarrier();

            customerAddress.MapFrom(model.CustomerDetails);

            //change delivery address.
            var deliveryAddress = customerAddress;

            if (model.IsBusinessCustomer && model.SelectedCompanyAddressId.HasValue)
            {
                deliveryAddress.MapFrom(GetCompanyAddress(model.SelectedCompanyAddressId.Value));
                deliveryAddress.MobilePhone = model.CustomerDetails.PhoneNumber;
                //Set selected company address to CustomerInfo.Address
                _moduleECommerce.CheckoutFlow.AddOrEditCustomerInfo(orderCarrier, deliveryAddress, SecurityToken.CurrentSecurityToken.UserID, _personStorage.CurrentSelectedOrganization.SystemId, SecurityToken.CurrentSecurityToken);
                _checkoutState.CopyAddressValues(customerAddress, deliveryAddress);
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(model.AlternativeAddress?.FirstName) && !string.IsNullOrWhiteSpace(model.AlternativeAddress?.LastName) && !string.IsNullOrWhiteSpace(model.AlternativeAddress?.Address))
                {
                    _checkoutState.CopyDeliveryAddressValues(model.AlternativeAddress.MapTo <AddressCarrier>());
                }
                _checkoutState.CopyAddressValues(customerAddress);

                _moduleECommerce.CheckoutFlow.AddOrEditCustomerInfo(orderCarrier, customerAddress, SecurityToken.CurrentSecurityToken);
            }

            //update the payment info, with customer address.
            var paymentInfo = orderCarrier.PaymentInfo.FirstOrDefault(x => !x.CarrierState.IsMarkedForDeleting);

            if (paymentInfo != null)
            {
                _moduleECommerce.CheckoutFlow.AddPaymentInfo(orderCarrier, paymentInfo.PaymentProvider, paymentInfo.PaymentMethod, deliveryAddress, SecurityToken.CurrentSecurityToken);
            }

            _checkoutState.NeedToRegister            = model.SignUp;
            _checkoutState.AlternativeAddressEnabled = model.ShowAlternativeAddress;

            //obtain the existing delivery, and change the address.
            var deliveryCarrier = orderCarrier.Deliveries.FirstOrDefault(x => !x.CarrierState.IsMarkedForDeleting);

            if (deliveryCarrier != null)
            {
                //Add delivery with new address. In default checkoutflow this causes the delivery address to be changed in existing delivery.
                _moduleECommerce.CheckoutFlow.AddDelivery(orderCarrier,
                                                          deliveryCarrier.DeliveryMethodID, deliveryAddress,
                                                          SecurityToken.CurrentSecurityToken);
            }
        }
 private void SetAddress(KlarnaOnChangeViewModel args, AddressCarrier addressCarrier)
 {
     if (addressCarrier != null)
     {
         addressCarrier.FirstName = args.GivenName;
         addressCarrier.LastName  = args.FamilyName;
         addressCarrier.Email     = args.Email;
         addressCarrier.Country   = args.Country;
         addressCarrier.Zip       = args.PostalCode;
     }
 }
 private bool IsEquals(Address address1, AddressCarrier address2)
 {
     if (address2 == null || address1 == null)
     {
         return(false);
     }
     return(string.Equals(address1.Address1 ?? string.Empty, address2.Address1 ?? string.Empty, StringComparison.OrdinalIgnoreCase) &&
            string.Equals(address1.Address2 ?? string.Empty, address2.Address2 ?? string.Empty, StringComparison.OrdinalIgnoreCase) &&
            string.Equals(address1.CareOf ?? string.Empty, address2.CareOf ?? string.Empty, StringComparison.OrdinalIgnoreCase) &&
            string.Equals(address1.City ?? string.Empty, address2.City ?? string.Empty, StringComparison.OrdinalIgnoreCase) &&
            string.Equals(address1.State ?? string.Empty, address2.State ?? string.Empty, StringComparison.OrdinalIgnoreCase) &&
            string.Equals(address1.Country ?? string.Empty, address2.Country ?? string.Empty, StringComparison.OrdinalIgnoreCase) &&
            string.Equals(address1.ZipCode ?? string.Empty, address2.Zip ?? string.Empty, StringComparison.OrdinalIgnoreCase) &&
            string.Equals(address1.PhoneNumber ?? string.Empty, address2.MobilePhone ?? string.Empty, StringComparison.OrdinalIgnoreCase));
 }
 private void CopyAddressValues(AddressCarrier source, CustomerDetailsViewModel target)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     if (target == null)
     {
         throw new ArgumentNullException(nameof(target));
     }
     target.Email       = source.Email ?? target.Email;
     target.PhoneNumber = source.MobilePhone ?? target.PhoneNumber;
     target.FirstName   = source.FirstName ?? target.FirstName;
     target.LastName    = source.LastName ?? target.LastName;
     target.Email       = source.Email ?? target.Email;
     target.Address     = source.Address1;
     target.CareOf      = source.CareOf;
     target.City        = source.City;
     target.Country     = source.Country;
     target.ZipCode     = source.Zip;
 }
        private void UpdatePersonInformation(Person person, CheckoutViewModel model)
        {
            var orderCarrier    = _requestModelAccessor.RequestModel.Cart.OrderCarrier;
            var addressCarrier  = orderCarrier.CustomerInfo.Address;
            var deliveryCarrier = orderCarrier.Deliveries.FirstOrDefault();

            AddressCarrier deliveryAddressCarrier = null;

            if (deliveryCarrier != null)
            {
                deliveryAddressCarrier = deliveryCarrier.Address;
            }

            var isUpdated = false;

            if (addressCarrier != null)
            {
                var firstName = person.FirstName ?? string.Empty;
                var lastName  = person.LastName ?? string.Empty;
                var email     = person.Email ?? string.Empty;
                var phone     = person.Phone ?? string.Empty;
                // Check if person information was changed
                if (!firstName.Equals(addressCarrier.FirstName, StringComparison.OrdinalIgnoreCase) ||
                    !lastName.Equals(addressCarrier.LastName, StringComparison.OrdinalIgnoreCase) ||
                    !email.Equals(addressCarrier.Email, StringComparison.OrdinalIgnoreCase) ||
                    !phone.Equals(addressCarrier.MobilePhone, StringComparison.OrdinalIgnoreCase))
                {
                    person.FirstName = addressCarrier.FirstName;
                    person.LastName  = addressCarrier.LastName;
                    person.Email     = addressCarrier.Email;
                    person.Phone     = addressCarrier.MobilePhone;
                    isUpdated        = true;
                }

                //Update address for private person
                if (!model.IsBusinessCustomer)
                {
                    var addressType = _addressTypeService.Get(AddressTypeNameConstants.Address);
                    var address     = person.Addresses.FirstOrDefault(x => x.AddressTypeSystemId == addressType.SystemId);
                    if (address == null)
                    {
                        address = new Address(addressType.SystemId);
                        person.Addresses.Add(address);
                    }

                    // Check if the user address and the address given in the order are the same. If they are different update the user address
                    if (!IsEquals(address, addressCarrier))
                    {
                        address.Address1    = addressCarrier.Address1;
                        address.CareOf      = addressCarrier.CareOf;
                        address.ZipCode     = addressCarrier.Zip;
                        address.City        = addressCarrier.City;
                        address.Country     = addressCarrier.Country;
                        address.PhoneNumber = addressCarrier.MobilePhone;
                        isUpdated           = true;
                    }
                }
            }

            //Update address for private person
            if (!model.IsBusinessCustomer)
            {
                var alternativeAddressType = _addressTypeService.Get(AddressTypeNameConstants.AlternativeAddress);
                var deliveryAddress        = person.Addresses.FirstOrDefault(x => x.AddressTypeSystemId == alternativeAddressType.SystemId);
                // Check if the user delivery addres and the given delivery address in the order are the same. If they are different update/create the user delivery address
                if (deliveryAddressCarrier != null && !IsEquals(deliveryAddress, deliveryAddressCarrier))
                {
                    if (deliveryAddress != null)
                    {
                        deliveryAddress.Address1    = deliveryAddressCarrier.Address1;
                        deliveryAddress.CareOf      = deliveryAddressCarrier.CareOf;
                        deliveryAddress.ZipCode     = deliveryAddressCarrier.Zip;
                        deliveryAddress.City        = deliveryAddressCarrier.City;
                        deliveryAddress.Country     = deliveryAddressCarrier.Country;
                        deliveryAddress.PhoneNumber = deliveryAddressCarrier.MobilePhone;
                    }
                    else
                    {
                        person.Addresses.Add(new Address(alternativeAddressType.SystemId)
                        {
                            Address1    = deliveryAddressCarrier.Address1,
                            Address2    = deliveryAddressCarrier.Address2,
                            CareOf      = deliveryAddressCarrier.CareOf,
                            ZipCode     = deliveryAddressCarrier.Zip,
                            City        = deliveryAddressCarrier.City,
                            Country     = deliveryAddressCarrier.Country,
                            State       = deliveryAddressCarrier.State,
                            PhoneNumber = deliveryAddressCarrier.MobilePhone
                        });
                    }

                    isUpdated = true;
                }
            }

            if (isUpdated)
            {
                using (_securityContextService.ActAsSystem())
                {
                    _personService.Update(person);
                }
            }
        }