public void Get_InvalidIntlRateV2Request_RequestError() { IntlRateV2Response response = _rateService.Get(new IntlRateV2Request { UserId = "kjzsdjbh fgkjashdf jhasdf" }); Assert.That(response.Error, Is.InstanceOf <RequestError>()); }
public void Get_InternationalValueOfContentsOver1000_IntlRateV2Response() { string request = string.Format( "<IntlRateV2Request PASSWORD=\"{1}\" USERID=\"{0}\"><Revision>2</Revision><Package ID=\"2fcd64731e0ff32b\"><Pounds>30</Pounds><Ounces>0</Ounces><Machinable>false</Machinable><MailType>Package</MailType><ValueOfContents>1550.00</ValueOfContents><Country>Canada</Country><Container /><Size>REGULAR</Size><Width>1</Width><Length>1</Length><Height>1</Height><Girth>4</Girth><OriginZip>18507</OriginZip><ExtraServices><ExtraService>1</ExtraService></ExtraServices></Package></IntlRateV2Request>", _userId, _password); IntlRateV2Response response = _rateService.Get(request.ToObject <IntlRateV2Request>()); Assert.That(response, Is.InstanceOf(typeof(IntlRateV2Response))); }
public IActionResult Get() { var path = hostingEnvironment.WebRootPath; var xmlFile = Path.Combine(path, "responsetrim.xml"); var content = System.IO.File.ReadAllText(xmlFile); XmlSerializer deserializer = new XmlSerializer(typeof(IntlRateV2Response)); var ms = new MemoryStream(Encoding.UTF8.GetBytes(content)); IntlRateV2Response responseJson = (IntlRateV2Response)deserializer.Deserialize(ms); // JsonConvert.DeserializeObject<IntlRateV2Response>(content); return(Ok(responseJson)); }
public void Get_IntlRateV2Request_IntlRateV4Response() { IntlRateV2Response response = _rateService.Get(RateServiceTestsData.GetInternationalRequest()); Assert.That(response, Is.InstanceOf <IntlRateV2Response>()); }
private List <ShippingOption> InterpretShippingOptions(IntlRateV2Response response, decimal minimumShippingRate) { if (response == null) { throw new ArgumentNullException(nameof(response)); } if (response.Error != null) { return(new List <ShippingOption> { new ShippingOption { Name = response.Error.Description } }); } if (minimumShippingRate < 0) { throw new ArgumentOutOfRangeException(nameof(minimumShippingRate), minimumShippingRate, "minimumShippingRate must be greater than zero"); } string[] carrierServicesOffered = _uspsSettings.InternationalServicesSelected.Split(','); decimal additionalHandlingCharge = _uspsSettings.AdditionalHandlingCharge; var options = new List <ShippingOption>(); foreach (SeeSharpShip.Models.Usps.International.Response.Package package in response.Packages) { // indicate a package error if there is one and skip to the next package if (package.Error != null) { options.Add(new ShippingOption { Name = package.Error.Description }); continue; } foreach (Service service in package.Services) { // service doesn't match one that is enabled, move on to the next one if (!carrierServicesOffered.Contains(service.Id)) { continue; } string serviceName = GetModifiedServiceName(service.SvcDescription); ExtraService insurance = service.ExtraServices?.FirstOrDefault(s => s.ServiceId == "1"); decimal rate = service.Postage + (insurance?.Price ?? 0) + additionalHandlingCharge; ShippingOption shippingOption = options.Find(o => o.Name == serviceName); // Use min shipping amount if rate is less than minimum rate = rate < minimumShippingRate ? minimumShippingRate : rate; if (shippingOption == null) { // service doesn't exist yet, so create a new one shippingOption = new ShippingOption { Name = serviceName, Rate = rate, }; options.Add(shippingOption); } else { // service is already in the list, so let's add the current postage rate to it shippingOption.Rate += rate; } } } return(options); }
//[HttpGet("{country}")] public async Task <IActionResult> GetAsync([FromQuery] IntlRequestPackage model) { //return Ok(model); IntlRequestPackage package = new Models.IntlRateRequest.IntlRequestPackage { // AcceptanceDateTime = DateTime.UtcNow.AddDays(1), Country = model.Country, OriginZip = "90001", Ounces = 15.0m, PackageId = "1", ValueOfContents = "10.00", Container = "RECTANGULAR", Size = "REGULAR", Width = "15", Length = "15", Height = "16", Girth = "15", }; if (!string.IsNullOrEmpty(model.DestinationPostalCode)) { package.DestinationPostalCode = model.DestinationPostalCode; package.AcceptanceDateTime = DateTime.UtcNow.AddDays(1); } IntlRateV2Request intlRateV2Request = new IntlRateV2Request { Package = package }; XmlSerializer xsSubmit = new XmlSerializer(typeof(IntlRateV2Request)); var xml = ""; using (var sww = new StringWriter()) { using (XmlWriter writer = XmlWriter.Create(sww)) { xsSubmit.Serialize(writer, intlRateV2Request); xml = sww.ToString(); } } string uspsUrl = "http://production.shippingapis.com/ShippingAPI.dll"; var formData = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("API", "IntlRateV2"), new KeyValuePair <string, string>("XML", xml) }); HttpClient httpClient = new HttpClient(); var response = await httpClient.PostAsync(uspsUrl, formData); var content = await response.Content.ReadAsStringAsync(); var webRootPath = _hostingEnvironment.WebRootPath; var responseXmlFile = Path.Combine(webRootPath, "response.xml"); System.IO.File.WriteAllText(responseXmlFile, content); //XmlSerializer deserializer = new XmlSerializer(typeof(IntlRateV2Response)); //var ms = new MemoryStream(Encoding.UTF8.GetBytes(content)); //var responseJson = deserializer.Deserialize(ms); try { XmlSerializer deserializer = new XmlSerializer(typeof(IntlRateV2Response)); var ms = new MemoryStream(Encoding.UTF8.GetBytes(content)); IntlRateV2Response responseJson = (IntlRateV2Response)deserializer.Deserialize(ms); return(Ok(responseJson)); } catch (Exception ex) { XmlSerializer serializer = new XmlSerializer(typeof(Error)); var ms = new MemoryStream(Encoding.UTF8.GetBytes(content)); Error error = (Error)serializer.Deserialize(ms); return(NotFound(error)); } }