/// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="ShipmentPackage">Shipment package</param>
        /// <param name="Error">Error</param>
        /// <returns>Shipping options</returns>
        public ShippingOptionCollection GetShippingOptions(ShipmentPackage ShipmentPackage, ref string Error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            if (ShipmentPackage == null)
            {
                throw new ArgumentNullException("ShipmentPackage");
            }
            if (ShipmentPackage.Items == null)
            {
                throw new NopException("No shipment items");
            }

            ShippingMethodCollection shippingMethods = ShippingMethodManager.GetAllShippingMethods();

            foreach (ShippingMethod shippingMethod in shippingMethods)
            {
                ShippingOption shippingOption = new ShippingOption();
                shippingOption.Name        = shippingMethod.Name;
                shippingOption.Description = shippingMethod.Description;
                shippingOption.Rate        = decimal.Zero;
                shippingOptions.Add(shippingOption);
            }

            return(shippingOptions);
        }
Ejemplo n.º 2
0
        private ShippingOptionCollection ParseResponse(RateReply reply)
        {
            var result = new ShippingOptionCollection();

            Debug.WriteLine("RateReply details:");
            Debug.WriteLine("**********************************************************");
            foreach (var rateDetail in reply.RateReplyDetails)
            {
                Debug.WriteLine("ServiceType: " + rateDetail.ServiceType);
                for (int i = 0; i < rateDetail.RatedShipmentDetails.Length; i++)
                {
                    var shipmentDetail = rateDetail.RatedShipmentDetails[i];
                    Debug.WriteLine("RateType : " + shipmentDetail.ShipmentRateDetail.RateType);
                    Debug.WriteLine("Total Billing Weight : " + shipmentDetail.ShipmentRateDetail.TotalBillingWeight.Value);
                    Debug.WriteLine("Total Base Charge : " + shipmentDetail.ShipmentRateDetail.TotalBaseCharge.Amount);
                    Debug.WriteLine("Total Discount : " + shipmentDetail.ShipmentRateDetail.TotalFreightDiscounts.Amount);
                    Debug.WriteLine("Total Surcharges : " + shipmentDetail.ShipmentRateDetail.TotalSurcharges.Amount);
                    Debug.WriteLine("Net Charge : " + shipmentDetail.ShipmentRateDetail.TotalNetCharge.Amount);
                    Debug.WriteLine("*********");

                    //take first one
                    if (i == 0)
                    {
                        var    shippingOption          = new ShippingOption();
                        string userFriendlyServiceType = GetUserFriendlyEnum(rateDetail.ServiceType.ToString());
                        shippingOption.Name = userFriendlyServiceType;
                        shippingOption.Rate = shipmentDetail.ShipmentRateDetail.TotalNetCharge.Amount;
                        result.Add(shippingOption);
                    }
                }
                Debug.WriteLine("**********************************************************");
            }

            return(result);
        }
 /// <summary>
 /// Gets the shipping options.
 /// </summary>
 /// <param name="order"></param>
 /// <returns></returns>
 public ShippingOptionCollection GetShippingOptions(Order order)
 {
     ShippingOptionCollection shippingOptionCollection = new ShippingOptionCollection();
       SimpleWeightShippingRateCollection simpleWeightShippingRateCollection = new SimpleWeightShippingRateController().FetchAll();
       ShippingOption shippingOption;
       foreach(SimpleWeightShippingRate simpleWeightShippingRate in simpleWeightShippingRateCollection) {
     shippingOption = new ShippingOption();
     shippingOption.Service = simpleWeightShippingRate.Service;
     shippingOption.Rate = simpleWeightShippingRate.AmountPerUnit * order.TotalWeight;
     shippingOptionCollection.Add(shippingOption);
       }
       return shippingOptionCollection;
 }
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="shipmentPackage">Shipment package</param>
        /// <param name="error">Error</param>
        /// <returns>Shipping options</returns>
        public ShippingOptionCollection GetShippingOptions(ShipmentPackage shipmentPackage, ref string error)
        {
            var shippingOptions = new ShippingOptionCollection();

            if (shipmentPackage == null)
            {
                throw new ArgumentNullException("shipmentPackage");
            }
            if (shipmentPackage.Items == null)
            {
                throw new NopException("No shipment items");
            }
            if (shipmentPackage.ShippingAddress == null)
            {
                error = "Shipping address is not set";
                return(shippingOptions);
            }
            if (shipmentPackage.ShippingAddress.Country == null)
            {
                error = "Shipping country is not set";
                return(shippingOptions);
            }

            decimal subTotal = decimal.Zero;

            foreach (var shoppingCartItem in shipmentPackage.Items)
            {
                if (shoppingCartItem.IsFreeShipping)
                {
                    continue;
                }
                subTotal += PriceHelper.GetSubTotal(shoppingCartItem, shipmentPackage.Customer, true);
            }

            decimal weight = ShippingManager.GetShoppingCartTotalWeigth(shipmentPackage.Items, shipmentPackage.Customer);

            var shippingMethods = ShippingMethodManager.GetAllShippingMethods(shipmentPackage.ShippingAddress.CountryId);

            foreach (var shippingMethod in shippingMethods)
            {
                var shippingOption = new ShippingOption();
                shippingOption.Name        = shippingMethod.Name;
                shippingOption.Description = shippingMethod.Description;
                shippingOption.Rate        = GetRate(subTotal, weight, shippingMethod.ShippingMethodId);
                shippingOptions.Add(shippingOption);
            }

            return(shippingOptions);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the shipping options.
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public ShippingOptionCollection GetShippingOptions(Order order)
        {
            ShippingOptionCollection           shippingOptionCollection           = new ShippingOptionCollection();
            SimpleWeightShippingRateCollection simpleWeightShippingRateCollection = new SimpleWeightShippingRateController().FetchAll();
            ShippingOption shippingOption;

            foreach (SimpleWeightShippingRate simpleWeightShippingRate in simpleWeightShippingRateCollection)
            {
                shippingOption         = new ShippingOption();
                shippingOption.Service = simpleWeightShippingRate.Service;
                shippingOption.Rate    = simpleWeightShippingRate.AmountPerUnit * order.TotalWeight;
                shippingOptionCollection.Add(shippingOption);
            }
            return(shippingOptionCollection);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="shipmentPackage">Shipment package</param>
        /// <param name="error">Error</param>
        /// <returns>Shipping options</returns>
        public ShippingOptionCollection GetShippingOptions(ShipmentPackage shipmentPackage, ref string error)
        {
            var shippingOptions = new ShippingOptionCollection();

            if (shipmentPackage == null)
            {
                throw new ArgumentNullException("shipmentPackage");
            }
            if (shipmentPackage.Items == null)
            {
                throw new NopException("Нет товаров для доставки. ");
            }
            if (shipmentPackage.ShippingAddress == null)
            {
                error = "Не указан адрес доставки. ";
                return(shippingOptions);
            }
            if (shipmentPackage.ShippingAddress.Country == null)
            {
                error = "Не указана страна доставки. ";
                return(shippingOptions);
            }

            var shippingMethods = ShippingMethodManager.GetAllShippingMethods(shipmentPackage.ShippingAddress.CountryId);

            foreach (var shippingMethod in shippingMethods)
            {
                var shippingOption = new ShippingOption();
                shippingOption.Name        = shippingMethod.Name;
                shippingOption.Description = shippingMethod.Description;
                if (shippingMethod.ShippingMethodId == 4)
                {
                    shippingOption.Rate = 0;
                }
                else
                {
                    shippingOption.Rate = GetRate();
                }
                shippingOptions.Add(shippingOption);
            }

            return(shippingOptions);
        }
Ejemplo n.º 7
0
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="ShipmentPackage">Shipment package</param>
        /// <param name="Error">Error</param>
        /// <returns>Shipping options</returns>
        public ShippingOptionCollection GetShippingOptions(ShipmentPackage ShipmentPackage, ref string Error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            if (ShipmentPackage == null)
            {
                throw new ArgumentNullException("ShipmentPackage");
            }
            if (ShipmentPackage.Items == null)
            {
                throw new NopException("No shipment items");
            }

            decimal subTotal = decimal.Zero;

            foreach (ShoppingCartItem shoppingCartItem in ShipmentPackage.Items)
            {
                if (shoppingCartItem.IsFreeShipping)
                {
                    continue;
                }
                subTotal += PriceHelper.GetSubTotal(shoppingCartItem, ShipmentPackage.Customer, true);
            }

            decimal weight = ShippingManager.GetShoppingCartTotalWeigth(ShipmentPackage.Items);

            ShippingMethodCollection shippingMethods = ShippingMethodManager.GetAllShippingMethods();

            foreach (ShippingMethod shippingMethod in shippingMethods)
            {
                ShippingOption shippingOption = new ShippingOption();
                shippingOption.Name        = shippingMethod.Name;
                shippingOption.Description = shippingMethod.Description;
                shippingOption.Rate        = GetRate(subTotal, weight, shippingMethod.ShippingMethodID);
                shippingOptions.Add(shippingOption);
            }

            return(shippingOptions);
        }
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="ShipmentPackage">Shipment package</param>
        /// <param name="Error">Error</param>
        /// <returns>Shipping options</returns>
        public ShippingOptionCollection GetShippingOptions(ShipmentPackage ShipmentPackage, ref string Error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            if (ShipmentPackage == null)
                throw new ArgumentNullException("ShipmentPackage");
            if (ShipmentPackage.Items == null)
                throw new NopException("No shipment items");

            

            ShippingMethodCollection shippingMethods = ShippingMethodManager.GetAllShippingMethods();
            foreach (ShippingMethod shippingMethod in shippingMethods)
            {
                ShippingOption shippingOption = new ShippingOption();
                shippingOption.Name = shippingMethod.Name;
                shippingOption.Description = shippingMethod.Description;
                shippingOption.Rate = GetRate();
                shippingOptions.Add(shippingOption);
            }

            return shippingOptions;
        }
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="shipmentPackage">Shipment package</param>
        /// <param name="error">Error</param>
        /// <returns>Shipping options</returns>
        public ShippingOptionCollection GetShippingOptions(ShipmentPackage shipmentPackage, ref string error)
        {
            var shippingOptions = new ShippingOptionCollection();

            if (shipmentPackage == null)
            {
                throw new ArgumentNullException("shipmentPackage");
            }
            if (shipmentPackage.Items == null)
            {
                throw new NopException("No shipment items");
            }
            if (shipmentPackage.ShippingAddress == null)
            {
                error = "Shipping address is not set";
                return(shippingOptions);
            }
            if (shipmentPackage.ShippingAddress.Country == null)
            {
                error = "Shipping country is not set";
                return(shippingOptions);
            }

            var shippingMethods = ShippingMethodManager.GetAllShippingMethods(shipmentPackage.ShippingAddress.CountryId);

            foreach (var shippingMethod in shippingMethods)
            {
                var shippingOption = new ShippingOption();
                shippingOption.Name        = shippingMethod.Name;
                shippingOption.Description = shippingMethod.Description;
                shippingOption.Rate        = GetRate();
                shippingOptions.Add(shippingOption);
            }

            return(shippingOptions);
        }
Ejemplo n.º 10
0
        private ShippingOptionCollection ParseResponse(string response, ref string error)
        {
            var shippingOptions = new ShippingOptionCollection();

            using (var sr = new StringReader(response))
                using (var tr = new XmlTextReader(sr))
                    while (tr.Read())
                    {
                        if ((tr.Name == "Error") && (tr.NodeType == XmlNodeType.Element))
                        {
                            string errorText = "";
                            while (tr.Read())
                            {
                                if ((tr.Name == "ErrorCode") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    errorText += "UPS Rating Error, Error Code: " + tr.ReadString() + ", ";
                                }
                                if ((tr.Name == "ErrorDescription") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    errorText += "Error Desc: " + tr.ReadString();
                                }
                            }
                            error = "UPS Error returned: " + errorText;
                        }
                        if ((tr.Name == "RatedShipment") && (tr.NodeType == XmlNodeType.Element))
                        {
                            string serviceCode   = "";
                            string monetaryValue = "";
                            while (tr.Read())
                            {
                                if ((tr.Name == "Service") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    while (tr.Read())
                                    {
                                        if ((tr.Name == "Code") && (tr.NodeType == XmlNodeType.Element))
                                        {
                                            serviceCode = tr.ReadString();
                                            tr.ReadEndElement();
                                        }
                                        if ((tr.Name == "Service") && (tr.NodeType == XmlNodeType.EndElement))
                                        {
                                            break;
                                        }
                                    }
                                }
                                if (((tr.Name == "RatedShipment") && (tr.NodeType == XmlNodeType.EndElement)) || ((tr.Name == "RatedPackage") && (tr.NodeType == XmlNodeType.Element)))
                                {
                                    break;
                                }
                                if ((tr.Name == "TotalCharges") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    while (tr.Read())
                                    {
                                        if ((tr.Name == "MonetaryValue") && (tr.NodeType == XmlNodeType.Element))
                                        {
                                            monetaryValue = tr.ReadString();
                                            tr.ReadEndElement();
                                        }
                                        if ((tr.Name == "TotalCharges") && (tr.NodeType == XmlNodeType.EndElement))
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                            string service = GetServiceName(serviceCode);

                            //Weed out unwanted or unkown service rates
                            if (service.ToUpper() != "UNKNOWN")
                            {
                                var shippingOption = new ShippingOption();
                                shippingOption.Rate = Convert.ToDecimal(monetaryValue, new CultureInfo("en-US"));
                                shippingOption.Name = service;
                                shippingOptions.Add(shippingOption);
                            }
                        }
                    }

            return(shippingOptions);
        }
        private ShippingOptionCollection ParseResponse(string response, ref string error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            using (StringReader sr = new StringReader(response))
            using (XmlTextReader tr = new XmlTextReader(sr))


                do
                // while (tr.Read())
                {
                    // Read the next XML record
                    tr.Read();

                    if ((tr.Name == "Error") && (tr.NodeType == XmlNodeType.Element))
                    {
                        string errorText = "";
                        while (tr.Read())
                        {
                            if ((tr.Name == "Description") && (tr.NodeType == XmlNodeType.Element))
                                errorText += "Error Desc: " + tr.ReadString();
                            if ((tr.Name == "HelpContext") && (tr.NodeType == XmlNodeType.Element))
                                errorText += "USPS Help Context: " + tr.ReadString() + ". ";
                        }
                        error = "USPS Error returned: " + errorText;
                    }

                    // Process the inner postage XML
                    if ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element))
                    {
                        string serviceCode = "";
                        string postalRate = "";

                        // while (tr.Read())
                        do
                        {

                            tr.Read();

                            if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.Element))
                            {
                                serviceCode = tr.ReadString();

                                tr.ReadEndElement();
                                if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.EndElement))
                                    break;
                            }
                            // if (((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)) || ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element)))
                            //   break;

                            if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.Element))
                            {
                                postalRate = tr.ReadString();
                                tr.ReadEndElement();
                                if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.EndElement))
                                    break;
                            }

                        } while (!((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)));

                        if (shippingOptions.Find((s) => s.Name == serviceCode) == null)
                        {
                            ShippingOption shippingOption = new ShippingOption();
                            //TODO check whether we need to multiply rate by package quantity
                            shippingOption.Rate = Convert.ToDecimal(postalRate, new CultureInfo("en-US"));
                            shippingOption.Name = serviceCode;
                            shippingOptions.Add(shippingOption);
                        }
                    }
                } while (!tr.EOF);
            return shippingOptions;
        }
        private ShippingOptionCollection ParseResponse(string response, bool isDomestic, ref string error)
        {
            var shippingOptions = new ShippingOptionCollection();

            string postageStr     = isDomestic ? "Postage" : "Service";
            string mailServiceStr = isDomestic ? "MailService" : "SvcDescription";
            string rateStr        = isDomestic ? "Rate" : "Postage";

            using (var sr = new StringReader(response))
                using (var tr = new XmlTextReader(sr))
                {
                    do
                    {
                        tr.Read();

                        if ((tr.Name == "Error") && (tr.NodeType == XmlNodeType.Element))
                        {
                            string errorText = "";
                            while (tr.Read())
                            {
                                if ((tr.Name == "Description") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    errorText += "Error Desc: " + tr.ReadString();
                                }
                                if ((tr.Name == "HelpContext") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    errorText += "USPS Help Context: " + tr.ReadString() + ". ";
                                }
                            }
                            error = "USPS Error returned: " + errorText;
                        }

                        if ((tr.Name == postageStr) && (tr.NodeType == XmlNodeType.Element))
                        {
                            string serviceCode = string.Empty;
                            string postalRate  = string.Empty;

                            do
                            {
                                tr.Read();

                                if ((tr.Name == mailServiceStr) && (tr.NodeType == XmlNodeType.Element))
                                {
                                    serviceCode = tr.ReadString();

                                    tr.ReadEndElement();
                                    if ((tr.Name == mailServiceStr) && (tr.NodeType == XmlNodeType.EndElement))
                                    {
                                        break;
                                    }
                                }

                                if ((tr.Name == rateStr) && (tr.NodeType == XmlNodeType.Element))
                                {
                                    postalRate = tr.ReadString();
                                    tr.ReadEndElement();
                                    if ((tr.Name == rateStr) && (tr.NodeType == XmlNodeType.EndElement))
                                    {
                                        break;
                                    }
                                }
                            }while (!((tr.Name == postageStr) && (tr.NodeType == XmlNodeType.EndElement)));

                            if (shippingOptions.Find((s) => s.Name == serviceCode) == null)
                            {
                                var shippingOption = new ShippingOption();
                                //TODO check whether we need to multiply rate by package quantity
                                shippingOption.Rate = Convert.ToDecimal(postalRate, new CultureInfo("en-US"));
                                shippingOption.Name = serviceCode;
                                shippingOptions.Add(shippingOption);
                            }
                        }
                    }while (!tr.EOF);
                }
            return(shippingOptions);
        }
        private ShippingOptionCollection ParseResponse(string response, ref string error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            using (StringReader sr = new StringReader(response))
            using (XmlTextReader tr = new XmlTextReader(sr))
                while (tr.Read())
                {
                    if ((tr.Name == "Error") && (tr.NodeType == XmlNodeType.Element))
                    {
                        string errorText = "";
                        while (tr.Read())
                        {
                            if ((tr.Name == "ErrorCode") && (tr.NodeType == XmlNodeType.Element))
                            {
                                errorText += "UPS Rating Error, Error Code: " + tr.ReadString() + ", ";
                            }
                            if ((tr.Name == "ErrorDescription") && (tr.NodeType == XmlNodeType.Element))
                            {
                                errorText += "Error Desc: " + tr.ReadString();
                            }
                        }
                        error = "UPS Error returned: " + errorText;
                    }
                    if ((tr.Name == "RatedShipment") && (tr.NodeType == XmlNodeType.Element))
                    {
                        string serviceCode = "";
                        string monetaryValue = "";
                        while (tr.Read())
                        {
                            if ((tr.Name == "Service") && (tr.NodeType == XmlNodeType.Element))
                            {
                                while (tr.Read())
                                {
                                    if ((tr.Name == "Code") && (tr.NodeType == XmlNodeType.Element))
                                    {
                                        serviceCode = tr.ReadString();
                                        tr.ReadEndElement();
                                    }
                                    if ((tr.Name == "Service") && (tr.NodeType == XmlNodeType.EndElement))
                                    {
                                        break;
                                    }
                                }
                            }
                            if (((tr.Name == "RatedShipment") && (tr.NodeType == XmlNodeType.EndElement)) || ((tr.Name == "RatedPackage") && (tr.NodeType == XmlNodeType.Element)))
                            {
                                break;
                            }
                            if ((tr.Name == "TotalCharges") && (tr.NodeType == XmlNodeType.Element))
                            {
                                while (tr.Read())
                                {
                                    if ((tr.Name == "MonetaryValue") && (tr.NodeType == XmlNodeType.Element))
                                    {
                                        monetaryValue = tr.ReadString();
                                        tr.ReadEndElement();
                                    }
                                    if ((tr.Name == "TotalCharges") && (tr.NodeType == XmlNodeType.EndElement))
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        string service = GetServiceName(serviceCode);

                        //Weed out unwanted or unkown service rates
                        if (service.ToUpper() != "UNKNOWN")
                        {
                            ShippingOption shippingOption = new ShippingOption();
                            shippingOption.Rate = Convert.ToDecimal(monetaryValue, new CultureInfo("en-US"));
                            shippingOption.Name = service;
                            shippingOptions.Add(shippingOption);
                        }

                    }
                }

            return shippingOptions;
        }
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="shipmentPackage">Shipment package</param>
        /// <param name="error">Error</param>
        /// <returns>Shipping options</returns>
        public ShippingOptionCollection GetShippingOptions(ShipmentPackage shipmentPackage, ref string error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            if (shipmentPackage == null)
            {
                throw new ArgumentNullException("shipmentPackage");
            }
            if (shipmentPackage.Items == null)
            {
                throw new NopException("No shipment items");
            }
            if (shipmentPackage.ShippingAddress == null)
            {
                error = "Shipping address is not set";
                return(shippingOptions);
            }

            shipmentPackage.ZipPostalCodeFrom = AustraliaPostSettings.ShippedFromZipPostalCode;
            string  ZipPostalCodeFrom = shipmentPackage.ZipPostalCodeFrom;
            string  ZipPostalCodeTo   = shipmentPackage.ShippingAddress.ZipPostalCode;
            int     weight            = GetWeight(shipmentPackage);
            int     length            = GetLength(shipmentPackage);
            int     width             = GetWidth(shipmentPackage);
            int     height            = GetHeight(shipmentPackage);
            Country country           = shipmentPackage.ShippingAddress.Country;

            if (length > MAX_LENGTH)
            {
                error = "Length exceed.";
                return(shippingOptions);
            }
            if (weight > MAX_WEIGHT)
            {
                error = "Weight exceed.";
                return(shippingOptions);
            }

            try
            {
                switch (country.ThreeLetterIsoCode)
                {
                case "AUS":
                    shippingOptions.Add(RequestShippingOption(ZipPostalCodeFrom, ZipPostalCodeTo, country.TwoLetterIsoCode, "Standard", weight, length, width, height));
                    shippingOptions.Add(RequestShippingOption(ZipPostalCodeFrom, ZipPostalCodeTo, country.TwoLetterIsoCode, "Express", weight, length, width, height));
                    shippingOptions.Add(RequestShippingOption(ZipPostalCodeFrom, ZipPostalCodeTo, country.TwoLetterIsoCode, "EXP_PLT", weight, length, width, height));
                    break;

                default:
                    shippingOptions.Add(RequestShippingOption(ZipPostalCodeFrom, ZipPostalCodeTo, country.TwoLetterIsoCode, "Air", weight, length, width, height));
                    shippingOptions.Add(RequestShippingOption(ZipPostalCodeFrom, ZipPostalCodeTo, country.TwoLetterIsoCode, "Sea", weight, length, width, height));
                    shippingOptions.Add(RequestShippingOption(ZipPostalCodeFrom, ZipPostalCodeTo, country.TwoLetterIsoCode, "ECI_D", weight, length, width, height));
                    shippingOptions.Add(RequestShippingOption(ZipPostalCodeFrom, ZipPostalCodeTo, country.TwoLetterIsoCode, "ECI_M", weight, length, width, height));
                    shippingOptions.Add(RequestShippingOption(ZipPostalCodeFrom, ZipPostalCodeTo, country.TwoLetterIsoCode, "EPI", weight, length, width, height));
                    break;
                }

                foreach (ShippingOption shippingOption in shippingOptions)
                {
                    shippingOption.Rate += AustraliaPostSettings.AdditionalHandlingCharge;
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return(shippingOptions);
        }
Ejemplo n.º 15
0
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="cart">Shopping cart</param>
        /// <param name="customer">Customer</param>
        /// <param name="shippingAddress">Shipping address</param>
        /// <param name="allowedShippingRateComputationMethodId">Allowed shipping rate computation method identifier; null to load shipping options of all methods</param>
        /// <param name="error">Error</param>
        /// <returns>Shipping options</returns>
        public static ShippingOptionCollection GetShippingOptions(ShoppingCart cart,
                                                                  Customer customer, Address shippingAddress,
                                                                  int?allowedShippingRateComputationMethodId, ref string error)
        {
            if (cart == null)
            {
                throw new ArgumentNullException("cart");
            }

            var shippingOptions = new ShippingOptionCollection();

            bool isFreeShipping = IsFreeShipping(cart, customer);

            var ShipmentPackage = CreateShipmentPackage(cart, customer, shippingAddress);
            var shippingRateComputationMethods = ShippingRateComputationMethodManager.GetAllShippingRateComputationMethods(false);

            if (shippingRateComputationMethods.Count == 0)
            {
                throw new NopException("Shipping rate computation method could not be loaded");
            }

            foreach (var srcm in shippingRateComputationMethods)
            {
                if (allowedShippingRateComputationMethodId.HasValue &&
                    allowedShippingRateComputationMethodId.Value > 0 &&
                    allowedShippingRateComputationMethodId.Value != srcm.ShippingRateComputationMethodId)
                {
                    continue;
                }

                var iShippingRateComputationMethod = Activator.CreateInstance(Type.GetType(srcm.ClassName)) as IShippingRateComputationMethod;

                var shippingOptions2 = iShippingRateComputationMethod.GetShippingOptions(ShipmentPackage, ref error);
                if (shippingOptions2 != null)
                {
                    foreach (var so2 in shippingOptions2)
                    {
                        so2.ShippingRateComputationMethodId = srcm.ShippingRateComputationMethodId;
                        shippingOptions.Add(so2);
                    }
                }
            }

            if (shippingOptions.Count == 0)
            {
                error = "Shipping options could not be loaded";
            }

            //additional shipping charges
            decimal additionalShippingCharge = GetShoppingCartAdditionalShippingCharge(cart, customer);

            shippingOptions.ForEach(so => so.Rate += additionalShippingCharge);

            //discounts
            foreach (var so in shippingOptions)
            {
                decimal rateWithoutDiscount = Math.Round(so.Rate, 2);
                decimal rateWithDiscount    = decimal.Zero;

                Discount rateDiscount       = null;
                decimal  rateDiscountAmount = GetShippingDiscount(customer, rateWithoutDiscount, out rateDiscount);
                rateWithDiscount = rateWithoutDiscount - rateDiscountAmount;
                if (rateWithDiscount < decimal.Zero)
                {
                    rateWithDiscount = decimal.Zero;
                }

                rateWithDiscount = Math.Round(rateWithDiscount, 2);

                so.Rate = rateWithDiscount;
                if (rateDiscount != null)
                {
                    so.AppliedDiscountId = rateDiscount.DiscountId;
                }
            }

            //free shipping
            if (isFreeShipping)
            {
                shippingOptions.ForEach(so => so.Rate = decimal.Zero);
            }

            return(shippingOptions);
        }
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="ShipmentPackage">Shipment package</param>
        /// <param name="Error">Error</param>
        /// <returns>Shipping options</returns>
        public ShippingOptionCollection GetShippingOptions(ShipmentPackage ShipmentPackage, ref string Error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            if (ShipmentPackage == null)
                throw new ArgumentNullException("ShipmentPackage");
            if (ShipmentPackage.Items == null)
                throw new NopException("No shipment items");
            if (ShipmentPackage.ShippingAddress == null)
            {
                Error = "Shipping address is not set";
                return shippingOptions;
            }
            if (ShipmentPackage.ShippingAddress.Country == null)
            {
                Error = "Shipping country is not set";
                return shippingOptions;
            }

            decimal subTotal = decimal.Zero;
            foreach (ShoppingCartItem shoppingCartItem in ShipmentPackage.Items)
            {
                if (shoppingCartItem.IsFreeShipping)
                    continue;
                subTotal += PriceHelper.GetSubTotal(shoppingCartItem, ShipmentPackage.Customer, true);
            }
            decimal weight = ShippingManager.GetShoppingCartTotalWeigth(ShipmentPackage.Items);

            ShippingMethodCollection shippingMethods = ShippingMethodManager.GetAllShippingMethods();
            foreach (ShippingMethod shippingMethod in shippingMethods)
            {
                ShippingOption shippingOption = new ShippingOption();
                shippingOption.Name = shippingMethod.Name;
                shippingOption.Description = shippingMethod.Description;
                shippingOption.Rate = GetRate(subTotal, weight, shippingMethod.ShippingMethodID, ShipmentPackage.ShippingAddress.Country.CountryID);
                shippingOptions.Add(shippingOption);
            }

            return shippingOptions;
        }
        /// <summary>
        ///  Gets available shipping options
        /// </summary>
        /// <param name="shipmentPackage">Shipment package</param>
        /// <param name="error">Error</param>
        /// <returns>Shipping options</returns>
        public ShippingOptionCollection GetShippingOptions(ShipmentPackage shipmentPackage, ref string error)
        {
            var shippingOptions = new ShippingOptionCollection();

            if (shipmentPackage == null)
            {
                throw new ArgumentNullException("shipmentPackage");
            }
            if (shipmentPackage.Items == null)
            {
                throw new NopException("No shipment items");
            }
            if (shipmentPackage.ShippingAddress == null)
            {
                error = "Shipping address is not set";
                return(shippingOptions);
            }
            if (shipmentPackage.ShippingAddress.Country == null)
            {
                error = "Shipping country is not set";
                return(shippingOptions);
            }
            if (shipmentPackage.ShippingAddress.StateProvince == null)
            {
                error = "Shipping state is not set";
                return(shippingOptions);
            }

            try
            {
                var profile = new Profile();
                //use "CPC_DEMO_XML" merchant ID for testing
                profile.MerchantId = SettingManager.GetSettingValue("ShippingRateComputationMethod.CanadaPost.CustomerID");

                var destination = new Destination();
                destination.City            = shipmentPackage.ShippingAddress.City;
                destination.StateOrProvince = shipmentPackage.ShippingAddress.StateProvince.Abbreviation;
                destination.Country         = shipmentPackage.ShippingAddress.Country.TwoLetterIsoCode;
                destination.PostalCode      = shipmentPackage.ShippingAddress.ZipPostalCode;

                var items = CreateItems(shipmentPackage);

                var lang = CanadaPostLanguageEnum.English;
                if (NopContext.Current.WorkingLanguage.LanguageCulture.ToLowerInvariant().StartsWith("fr"))
                {
                    lang = CanadaPostLanguageEnum.French;
                }

                var requestResult = GetShippingOptionsInternal(profile, destination, items, lang);
                if (requestResult.IsError)
                {
                    error = requestResult.StatusMessage;
                }
                else
                {
                    foreach (var dr in requestResult.AvailableRates)
                    {
                        var so = new ShippingOption();
                        so.Name = dr.Name;
                        if (!string.IsNullOrEmpty(dr.DeliveryDate))
                        {
                            so.Name += string.Format(" - {0}", dr.DeliveryDate);
                        }
                        so.Rate = dr.Amount;
                        shippingOptions.Add(so);
                    }
                }

                foreach (var shippingOption in shippingOptions)
                {
                    if (!shippingOption.Name.ToLower().StartsWith("canada post"))
                    {
                        shippingOption.Name = string.Format("Canada Post {0}", shippingOption.Name);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                error = e.Message;
            }

            return(shippingOptions);
        }
Ejemplo n.º 18
0
        public ShippingOptionCollection GetShippingOptions(ShipmentPackage shipmentPackage, ref string error)
        {
            var shippingOptions = new ShippingOptionCollection();

            if (shipmentPackage == null)
            {
                throw new ArgumentNullException("shipmentPackage");
            }
            if (shipmentPackage.Items == null)
            {
                throw new NopException("No shipment items");
            }
            if (shipmentPackage.ShippingAddress == null)
            {
                error = "Shipping address is not set";
                return(shippingOptions);
            }

            string  url                      = SettingManager.GetSettingValue("ShippingRateComputationMethod.DHL.URL");
            string  siteid                   = SettingManager.GetSettingValue("ShippingRateComputationMethod.DHL.SiteID");
            string  password                 = SettingManager.GetSettingValue("ShippingRateComputationMethod.DHL.Password");
            string  countryFrom              = SettingManager.GetSettingValue("ShippingRateComputationMethod.DHL.ShippingCountry");
            string  cityFrom                 = SettingManager.GetSettingValue("ShippingRateComputationMethod.DHL.ShippingCity");
            string  divisionFrom             = SettingManager.GetSettingValue("ShippingRateComputationMethod.DHL.ShippingDivision");
            string  postalCodeFrom           = SettingManager.GetSettingValue("ShippingRateComputationMethod.DHL.DefaultShippedFromZipPostalCode");
            decimal additionalHandlingCharge = SettingManager.GetSettingValueDecimalNative("ShippingRateComputationMethod.DHL.AdditionalHandlingCharge");

            shipmentPackage.CountryFrom       = CountryManager.GetCountryByTwoLetterIsoCode(SettingManager.GetSettingValue("ShippingRateComputationMethod.DHL.ShippingCountry"));
            shipmentPackage.ZipPostalCodeFrom = postalCodeFrom;

            string requestStringWithTokens = CreateRequest(siteid, password, cityFrom, divisionFrom, shipmentPackage);
            bool   isDomestic = IsDomestic(shipmentPackage);

            string carrierServices = string.Empty;

            if (isDomestic)
            {
                carrierServices = SettingManager.GetSettingValue("ShippingRateComputationMethod.DHL.DomesticServices");

                foreach (DHLService service in domesticSeviceList)
                {
                    if (carrierServices.Contains(service.ServiceID))
                    {
                        string requestString  = requestStringWithTokens.Replace(ProductCodeToken, service.ServiceID);
                        string responseXML    = DoRequest(url, requestString);
                        var    shippingOption = ParseResponse(responseXML, ref error);
                        if (shippingOption != null)
                        {
                            shippingOption.Name  = string.Format("DHL {0}", GetServiceName(service.ServiceID, true));
                            shippingOption.Rate += additionalHandlingCharge;
                            shippingOptions.Add(shippingOption);
                        }
                    }
                }
            }
            else
            {
                carrierServices = SettingManager.GetSettingValue("ShippingRateComputationMethod.DHL.InternationalServices");
                foreach (DHLService service in intlServiceList)
                {
                    if (carrierServices.Contains(service.ServiceID))
                    {
                        string requestString  = requestStringWithTokens.Replace(ProductCodeToken, service.ServiceID);
                        string responseXML    = DoRequest(url, requestString);
                        var    shippingOption = ParseResponse(responseXML, ref error);
                        if (shippingOption != null)
                        {
                            shippingOption.Name  = string.Format("DHL {0}", GetServiceName(service.ServiceID, false));
                            shippingOption.Rate += additionalHandlingCharge;
                            shippingOptions.Add(shippingOption);
                        }
                    }
                }
            }
            return(shippingOptions);
        }
        private ShippingOptionCollection ParseResponse(string response, ref string error)
        {
            ShippingOptionCollection shippingOptions = new ShippingOptionCollection();

            using (StringReader sr = new StringReader(response))
                using (XmlTextReader tr = new XmlTextReader(sr))


                    do
                    // while (tr.Read())
                    {
                        // Read the next XML record
                        tr.Read();

                        if ((tr.Name == "Error") && (tr.NodeType == XmlNodeType.Element))
                        {
                            string errorText = "";
                            while (tr.Read())
                            {
                                if ((tr.Name == "Description") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    errorText += "Error Desc: " + tr.ReadString();
                                }
                                if ((tr.Name == "HelpContext") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    errorText += "USPS Help Context: " + tr.ReadString() + ". ";
                                }
                            }
                            error = "USPS Error returned: " + errorText;
                        }

                        // Process the inner postage XML
                        if ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element))
                        {
                            string serviceCode = "";
                            string postalRate  = "";

                            // while (tr.Read())
                            do
                            {
                                tr.Read();

                                if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    serviceCode = tr.ReadString();

                                    tr.ReadEndElement();
                                    if ((tr.Name == "MailService") && (tr.NodeType == XmlNodeType.EndElement))
                                    {
                                        break;
                                    }
                                }
                                // if (((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)) || ((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.Element)))
                                //   break;

                                if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.Element))
                                {
                                    postalRate = tr.ReadString();
                                    tr.ReadEndElement();
                                    if ((tr.Name == "Rate") && (tr.NodeType == XmlNodeType.EndElement))
                                    {
                                        break;
                                    }
                                }
                            } while (!((tr.Name == "Postage") && (tr.NodeType == XmlNodeType.EndElement)));

                            if (shippingOptions.Find((s) => s.Name == serviceCode) == null)
                            {
                                ShippingOption shippingOption = new ShippingOption();
                                //TODO check whether we need to multiply rate by package quantity
                                shippingOption.Rate = Convert.ToDecimal(postalRate, new CultureInfo("en-US"));
                                shippingOption.Name = serviceCode;
                                shippingOptions.Add(shippingOption);
                            }
                        }
                    } while (!tr.EOF);
            return(shippingOptions);
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Gets the shipping options.
 /// </summary>
 /// <param name="order">The order.</param>
 /// <returns></returns>
 public ShippingOptionCollection GetShippingOptions(Order order)
 {
     ShippingOptionCollection shippingOptionCollection = new ShippingOptionCollection();
       ShippingOptionCollection serviceOptionCollection;
       foreach(IShippingProvider shippingProvider in _shippingProviderCollection) {
     serviceOptionCollection = shippingProvider.GetShippingOptions(order);
     if(this.ShippingServiceSettings.ShippingBuffer > 0) {
       foreach(ShippingOption shippingOption in serviceOptionCollection) {
     shippingOption.Rate = shippingOption.Rate + this.ShippingServiceSettings.ShippingBuffer;
       }
     }
     shippingOptionCollection.Add(serviceOptionCollection);
       }
       return shippingOptionCollection;
 }
        private ShippingOptionCollection ParseResponse(RateReply reply)
        {
            ShippingOptionCollection result = new ShippingOptionCollection();

            Debug.WriteLine("RateReply details:");
            Debug.WriteLine("**********************************************************");
            foreach (RateReplyDetail rateDetail in reply.RateReplyDetails)
            {
                Debug.WriteLine("ServiceType: " + rateDetail.ServiceType);
                for (int i = 0; i < rateDetail.RatedShipmentDetails.Length; i++)
                {
                    RatedShipmentDetail shipmentDetail = rateDetail.RatedShipmentDetails[i];
                    Debug.WriteLine("RateType : " + shipmentDetail.ShipmentRateDetail.RateType);
                    Debug.WriteLine("Total Billing Weight : " + shipmentDetail.ShipmentRateDetail.TotalBillingWeight.Value);
                    Debug.WriteLine("Total Base Charge : " + shipmentDetail.ShipmentRateDetail.TotalBaseCharge.Amount);
                    Debug.WriteLine("Total Discount : " + shipmentDetail.ShipmentRateDetail.TotalFreightDiscounts.Amount);
                    Debug.WriteLine("Total Surcharges : " + shipmentDetail.ShipmentRateDetail.TotalSurcharges.Amount);
                    Debug.WriteLine("Net Charge : " + shipmentDetail.ShipmentRateDetail.TotalNetCharge.Amount);
                    Debug.WriteLine("*********");

                    //take first one
                    if (i == 0)
                    {
                        ShippingOption shippingOption = new ShippingOption();
                        string userFriendlyServiceType = GetUserFriendlyEnum(rateDetail.ServiceType.ToString());
                        shippingOption.Name = userFriendlyServiceType;
                        shippingOption.Rate = shipmentDetail.ShipmentRateDetail.TotalNetCharge.Amount;
                        result.Add(shippingOption);
                    }
                }
                Debug.WriteLine("**********************************************************");
            }

            return result;
        }