private void ProcessRateRequest(CustomerInfo customer, ShipmentInfo shipment, Shipper shipper, Receiver receiver, RatingResponse response) { if (customer == null) { throw new Exception("Customer not found"); } if (shipment == null) { throw new Exception("Shipment not found"); } if (shipper == null) { throw new Exception("Shipper not found"); } if (receiver == null) { throw new Exception("Receiver not found"); } if (response == null) { throw new Exception("Response not found"); } xmlWriter.WriteStartElement("Customer", ""); AddElement("CustomerNumber", customer.CustomerNumber); AddElement("Password", customer.Password); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Shipment", ""); xmlWriter.WriteStartElement("Packages", ""); foreach (PackageInfo packageInfo in shipment.Packages) { xmlWriter.WriteStartElement("Package", ""); AddElement("Weight", packageInfo.Weight.ToString()); AddElement("Height", packageInfo.Height.ToString()); AddElement("Length", packageInfo.Length.ToString()); AddElement("Width", packageInfo.Width.ToString()); AddElement("HSCode", packageInfo.HSCode); AddElement("Amount", packageInfo.Amount.ToString()); AddElement("ProductValue", packageInfo.ProductValue.ToString()); xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); AddElement("DimensionUnit", string.IsNullOrEmpty(shipment.DimensionUnit.ToString()) ? "IN" : shipment.DimensionUnit.ToString()); AddElement("WeightUnit", string.IsNullOrEmpty(shipment.WeightUnit.ToString()) ? "LB" : shipment.WeightUnit.ToString()); if (shipment.DeclaredValue > 0) { AddElement("DeclaredValue", shipment.DeclaredValue.ToString()); } if (!string.IsNullOrEmpty(shipment.DeclaredValueCurrency)) { AddElement("DeclaredValueCurrency", shipment.DeclaredValueCurrency); } if (shipment.Insurance > 0) { AddElement("Insurance", shipment.Insurance.ToString()); } if (!string.IsNullOrEmpty(shipment.InsuranceCurrency)) { AddElement("InsuranceCurrency", shipment.InsuranceCurrency); } if (shipment.SpecialServices != null && shipment.SpecialServices.Count > 0) { foreach (string specialService in shipment.SpecialServices) { AddElement("SpecialServiceTypes", specialService); } } if (shipment.Accessorials != null && shipment.Accessorials.Count > 0) { foreach (string accessorials in shipment.Accessorials) { AddElement("Accessorials", accessorials); } } xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Shipper", ""); AddElement("Country", shipper.Country); AddElement("State", shipper.State); AddElement("City", shipper.City); AddElement("PostalCode", shipper.PostalCode); if (shipper.Address != null) { xmlWriter.WriteStartElement("Address", ""); foreach (string address in shipper.Address) { AddElement("A", address); } xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Receiver", ""); AddElement("Country", receiver.Country); AddElement("State", receiver.State); AddElement("City", receiver.City); AddElement("PostalCode", receiver.PostalCode); if (receiver.Address != null) { xmlWriter.WriteStartElement("Address", ""); foreach (string address in receiver.Address) { AddElement("A", address); } xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("Response", ""); xmlWriter.WriteStartElement("ErrorMessage", ""); AddElement("ErrorCode", response.ErrorMessage.ErrorCode); AddElement("ErrorMessage", response.ErrorMessage.ErrorMessage); xmlWriter.WriteEndElement(); AddElement("FreightCharge", response.FreightCharge.ToString()); AddElement("DutiesAndTaxes", response.DutiesAndTaxes.ToString()); AddElement("TotalCharges", response.TotalCharge.ToString()); xmlWriter.WriteEndElement(); }
public static bool ValidateRateRequest(CustomerInfo customer, ShipmentInfo shipment, Shipper shipper, Receiver receiver, RatingResponse response) { if (customer == null) { response.ErrorMessage.ErrorCode = "003"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_NO_CUSTOMER; return(false); } else { if (string.IsNullOrEmpty(customer.CustomerNumber)) { response.ErrorMessage.ErrorCode = "004"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_CUSTOMER_NUMBER_EMPTY; return(false); } if (string.IsNullOrEmpty(customer.Password)) { response.ErrorMessage.ErrorCode = "005"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_PASSWORD_EMPTY; return(false); } if (string.IsNullOrEmpty(receiver.Country)) { response.ErrorMessage.ErrorCode = "006"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_RECEIVER_COUNTRY_EMPTY; return(false); } else if ((receiver.Country == "US" || receiver.Country == "CA") && string.IsNullOrEmpty(receiver.State)) { response.ErrorMessage.ErrorCode = "007"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_RECEIVER_STATE_EMPTY; return(false); } if (string.IsNullOrEmpty(shipper.Country)) { response.ErrorMessage.ErrorCode = "008"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_SHIPPER_COUNTRY_EMPTY; return(false); } else if ((shipper.Country == "US" || shipper.Country == "CA") && string.IsNullOrEmpty(shipper.State)) { response.ErrorMessage.ErrorCode = "009"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_SHIPPER_STATE_EMPTY; return(false); } if (shipment.Packages == null || shipment.Packages.Count <= 0) { response.ErrorMessage.ErrorCode = "010"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_SHIPMENT_PACKAGE_UNDEFINED; return(false); } else { foreach (PackageInfo package in shipment.Packages) { if (package.Weight == 0 || package.Width == 0 || package.Height == 0 || package.Length == 0) { response.ErrorMessage.ErrorCode = "011"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_SHIPMENT_INVALID_PACKAGE_DIMENSION; return(false); } if (shipper.Country != receiver.Country && string.IsNullOrEmpty(package.HSCode)) { response.ErrorMessage.ErrorCode = "012"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_SHIPMENT_PRODUCTHSCODE_UNDEFINED; return(false); } if (shipper.Country != receiver.Country && package.ProductValue == 0) { response.ErrorMessage.ErrorCode = "013"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_SHIPMENT_PRODUCT_VALUE_UNDEFINED; return(false); } } } if (shipment.SpecialServices != null && shipment.SpecialServices.Count > 0) { foreach (string specialService in shipment.SpecialServices) { if (!Enum.IsDefined(typeof(SpecialService), specialService)) { response.ErrorMessage.ErrorCode = "014"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_SHIPMENT_INVALID_SPECIAL_SERVICE; return(false); } } } if (shipment.Accessorials != null && shipment.Accessorials.Count > 0) { foreach (string accessorial in shipment.Accessorials) { if (!Enum.IsDefined(typeof(Accessorial), accessorial)) { response.ErrorMessage.ErrorCode = "015"; response.ErrorMessage.ErrorMessage = CommonMessages.ERROR_SHIPMENT_INVALID_ACCESSORIAL; return(false); } } } return(true); } }
public string CompileRateRequestFile(CustomerInfo customer, ShipmentInfo shipment, Shipper shipper, Receiver receiver, RatingResponse response) { stream = new MemoryStream(); xmlWriter = new XmlTextWriter(stream, Encoding.UTF8); xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement("RateRequest"); ProcessRateRequest(customer, shipment, shipper, receiver, response); xmlWriter.WriteEndElement(); xmlWriter.WriteEndDocument(); xmlWriter.Flush(); xmlWriter.Close(); byte[] buffer = stream.ToArray(); return(Encoding.UTF8.GetString(buffer)); }