Example #1
0
        public static void ToAddress(this OrderReferenceDetails details, cloudCommerce.Core.Domain.Common.Address address, ICountryService countryService,
                                     IStateProvinceService stateProvinceService, out bool countryAllowsShipping, out bool countryAllowsBilling)
        {
            countryAllowsShipping = countryAllowsBilling = true;

            if (details.IsSetBuyer() && details.Buyer.IsSetEmail())
            {
                address.Email = details.Buyer.Email;
            }

            if (details.IsSetDestination() && details.Destination.IsSetPhysicalDestination())
            {
                details.Destination.PhysicalDestination.ToAddress(address, countryService, stateProvinceService, out countryAllowsShipping, out countryAllowsBilling);
            }
        }
Example #2
0
        public bool FindAndApplyAddress(OrderReferenceDetails details, Customer customer, bool isShippable, bool forceToTakeAmazonAddress)
        {
            // PlaceOrder requires billing address but we don't get one from Amazon here. so use shipping address instead until we get it from amazon.
            bool countryAllowsShipping, countryAllowsBilling;

            var amazonAddress = new cloudCommerce.Core.Domain.Common.Address()
            {
                CreatedOnUtc = DateTime.UtcNow
            };

            details.ToAddress(amazonAddress, _countryService, _stateProvinceService, out countryAllowsShipping, out countryAllowsBilling);

            if (isShippable && !countryAllowsShipping)
            {
                return(false);
            }

            if (amazonAddress.Email.IsEmpty())
            {
                amazonAddress.Email = customer.Email;
            }

            if (forceToTakeAmazonAddress)
            {
                // first time to get in touch with an amazon address
                var existingAddress = customer.Addresses.ToList().FindAddress(amazonAddress, true);

                if (existingAddress == null)
                {
                    customer.Addresses.Add(amazonAddress);
                    customer.BillingAddress = amazonAddress;
                }
                else
                {
                    customer.BillingAddress = existingAddress;
                }
            }
            else
            {
                if (customer.BillingAddress == null)
                {
                    customer.Addresses.Add(amazonAddress);
                    customer.BillingAddress = amazonAddress;
                }

                // we already have the address but it is uncomplete, so just complete it
                details.ToAddress(customer.BillingAddress, _countryService, _stateProvinceService, out countryAllowsShipping, out countryAllowsBilling);

                // but now we could have dublicates
                int newAddressId = customer.BillingAddress.Id;
                var addresses    = customer.Addresses.Where(x => x.Id != newAddressId).ToList();

                var existingAddress = addresses.FindAddress(customer.BillingAddress, false);

                if (existingAddress != null)
                {
                    // remove the new and take the old one
                    customer.RemoveAddress(customer.BillingAddress);
                    customer.BillingAddress = existingAddress;

                    try
                    {
                        _addressService.DeleteAddress(newAddressId);
                    }
                    catch (Exception exc)
                    {
                        exc.Dump();
                    }
                }
            }

            customer.ShippingAddress = (isShippable ? customer.BillingAddress : null);

            return(true);
        }
Example #3
0
        public static void ToAddress(this Address amazonAddress, cloudCommerce.Core.Domain.Common.Address address, ICountryService countryService,
                                     IStateProvinceService stateProvinceService, out bool countryAllowsShipping, out bool countryAllowsBilling)
        {
            countryAllowsShipping = countryAllowsBilling = true;

            if (amazonAddress.IsSetName())
            {
                address.ToFirstAndLastName(amazonAddress.Name);
            }

            if (amazonAddress.IsSetAddressLine1())
            {
                address.Address1 = amazonAddress.AddressLine1.TrimSafe().Truncate(4000);
            }

            if (amazonAddress.IsSetAddressLine2())
            {
                address.Address2 = amazonAddress.AddressLine2.TrimSafe().Truncate(4000);
            }

            if (amazonAddress.IsSetAddressLine3())
            {
                address.Address2 = address.Address2.Grow(amazonAddress.AddressLine3.TrimSafe(), ", ").Truncate(4000);
            }

            // normalize
            if (address.Address1.IsEmpty() && address.Address2.HasValue())
            {
                address.Address1 = address.Address2;
                address.Address2 = null;
            }
            else if (address.Address1.HasValue() && address.Address1 == address.Address2)
            {
                address.Address2 = null;
            }

            if (amazonAddress.IsSetCity())
            {
                address.City = amazonAddress.City.TrimSafe().Truncate(4000);
            }

            if (amazonAddress.IsSetPostalCode())
            {
                address.ZipPostalCode = amazonAddress.PostalCode.TrimSafe().Truncate(4000);
            }

            if (amazonAddress.IsSetPhone())
            {
                address.PhoneNumber = amazonAddress.Phone.TrimSafe().Truncate(4000);
            }

            if (amazonAddress.IsSetCountryCode())
            {
                var country = countryService.GetCountryByTwoOrThreeLetterIsoCode(amazonAddress.CountryCode);

                if (country != null)
                {
                    address.CountryId     = country.Id;
                    countryAllowsShipping = country.AllowsShipping;
                    countryAllowsBilling  = country.AllowsBilling;
                }
            }

            if (amazonAddress.IsSetStateOrRegion())
            {
                var stateProvince = stateProvinceService.GetStateProvinceByAbbreviation(amazonAddress.StateOrRegion);

                if (stateProvince != null)
                {
                    address.StateProvinceId = stateProvince.Id;
                }
            }

            //amazonAddress.District, amazonAddress.County ??

            if (address.CountryId == 0)
            {
                address.CountryId = null;
            }

            if (address.StateProvinceId == 0)
            {
                address.StateProvinceId = null;
            }
        }