From 82d09f0f2243c493959f133fc7846d9c72c377dc Mon Sep 17 00:00:00 2001 From: maximelaboissonniere Date: Tue, 7 Feb 2017 16:50:37 +0000 Subject: [PATCH] Added logic to support retail rates --- DotNetShipping.Tests/Features/UPSRates.cs | 38 ++++++++++++++++--- .../ShippingProviders/UPSProvider.cs | 22 +++++++++-- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/DotNetShipping.Tests/Features/UPSRates.cs b/DotNetShipping.Tests/Features/UPSRates.cs index 324225b..871d77f 100644 --- a/DotNetShipping.Tests/Features/UPSRates.cs +++ b/DotNetShipping.Tests/Features/UPSRates.cs @@ -43,7 +43,7 @@ public void UPS_Domestic_Returns_Rates_When_Using_International_Addresses_For_Si var response = rateManager.GetRates(DomesticAddress1, InternationalAddress1, Package1); - Debug.WriteLine(string.Format("Rates returned: {0}", response.Rates.Any() ? response.Rates.Count.ToString() : "0")); + Debug.WriteLine($"Rates returned: {(response.Rates.Any() ? response.Rates.Count.ToString() : "0")}"); Assert.NotNull(response); Assert.NotEmpty(response.Rates); @@ -66,7 +66,7 @@ public void UPS_Returns_Multiple_Rates_When_Using_Valid_Addresses_For_All_Servic var response = rateManager.GetRates(DomesticAddress1, DomesticAddress2, Package1); - Debug.WriteLine(string.Format("Rates returned: {0}", response.Rates.Any() ? response.Rates.Count.ToString() : "0")); + Debug.WriteLine($"Rates returned: {(response.Rates.Any() ? response.Rates.Count.ToString() : "0")}"); Assert.NotNull(response); Assert.NotEmpty(response.Rates); @@ -89,7 +89,7 @@ public void UPS_Returns_Multiple_Rates_When_Using_Valid_Addresses_For_All_Servic var response = rateManager.GetRates(DomesticAddress1, DomesticAddress2, Package1); - Debug.WriteLine(string.Format("Rates returned: {0}", response.Rates.Any() ? response.Rates.Count.ToString() : "0")); + Debug.WriteLine($"Rates returned: {(response.Rates.Any() ? response.Rates.Count.ToString() : "0")}"); Assert.NotNull(response); Assert.NotEmpty(response.Rates); @@ -112,7 +112,33 @@ public void UPS_Returns_Rates_When_Using_International_Origin_And_Destination_Ad var response = rateManager.GetRates(InternationalAddress2, InternationalAddress1, Package1); - Debug.WriteLine(string.Format("Rates returned: {0}", response.Rates.Any() ? response.Rates.Count.ToString() : "0")); + Debug.WriteLine($"Rates returned: {(response.Rates.Any() ? response.Rates.Count.ToString() : "0")}"); + + Assert.NotNull(response); + Assert.NotEmpty(response.Rates); + Assert.Empty(response.ServerErrors); + + foreach (var rate in response.Rates) + { + Assert.NotNull(rate); + Assert.True(rate.TotalCharges > 0); + + Debug.WriteLine(rate.Name + ": " + rate.TotalCharges); + } + } + + [Fact] + public void UPS_Returns_Rates_When_Using_International_Destination_Addresses_And_RetailRates_For_All_Services() + { + var rateManager = new RateManager(); + var provider = new UPSProvider(UPSLicenseNumber, UPSUserId, UPSPassword); + provider.UseRetailRates = true; + + rateManager.AddProvider(provider); + + var response = rateManager.GetRates(DomesticAddress1, InternationalAddress1, Package1); + + Debug.WriteLine($"Rates returned: {(response.Rates.Any() ? response.Rates.Count.ToString() : "0")}"); Assert.NotNull(response); Assert.NotEmpty(response.Rates); @@ -135,7 +161,7 @@ public void UPS_Returns_Rates_When_Using_International_Destination_Addresses_For var response = rateManager.GetRates(DomesticAddress1, InternationalAddress1, Package1); - Debug.WriteLine(string.Format("Rates returned: {0}", response.Rates.Any() ? response.Rates.Count.ToString() : "0")); + Debug.WriteLine($"Rates returned: {(response.Rates.Any() ? response.Rates.Count.ToString() : "0")}"); Assert.NotNull(response); Assert.NotEmpty(response.Rates); @@ -158,7 +184,7 @@ public void UPS_Returns_Single_Rate_When_Using_Domestic_Addresses_For_Single_Ser var response = rateManager.GetRates(DomesticAddress1, DomesticAddress2, Package1); - Debug.WriteLine(string.Format("Rates returned: {0}", response.Rates.Any() ? response.Rates.Count.ToString() : "0")); + Debug.WriteLine($"Rates returned: {(response.Rates.Any() ? response.Rates.Count.ToString() : "0")}"); Assert.NotNull(response); Assert.NotEmpty(response.Rates); diff --git a/DotNetShipping/ShippingProviders/UPSProvider.cs b/DotNetShipping/ShippingProviders/UPSProvider.cs index a834b6c..dde6e00 100644 --- a/DotNetShipping/ShippingProviders/UPSProvider.cs +++ b/DotNetShipping/ShippingProviders/UPSProvider.cs @@ -40,6 +40,7 @@ public enum AvailableServices private const string PRODUCTION_RATES_URL = "https://onlinetools.ups.com/ups.app/xml/Rate"; private AvailableServices _services = AvailableServices.All; private bool _useNegotiatedRates = false; + private bool _useRetailRates = false; private bool _useProduction = true; private readonly string _licenseNumber; private readonly string _password; @@ -122,9 +123,13 @@ public AvailableServices Services get { return _services; } set { _services = value; } } - private string RatesUrl + + private string RatesUrl => UseProduction ? PRODUCTION_RATES_URL : DEVELOPMENT_RATES_URL; + + public bool UseRetailRates { - get { return UseProduction ? PRODUCTION_RATES_URL : DEVELOPMENT_RATES_URL; } + get { return _useRetailRates; } + set { _useRetailRates = value; } } public bool UseNegotiatedRates @@ -165,7 +170,16 @@ private byte[] BuildRatesRequestMessage() writer.WriteElementString("Code", "03"); writer.WriteEndElement(); // writer.WriteStartElement("CustomerClassification"); - writer.WriteElementString("Code", string.IsNullOrWhiteSpace(_shipperNumber) ? "01" : "00"); // 00 gets shipper number rates, 01 for daily rates + + if (_useRetailRates) + { + writer.WriteElementString("Code", "04"); //04 gets retail rates + } + else + { + writer.WriteElementString("Code", string.IsNullOrWhiteSpace(_shipperNumber) ? "01" : "00"); // 00 gets shipper number rates, 01 for daily rates + } + writer.WriteEndElement(); // - /// + /// /// /// public IDictionary GetServiceCodes()