using System; using System.Xml; using System.Data; using System.Collections.Generic; using System.Configuration; using System.Web; using System.IO; using System.Text; using System.Net; public class USPS : ShippingBase { private const string TestURL = "http://testing.shippingapis.com/ShippingAPITest.dll"; private const string ProductionURL = "http://production.shippingapis.com/ShippingAPI.dll"; internal class UPSRateRequest { private string _UserID = null; private string _Service = "ALL"; // defaults to all, cannot be changed private string _FirstClassMailType = null; private string _ZipOrigination = null; private string _ZipDestination = null; private int? _Pounds = null; private int? _Ounces = null; private string _Container = null; private string _Size = "REGULAR"; private string _Width = null; private string _Length = null; private string _Height = null; private string _Girth = null; private bool? _Machinable = null; public string UserID { get { return _UserID; } set { _UserID = value; } } public string Service { get { return _Service; } set { _Service = value; } } public string FirstClassMailType { get { return _FirstClassMailType; } set { _FirstClassMailType = value; } } public string ZipOrigination { get { return _ZipOrigination; } set { _ZipOrigination = value; } } public string ZipDestination { get { return _ZipDestination; } set { _ZipDestination = value; } } public int Pounds { get { return (_Pounds ?? 0); } set { _Pounds = value; } } public int Ounces { get { return (_Ounces ?? 0); } set { _Ounces = value; } } public string Container { get { return _Container; } set { _Container = value; } } public string Size { get { return _Size; } set { _Size = value; } } public string Width { get { return _Width; } set { _Width = value; } } public string Length { get { return _Length; } set { _Length = value; } } public string Height { get { return _Height; } set { _Height = value; } } public string Girth { get { return _Girth; } set { _Girth = value; } } public bool Machinable { get { return (_Machinable ?? true); } set { _Machinable = value; } } public void SetWeightInDecimalPounds(double lbs) { _Pounds = (int)lbs; _Ounces = (int)Math.Ceiling(_Pounds.Value - lbs) * 16; } private bool EnsureRequiredFields() { return (_UserID != null && _Service != null && _ZipOrigination != null && _ZipDestination != null && _Pounds != null && _Ounces != null && _Size != null); } public string ToXMLString() { StringBuilder sbOut = new StringBuilder(); StringWriter sr = new StringWriter(sbOut); XmlTextWriter xmlWriter = new XmlTextWriter(sr); // begin the object xmlWriter.WriteStartElement("RateV3Request"); xmlWriter.WriteAttributeString("USERID", _UserID); int i = 0; while (_Pounds - (i*70) > 70) // loop through this until we have made all the packages but the last { // add a package at the weight of 70 pounds to the request xmlWriter.WriteStartElement("Package"); xmlWriter.WriteAttributeString("ID", i.ToString()); // write out all of the fields for the package xmlWriter.WriteElementString("Service", _Service); if (_FirstClassMailType != null) { xmlWriter.WriteElementString("FirstClassMailType", _FirstClassMailType); } xmlWriter.WriteElementString("ZipOrigination", _ZipOrigination); xmlWriter.WriteElementString("ZipDestination", _ZipDestination); xmlWriter.WriteElementString("Pounds", "70"); xmlWriter.WriteElementString("Ounces", "0"); if (_Container != null) { xmlWriter.WriteElementString("Container", _Container); } xmlWriter.WriteElementString("Size", _Size); if (_Width != null) { xmlWriter.WriteElementString("Width", _Width); } if (_Length != null) { xmlWriter.WriteElementString("Length", _Length); } if (_Height != null) { xmlWriter.WriteElementString("Height", _Height); } if (_Girth != null) { xmlWriter.WriteElementString("Girth", _Girth); } xmlWriter.WriteElementString("Machinable", (Machinable ? "true" : "false")); xmlWriter.WriteEndElement(); i++; } // write out the remainder of the shipment xmlWriter.WriteStartElement("Package"); xmlWriter.WriteAttributeString("ID", i.ToString()); // write out all of the fields for the package xmlWriter.WriteElementString("Service", _Service); if (_FirstClassMailType != null) { xmlWriter.WriteElementString("FirstClassMailType", _FirstClassMailType); } xmlWriter.WriteElementString("ZipOrigination", _ZipOrigination); xmlWriter.WriteElementString("ZipDestination", _ZipDestination); xmlWriter.WriteElementString("Pounds", (_Pounds - (i*70)).ToString()); xmlWriter.WriteElementString("Ounces", _Ounces.ToString()); if (_Container != null) { xmlWriter.WriteElementString("Container", _Container); } xmlWriter.WriteElementString("Size", _Size); if (_Width != null) { xmlWriter.WriteElementString("Width", _Width); } if (_Length != null) { xmlWriter.WriteElementString("Length", _Length); } if (_Height != null) { xmlWriter.WriteElementString("Height", _Height); } if (_Girth != null) { xmlWriter.WriteElementString("Girth", _Girth); } xmlWriter.WriteElementString("Machinable", (Machinable ? "true" : "false")); xmlWriter.WriteEndElement(); // xmlWriter.WriteEndElement(); // // clean up streams xmlWriter.Flush(); xmlWriter.Close(); sr.Flush(); sr.Close(); // return the string return sbOut.ToString(); } } public USPS() : base() { } public override List GetShippingMethods(string startzip, string endzip) { ShoppingCart ShoppingCart = ShoppingCart.Current; UPSRateRequest req = new UPSRateRequest(); req.UserID = "823WEBAD0427"; req.ZipOrigination = startzip; req.ZipDestination = endzip; req.SetWeightInDecimalPounds(ShoppingCart.TotalWeight); string PostData = req.ToXMLString(); XmlNode response = GetXmlResponse_USPS(ProductionURL, PostData); string[] ServicesSupported = Configuration.Get("Shipping.USPS.ServicesSupported").Split(','); List retList = new List(); foreach (XmlNode packageNode in response.ChildNodes[1].ChildNodes) { XmlNodeList xnl = packageNode.SelectNodes("Postage"); foreach (XmlNode xn in xnl) { string MailService = xn.SelectSingleNode("MailService").InnerText; // find out of the service is supported bool ServiceIsSupported = false; foreach (string s in ServicesSupported) { if (s == MailService) { ServiceIsSupported = true; break; } } if (ServiceIsSupported) { // grab the rate from XML decimal Rate = Decimal.Parse(xn.SelectSingleNode("Rate").InnerText); // Check if an existing method exists ShippingMethod existingMethod = null; foreach (ShippingMethod sm in retList) { if (sm.Name == MailService) { existingMethod = sm; break; } } // if an existing method exists, ADD the rate to it. otherwise create a new method if (existingMethod != null) existingMethod.Cost += Rate; else retList.Add(new ShippingMethod(MailService, Rate)); } } } return retList; /* List listSM = new List(); foreach (dotnetSHIP.Rate r in ship.Rates) { ShippingMethod sm = null; //TimeSpan ts = DateTime.Parse(r.ShipTime).Subtract(DateTime.Now); switch (r.Name) { case "FEDEX2DAY": sm = new ShippingMethod("Fedex 2 Day", Decimal.Parse(r.Charge)); break; case "FEDEXEXPRESSSAVER": sm = new ShippingMethod("Fedex 3 Day", Decimal.Parse(r.Charge)); break; case "GROUNDHOMEDELIVERY": sm = new ShippingMethod("Fedex Ground", Decimal.Parse(r.Charge)); break; default: break; } if (sm != null) { sm.DaysInTransit = r.ShipTime; listSM.Add(sm); } } return listSM; */ } public XmlNode GetXmlResponse_USPS(string url, string postData) { string FullURL = url + "?API=RateV3&XML=" + postData; HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(FullURL); objRequest.Method = "GET"; HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(objResponse.GetResponseStream()); objResponse.Close(); return xmlDoc; } }