Esempio n. 1
0
        private void BuildRatePackage(XmlWriter writer, IShipment shipment, UspsShipmentLineItemVisitor visitor)
        {
            var count = 0;

            var productWeight       = (int)Math.Ceiling(visitor.TotalWeight);
            int tOz                 = (int)Math.Ceiling(productWeight * 16.0m);
            int packageWeightPounds = tOz / 16;
            int packageWeightOunces = tOz % 16;

            var    productWidth  = (int)Math.Ceiling(visitor.TotalWidth);
            var    productLength = (int)Math.Ceiling(visitor.TotalLength);
            var    productHeight = (int)Math.Ceiling(visitor.TotalHeight);
            string packageSize   = GetPackageSize(productLength, productHeight, productWidth);

            writer.WriteStartElement("Package");
            writer.WriteAttributeString("ID", count.ToString());
            writer.WriteElementString("Service", "ALL");
            writer.WriteElementString("ZipOrigination", shipment.FromPostalCode);
            writer.WriteElementString("ZipDestination", shipment.ToPostalCode);
            writer.WriteElementString("Pounds", packageWeightPounds.ToString());
            writer.WriteElementString("Ounces", packageWeightOunces.ToString());
            writer.WriteElementString("Container", "Variable");
            writer.WriteElementString("Size", packageSize);
            writer.WriteElementString("Width", productWidth.ToString());
            writer.WriteElementString("Length", productLength.ToString());
            writer.WriteElementString("Height", productHeight.ToString());
            writer.WriteElementString("Machinable", "FALSE");  // Packages are not machinable
            writer.WriteEndElement();
        }
Esempio n. 2
0
        private string BuildRatePackage(IShipment shipment, UspsShipmentLineItemVisitor visitor)
        {
            var count = 0;
            var sb    = new StringBuilder();

            var productWeight       = (int)Math.Ceiling(visitor.TotalWeight);
            int tOz                 = (int)Math.Ceiling(productWeight * 16.0m);
            int packageWeightPounds = tOz / 16;
            int packageWeightOunces = tOz % 16;

            var    productWidth  = (int)Math.Ceiling(visitor.TotalWidth);
            var    productLength = (int)Math.Ceiling(visitor.TotalLength);
            var    productHeight = (int)Math.Ceiling(visitor.TotalHeight);
            string packageSize   = GetPackageSize(productLength, productHeight, productWidth);

            sb.AppendFormat("<Package ID=\"{0}\">", count);
            sb.Append("<Service>ALL</Service>");
            sb.AppendFormat("<ZipOrigination>{0}</ZipOrigination>", shipment.FromPostalCode);
            sb.AppendFormat("<ZipDestination>{0}</ZipDestination>", shipment.ToPostalCode);
            sb.AppendFormat("<Pounds>{0}</Pounds>", packageWeightPounds);
            sb.AppendFormat("<Ounces>{0}</Ounces>", packageWeightOunces);
            sb.Append("<Container>Variable</Container>");
            sb.AppendFormat("<Size>{0}</Size>", packageSize);
            sb.AppendFormat("<Width>{0}</Width>", productWidth);
            sb.AppendFormat("<Length>{0}</Length>", productLength);
            sb.AppendFormat("<Height>{0}</Height>", productHeight);
            sb.Append("<Machinable>FALSE</Machinable>"); // Packages are not machinable
            sb.Append("</Package>");

            return(sb.ToString());
        }
Esempio n. 3
0
        // Create rating request XML string
        private string RateRequest(IShipment shipment, UspsShipmentLineItemVisitor visitor)
        {
            // Changed weight logic per JeffreyABecker suggestions
            //int tOz = (int)Math.Ceiling(weight * 16.0m);
            //int packageWeightPounds = tOz / 16;
            //int packageWeightOunces = tOz % 16;

            var sb = new StringBuilder();

            sb.AppendFormat(shipment.ToCountryCode == "US"
                    ? "<RateV4Request USERID=\"{0}\" PASSWORD=\"{1}\">"
                    : "<IntlRateV2Response  USERID=\"{0}\" PASSWORD=\"{1}\">", _processorSettings.UspsUsername, _processorSettings.UspsPassword);
            sb.Append("<Revision/>");
            string packageStr = BuildRatePackage(shipment, visitor);

            sb.Append(packageStr);
            sb.Append("</RateV4Request>");
            return("API=RateV4&XML=" + sb);
        }
Esempio n. 4
0
        // Create rating request XML string
        private string RateRequest(IShipment shipment, UspsShipmentLineItemVisitor visitor)
        {
            // Changed weight logic per JeffreyABecker suggestions
            //int tOz = (int)Math.Ceiling(weight * 16.0m);
            //int packageWeightPounds = tOz / 16;
            //int packageWeightOunces = tOz % 16;

            var sb = new StringBuilder();

            using (var writer = XmlWriter.Create(sb, new XmlWriterSettings()
            {
                OmitXmlDeclaration = true
            }))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement(shipment.ToCountryCode == "US" ? "RateV4Request" : "IntlRateV2Response");
                writer.WriteAttributeString("USERID", _processorSettings.UspsUsername);
                writer.WriteAttributeString("PASSWORD", _processorSettings.UspsPassword);
                writer.WriteElementString("Revision", null);
                BuildRatePackage(writer, shipment, visitor);
                writer.WriteEndDocument();
            }
            return("API=RateV4&XML=" + ApplicationXWwwFormUrlencodedEncode(sb.ToString()));
        }
Esempio n. 5
0
        public override Attempt <IShipmentRateQuote> QuoteShipment(IShipment shipment)
        {
            decimal shippingPrice = 0M;

            try
            {
                var visitor = new UspsShipmentLineItemVisitor()
                {
                    UseOnSalePriceIfOnSale = false
                };

                shipment.Items.Accept(visitor);

                var http = new HttpRequestHandler();

                var collection = GetCollectionFromCache(shipment);

                if (collection == null)
                {
                    var rateXml = http.Post(RateRequest(shipment, visitor));

                    collection = UspsParseRates(rateXml, shipment);

                    _runtimeCache.InsertCacheItem(MakeCacheKey(shipment), () => collection);
                }
                //if (collection.Any(option => option.Service.Contains(shipment.ShipmentName) &&
                //                             option.Service.ToLower().Contains("medium") || option.Service.ToLower().Contains("large")))
                //{
                //    var packageSize = GetPackageSize(length, height, width);
                //    shippingPrice = packageSize == "REGULAR" ? collection.First(option => option.Service.Contains(shipment.ShipmentName) &&
                //        option.Service.ToLower().Contains("medium")).Rate : collection.First(option => option.Service.Contains(shipment.ShipmentName) &&
                //            option.Service.ToLower().Contains("large")).Rate;
                //}
                //else
                //{
                //    shippingPrice = collection.First(option => option.Service.Contains(shipment.ShipmentName)).Rate;
                //}
                var namedCollection = collection.Where(option => HttpUtility.HtmlDecode(option.Service).Contains(HttpUtility.HtmlDecode(_shipMethod.ServiceCode)));
                var deliveryOptions = namedCollection as IList <DeliveryOption> ?? namedCollection.ToList();
                if (deliveryOptions.Any(o => o.Service.Contains("medium") || o.Service.Contains("large")))
                {
                    var packageSize    = GetPackageSize(1, 1, 1);
                    var firstOrDefault = deliveryOptions.FirstOrDefault(x => x.Service.Contains("medium"));
                    if (firstOrDefault != null)
                    {
                        var deliveryOption = deliveryOptions.FirstOrDefault(x => x.Service.Contains("large"));
                        if (deliveryOption != null)
                        {
                            shippingPrice = packageSize == "REGULAR" ? firstOrDefault.Rate : deliveryOption.Rate;
                        }
                    }
                }
                else
                {
                    var firstOrDefault = deliveryOptions.FirstOrDefault();
                    if (firstOrDefault != null)
                    {
                        shippingPrice = firstOrDefault.Rate;
                    }
                }
            }
            catch (Exception ex)
            {
                return(Attempt <IShipmentRateQuote> .Fail(
                           new Exception("An error occured during your request : " +
                                         ex.Message +
                                         " Please contact your administrator or try again.")));
            }

            return(Attempt <IShipmentRateQuote> .Succeed(new ShipmentRateQuote(shipment, _shipMethod) { Rate = shippingPrice }));
        }
        public override Attempt<IShipmentRateQuote> QuoteShipment(IShipment shipment)
        {            
            decimal shippingPrice = 0M;
            try
            {
                var visitor = new UspsShipmentLineItemVisitor() { UseOnSalePriceIfOnSale = false };

                shipment.Items.Accept(visitor);

                var http = new HttpRequestHandler();

                var collection = GetCollectionFromCache(shipment);

                if (collection == null)
                {
                    var rateXml = http.Post(RateRequest(shipment, visitor));

                    collection = UspsParseRates(rateXml, shipment);

                    _runtimeCache.InsertCacheItem(MakeCacheKey(shipment), () => collection);      
                }
                //if (collection.Any(option => option.Service.Contains(shipment.ShipmentName) &&
                //                             option.Service.ToLower().Contains("medium") || option.Service.ToLower().Contains("large")))
                //{
                //    var packageSize = GetPackageSize(length, height, width);
                //    shippingPrice = packageSize == "REGULAR" ? collection.First(option => option.Service.Contains(shipment.ShipmentName) && 
                //        option.Service.ToLower().Contains("medium")).Rate : collection.First(option => option.Service.Contains(shipment.ShipmentName) && 
                //            option.Service.ToLower().Contains("large")).Rate;
                //}
                //else
                //{
                //    shippingPrice = collection.First(option => option.Service.Contains(shipment.ShipmentName)).Rate;
                //}
                var namedCollection = collection.Where(option => HttpUtility.HtmlDecode(option.Service).Contains(HttpUtility.HtmlDecode(_shipMethod.ServiceCode)));
                var deliveryOptions = namedCollection as IList<DeliveryOption> ?? namedCollection.ToList();
                if (deliveryOptions.Any(o => o.Service.Contains("medium") || o.Service.Contains("large")))
                {
                    var packageSize = GetPackageSize(1, 1, 1);
                    var firstOrDefault = deliveryOptions.FirstOrDefault(x => x.Service.Contains("medium"));
                    if (firstOrDefault != null)
                    {
                        var deliveryOption = deliveryOptions.FirstOrDefault(x => x.Service.Contains("large"));
                        if (deliveryOption != null)
                            shippingPrice = packageSize == "REGULAR" ? firstOrDefault.Rate : deliveryOption.Rate;
                    }
                }
                else
                {
                    var firstOrDefault = deliveryOptions.FirstOrDefault();
                    if (firstOrDefault != null) shippingPrice = firstOrDefault.Rate;
                }
            }
            catch (Exception ex)
            {
                return Attempt<IShipmentRateQuote>.Fail(
                           new Exception("An error occured during your request : " +
                                                        ex.Message +
                                                        " Please contact your administrator or try again."));
            }

            return Attempt<IShipmentRateQuote>.Succeed(new ShipmentRateQuote(shipment, _shipMethod) { Rate = shippingPrice });
        }
        private string BuildRatePackage(IShipment shipment, UspsShipmentLineItemVisitor visitor)
        {
            var count = 0;
            var sb = new StringBuilder();        
             
            var productWeight = (int)Math.Ceiling(visitor.TotalWeight);
            int tOz = (int)Math.Ceiling(productWeight * 16.0m);
            int packageWeightPounds = tOz / 16;
            int packageWeightOunces = tOz % 16;

            var productWidth = (int)Math.Ceiling(visitor.TotalWidth);
            var productLength = (int)Math.Ceiling(visitor.TotalLength);
            var productHeight = (int)Math.Ceiling(visitor.TotalHeight);
            string packageSize = GetPackageSize(productLength, productHeight, productWidth);
           
            sb.AppendFormat("<Package ID=\"{0}\">", count);
            sb.Append("<Service>ALL</Service>");
            sb.AppendFormat("<ZipOrigination>{0}</ZipOrigination>", shipment.FromPostalCode);
            sb.AppendFormat("<ZipDestination>{0}</ZipDestination>", shipment.ToPostalCode);
            sb.AppendFormat("<Pounds>{0}</Pounds>", packageWeightPounds);
            sb.AppendFormat("<Ounces>{0}</Ounces>", packageWeightOunces);
            sb.Append("<Container>Variable</Container>");
            sb.AppendFormat("<Size>{0}</Size>", packageSize);
            sb.AppendFormat("<Width>{0}</Width>", productWidth);
            sb.AppendFormat("<Length>{0}</Length>", productLength);
            sb.AppendFormat("<Height>{0}</Height>", productHeight);
            sb.Append("<Machinable>FALSE</Machinable>"); // Packages are not machinable
            sb.Append("</Package>");
                                               
            return sb.ToString();
        }
        // Create rating request XML string
        private string RateRequest(IShipment shipment, UspsShipmentLineItemVisitor visitor)
        {
            // Changed weight logic per JeffreyABecker suggestions
            //int tOz = (int)Math.Ceiling(weight * 16.0m);
            //int packageWeightPounds = tOz / 16;
            //int packageWeightOunces = tOz % 16;

            var sb = new StringBuilder();
            sb.AppendFormat(shipment.ToCountryCode == "US"
                    ? "<RateV4Request USERID=\"{0}\" PASSWORD=\"{1}\">"
                    : "<IntlRateV2Response  USERID=\"{0}\" PASSWORD=\"{1}\">", _processorSettings.UspsUsername, _processorSettings.UspsPassword);
            sb.Append("<Revision/>");
            string packageStr = BuildRatePackage(shipment, visitor);
            sb.Append(packageStr);
            sb.Append("</RateV4Request>");
            return "API=RateV4&XML=" + sb;
        }
        // Create rating request XML string
        private string RateRequest(IShipment shipment, UspsShipmentLineItemVisitor visitor)
        {
            // Changed weight logic per JeffreyABecker suggestions
            //int tOz = (int)Math.Ceiling(weight * 16.0m);
            //int packageWeightPounds = tOz / 16;
            //int packageWeightOunces = tOz % 16;

            var sb = new StringBuilder();
            using (var writer = XmlWriter.Create(sb, new XmlWriterSettings() { OmitXmlDeclaration = true }))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement(shipment.ToCountryCode == "US" ? "RateV4Request" : "IntlRateV2Response");
                writer.WriteAttributeString("USERID", _processorSettings.UspsUsername);
                writer.WriteAttributeString("PASSWORD", _processorSettings.UspsPassword);
                writer.WriteElementString("Revision", null);
                BuildRatePackage(writer, shipment, visitor);
                writer.WriteEndDocument();
            }
            return "API=RateV4&XML=" + ApplicationXWwwFormUrlencodedEncode(sb.ToString());
        }
        private void BuildRatePackage(XmlWriter writer, IShipment shipment, UspsShipmentLineItemVisitor visitor)
        {
            var count = 0;

            var productWeight = (int)Math.Ceiling(visitor.TotalWeight);
            int tOz = (int)Math.Ceiling(productWeight * 16.0m);
            int packageWeightPounds = tOz / 16;
            int packageWeightOunces = tOz % 16;

            var productWidth = (int)Math.Ceiling(visitor.TotalWidth);
            var productLength = (int)Math.Ceiling(visitor.TotalLength);
            var productHeight = (int)Math.Ceiling(visitor.TotalHeight);
            string packageSize = GetPackageSize(productLength, productHeight, productWidth);

            writer.WriteStartElement("Package");
            writer.WriteAttributeString("ID", count.ToString());
            writer.WriteElementString("Service", "ALL");
            writer.WriteElementString("ZipOrigination", shipment.FromPostalCode);
            writer.WriteElementString("ZipDestination", shipment.ToPostalCode);
            writer.WriteElementString("Pounds", packageWeightPounds.ToString());
            writer.WriteElementString("Ounces", packageWeightOunces.ToString());
            writer.WriteElementString("Container", "Variable");
            writer.WriteElementString("Size", packageSize);
            writer.WriteElementString("Width", productWidth.ToString());
            writer.WriteElementString("Length", productLength.ToString());
            writer.WriteElementString("Height", productHeight.ToString());
            writer.WriteElementString("Machinable", "FALSE");  // Packages are not machinable
            writer.WriteEndElement();
        }