コード例 #1
0
ファイル: Edost.cs プロジェクト: gkovalev/nastia
        private List <ShippingOption> ParseAnswer(string responseFromServer)
        {
            var shippingOptions = new List <ShippingOption>();

            if (String.IsNullOrEmpty(responseFromServer))
            {
                return(shippingOptions);
            }

            using (var sr = new StringReader(responseFromServer))
                using (var reader = new XmlTextReader(sr))
                {
                    bool flagStartTagStat         = false;
                    bool flagStartTagTarif        = false;
                    bool flagStartTagPrice        = false;
                    bool flagStartTagPriceCash    = false;
                    bool flagStartTagTransfer     = false;
                    bool flagStartTagDay          = false;
                    bool flagStartTagCompany      = false;
                    bool flagStartTagName         = false;
                    bool flagStartTagPickpointmap = false;


                    string price        = string.Empty;
                    string priceCash    = string.Empty;
                    string transfer     = string.Empty;
                    string company      = string.Empty;
                    string name         = string.Empty;
                    string day          = string.Empty;
                    string pickpointmap = string.Empty;


                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Element:
                            if (reader.Name == "stat")
                            {
                                flagStartTagStat = true;
                            }

                            if (reader.Name == "tarif")
                            {
                                flagStartTagTarif = true;
                            }

                            if (reader.Name == "price")
                            {
                                flagStartTagPrice = true;
                            }

                            if (reader.Name == "pricecash")
                            {
                                flagStartTagPriceCash = true;
                            }

                            if (reader.Name == "transfer")
                            {
                                flagStartTagTransfer = true;
                            }
                            if (reader.Name == "pickpointmap")
                            {
                                flagStartTagPickpointmap = true;
                            }

                            if (reader.Name == "day")
                            {
                                flagStartTagDay = true;
                            }

                            if (reader.Name == "company")
                            {
                                flagStartTagCompany = true;
                            }

                            if (reader.Name == "name")
                            {
                                flagStartTagName = true;
                            }
                            break;

                        case XmlNodeType.Text:
                            if (flagStartTagStat)
                            {
                                flagStartTagStat = false;
                                if (reader.Value != "1")
                                {
                                    GetErrorEdost(reader.Value);
                                    return(shippingOptions);
                                }
                            }
                            if (flagStartTagTarif && flagStartTagPrice)
                            {
                                price = reader.Value;
                            }

                            if (flagStartTagTarif && flagStartTagPriceCash)
                            {
                                priceCash = reader.Value;
                            }

                            if (flagStartTagTarif && flagStartTagTransfer)
                            {
                                transfer = reader.Value;
                            }

                            if (flagStartTagTarif && flagStartTagPickpointmap)
                            {
                                pickpointmap = reader.Value;
                            }

                            if (flagStartTagTarif && flagStartTagDay)
                            {
                                day = reader.Value;
                            }

                            if (flagStartTagTarif && flagStartTagCompany)
                            {
                                company = reader.Value;
                            }

                            if (flagStartTagTarif && flagStartTagName)
                            {
                                name = reader.Value;
                            }

                            break;

                        case XmlNodeType.EndElement:
                            if (reader.Name == "stat")
                            {
                                flagStartTagStat = false;
                            }

                            if (reader.Name == "tarif")
                            {
                                flagStartTagTarif = false;
                                decimal rate         = price.TryParseDecimal() * Rate;
                                decimal cash         = priceCash.TryParseDecimal() * Rate;
                                decimal cashTransfer = transfer.TryParseDecimal() * Rate;

                                var shippingOption = new ShippingOption
                                {
                                    Name         = company + (string.IsNullOrWhiteSpace(name) ? string.Empty : " (" + name + ")"),
                                    Rate         = rate,
                                    DeliveryTime = day,
                                    // extension наложеный платеж или пикпойнт
                                    Extend = priceCash.IsNotEmpty() && transfer.IsNotEmpty() ? GetExtended(rate, cash, cashTransfer, pickpointmap) : null
                                };
                                if (String.IsNullOrEmpty(pickpointmap))
                                {
                                    shippingOptions.Add(shippingOption);
                                }
                                else
                                {
                                    shippingOptions.Insert(0, shippingOption);
                                }

                                price        = string.Empty;
                                priceCash    = string.Empty;
                                transfer     = string.Empty;
                                company      = string.Empty;
                                name         = string.Empty;
                                day          = string.Empty;
                                pickpointmap = string.Empty;
                            }

                            if (reader.Name == "price")
                            {
                                flagStartTagPrice = false;
                            }

                            if (reader.Name == "pricecash")
                            {
                                flagStartTagPriceCash = false;
                            }

                            if (reader.Name == "transfer")
                            {
                                flagStartTagTransfer = false;
                            }

                            if (reader.Name == "pickpointmap")
                            {
                                flagStartTagPickpointmap = false;
                            }

                            if (reader.Name == "day")
                            {
                                flagStartTagDay = false;
                            }

                            if (reader.Name == "company")
                            {
                                flagStartTagCompany = false;
                            }

                            if (reader.Name == "name")
                            {
                                flagStartTagName = false;
                            }
                            break;
                        }
                    }
                }
            return(shippingOptions);
        }
コード例 #2
0
ファイル: Usps.cs プロジェクト: olegp306/ToyCorp2017
        private List <ShippingOption> ParseResponseMessage(string response)
        {
            string serviceTag     = "Service";
            string serviceNameTag = "SvcDescription";
            string rateTag        = "Postage";

            if (_isDomenic)
            {
                serviceTag     = "Postage";
                serviceNameTag = "MailService";
                rateTag        = "Rate";
            }

            var shippingOptions = new List <ShippingOption>();

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

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

                            do
                            {
                                tr.Read();

                                if ((tr.Name == serviceNameTag) && (tr.NodeType == XmlNodeType.Element))
                                {
                                    serviceCode = tr.ReadString().Replace("**", "");
                                    int idx1 = serviceCode.IndexOf("&lt;sup&gt;");
                                    int idx2 = serviceCode.IndexOf("&lt;/sup&gt;") + 12;

                                    if (idx1 >= 0)
                                    {
                                        serviceCode = serviceCode.Remove(idx1, idx2 - idx1);
                                    }

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

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

                            if ((EnabledService.Contains(serviceCode)) && (shippingOptions.Find(s => s.Name == serviceCode) == null))
                            {
                                var shippingRate = (Rate > 0) ? postalRate.TryParseFloat() * Rate + Extracharge
                                                          : postalRate.TryParseFloat() + Extracharge;
                                //if (Rate > 0)
                                //{
                                //    shippingRate *= Rate;
                                //}

                                var shippingOption = new ShippingOption
                                {
                                    Rate = shippingRate,
                                    Name = serviceCode
                                };
                                shippingOptions.Add(shippingOption);
                            }
                        }
                    }while (!tr.EOF);
                }
            return(shippingOptions);
        }
コード例 #3
0
ファイル: FedEx.cs プロジェクト: AzarinSergey/learn
        private List<ShippingOption> ParseAnswer(RateReply reply)
        {
            var res = new List<ShippingOption>();
            var enabledServices = EnabledService;
            foreach (var rateDetail in reply.RateReplyDetails)
            {
                var shippingOption = new ShippingOption();
                if (!String.IsNullOrEmpty(enabledServices) && !enabledServices.Contains(rateDetail.ServiceType.ToString()))
                {
                    continue;
                }
                string serviceName = GetServiceName(rateDetail.ServiceType.ToString());
                shippingOption.Name = serviceName;
                foreach (RatedShipmentDetail shipmentDetail in rateDetail.RatedShipmentDetails)
                {
                    shippingOption.Rate = Rate > 0 ? (float)shipmentDetail.ShipmentRateDetail.TotalNetCharge.Amount * Rate + Extracharge
                                                   : (float)shipmentDetail.ShipmentRateDetail.TotalNetCharge.Amount + Extracharge;

                    // Vladimir: Старый код вытаскивал только некоторые Rate. Не знаю зачем. Пусть будут все.
                    //if (shipmentDetail.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_ACCOUNT_SHIPMENT)
                    //{
                    //    shippingOption.Rate = shipmentDetail.ShipmentRateDetail.TotalNetCharge.Amount + Extracharge;
                    //    if (Rate > 0)
                    //    {
                    //        shippingOption.Rate *= Rate;
                    //    }
                    //    break;
                    //}

                    //if (shipmentDetail.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_LIST_SHIPMENT) // Get List Rates (not discount rates)
                    //{
                    //    shippingOption.Rate = Rate > 0 ? shipmentDetail.ShipmentRateDetail.TotalNetCharge.Amount * Rate + Extracharge
                    //                                   : shipmentDetail.ShipmentRateDetail.TotalNetCharge.Amount + Extracharge;
                    //    break;
                    //}

                    //var shippingRate = shipmentDetail.ShipmentRateDetail.TotalNetCharge.Amount + Extracharge;
                    //if (Rate > 0)
                    //{
                    //    shippingRate *= Rate;
                    //}
                    //shippingOption.Rate = shippingRate;
                }
                res.Add(shippingOption);
            }
            return res;
        }
コード例 #4
0
        private List <ShippingOption> ParseAnswer(string responseFromServer)
        {
            if (responseFromServer.IsNullOrEmpty())
            {
                return(new List <ShippingOption>());
            }

            var shippingOptions = new List <ShippingOption>();

            var doc = XDocument.Parse(responseFromServer);

            if (doc.Root == null)
            {
                return(shippingOptions);
            }

            var status = doc.Root.Element("stat");

            if (status != null && status.Value != "1")
            {
                GetErrorEdost(status.Value, CityTo);
                return(shippingOptions);
            }

            foreach (var el in doc.Root.Elements("tarif"))
            {
                var idEl            = el.Element("id");
                var priceEl         = el.Element("price");
                var priceCashEl     = el.Element("pricecash");
                var priceTransferEl = el.Element("transfer");
                var nameEl          = el.Element("name");
                var pickpointMapEl  = el.Element("pickpointmap");
                var companyEl       = el.Element("company");
                var dayEl           = el.Element("day");

                if (idEl == null || priceEl == null || nameEl == null || companyEl == null)
                {
                    continue;
                }

                var price     = priceEl.Value.TryParseFloat() * Rate;
                var priceCash = priceCashEl != null?priceCashEl.Value.TryParseFloat() * Rate : 0;

                var priceTransfer = priceTransferEl != null?priceTransferEl.Value.TryParseFloat() * Rate : 0;

                var pickpointmap = pickpointMapEl != null ? pickpointMapEl.Value : string.Empty;

                var shippingOption = new ShippingOption
                {
                    ShippingMethodId = Convert.ToInt32(idEl.Value),
                    Name             = companyEl.Value + (string.IsNullOrWhiteSpace(nameEl.Value) ? string.Empty : " (" + nameEl.Value + ")"),
                    Rate             = price,
                    DeliveryTime     = dayEl.Value,
                    // extension наложеный платеж или пикпойнт
                    Extend =
                        priceCash != 0 || priceTransfer != 0 || pickpointmap.IsNotEmpty()
                            ? GetExtended(price, priceCash, priceTransfer, pickpointmap)
                            : null
                };

                if (pickpointmap.IsNotEmpty())
                {
                    shippingOptions.Insert(0, shippingOption);
                }
                else
                {
                    shippingOptions.Add(shippingOption);
                }
            }

            bool first = true;

            foreach (var el in doc.Root.Elements("office"))
            {
                var name        = el.Element("name");
                var addressEl   = el.Element("address");
                var telEl       = el.Element("tel");
                var schelduleEl = el.Element("schedule");
                var tarifId     = el.Element("to_tarif").Value.TryParseFloat();

                var point = new ShippingPoint()
                {
                    Id          = Convert.ToInt32(el.Element("id").Value),
                    Address     = name.Value,
                    Description = string.Format("<div>{0}</div> <div>{1}</div> <div>{2}</div>",
                                                addressEl.Value, telEl.Value, schelduleEl.Value)
                };

                var option = shippingOptions.Find(o => o.ShippingMethodId == tarifId);
                if (option != null)
                {
                    option.ShippingPoints.Add(point);

                    if (first || point.Id == PickPointID)
                    {
                        if (option.Extend == null)
                        {
                            option.Extend = new ShippingOptionEx();
                        }

                        option.Extend.PickpointId      = point.Id.ToString();
                        option.Extend.PickpointAddress = point.Address;
                        option.Extend.Type            |= option.Name.ToLower().Contains("boxberry")
                            ? ExtendedType.Boxberry
                            : option.Name.ToLower().Contains("сдэк") ? ExtendedType.Cdek : ExtendedType.CashOnDelivery;
                        option.Extend.AdditionalData = point.Description;
                    }
                }
                first = false;
            }



            return(shippingOptions);
        }
コード例 #5
0
ファイル: Cdek.cs プロジェクト: olegp306/ToyCorp2017
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public List <ShippingOption> GetShippingOptions()
        {
            if (string.IsNullOrEmpty(CityTo) || string.IsNullOrEmpty(CityFrom))
            {
                return(new List <ShippingOption>());
            }

            var cdekCityToId   = GetCdekCityId(CityTo);
            var cdekCityFromId = GetCdekCityId(CityFrom);

            var listShippingOptions = new List <ShippingOption>();

            var dateExecute = DateTime.Now.Date.ToString("yyyy-MM-dd");

            var goods = new List <CdekGoods>();

            foreach (var item in ShoppingCart)
            {
                var currentProduct = ProductService.GetProduct(item.Offer.ProductId);
                if (currentProduct != null)
                {
                    var sizes = currentProduct.Size.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    if (sizes.Count() == 3)
                    {
                        var length = Math.Round(Convert.ToSingle(sizes[0]) / 10);
                        var width  = Math.Round(Convert.ToSingle(sizes[1]) / 10);
                        var height = Math.Round(Convert.ToSingle(sizes[2]) / 10);

                        goods.Add(new CdekGoods
                        {
                            weight =
                                (currentProduct.Weight * item.Amount) == 0
                                    ? "0.001"
                                    : (currentProduct.Weight * item.Amount).ToString().Replace(",", "."),
                            length = length > 0 ? length.ToString() : "1",
                            width  = width > 0 ? width.ToString() : "1",
                            height = height > 0 ? height.ToString() : "1",
                        });
                    }
                    else
                    {
                        goods.Add(new CdekGoods
                        {
                            weight =
                                (currentProduct.Weight * item.Amount) == 0
                                    ? "0.001"
                                    : (currentProduct.Weight * item.Amount).ToString().Replace(",", "."),
                            length = "1",
                            width  = "1",
                            height = "1",
                        });
                    }
                }
            }

            var listShippingPoints = new List <ShippingPoint>();
            int index = 0;

            foreach (var cdekPoint in GetListOfCityPoints(cdekCityToId))
            {
                listShippingPoints.Add(new ShippingPoint
                {
                    Id          = index,
                    Code        = cdekPoint.Code,
                    Address     = cdekPoint.Address,
                    Description = cdekPoint.Note + " " + cdekPoint.WorkTime
                });
                ++index;
            }

            foreach (var cdekTarrifId in ActiveTariffs.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                var cdekTariff = tariffs.FirstOrDefault(item => string.Equals(item.tariffId.ToString(), cdekTarrifId));
                if (cdekTariff == null)
                {
                    continue;
                }

                var jsonData = JsonConvert.SerializeObject(
                    new
                {
                    version        = "1.0",
                    dateExecute    = dateExecute,
                    authLogin      = AuthLogin,
                    secure         = GetMd5Hash(MD5.Create(), dateExecute + "&" + AuthPassword),
                    senderCityId   = cdekCityFromId.ToString(),
                    receiverCityId = cdekCityToId.ToString(),
                    tariffId       = cdekTariff.tariffId,
                    goods          = goods
                });

                var responseResult = PostRequestGetString(_urlCalculatePrice, jsonData, "application/json");

                var cdekAnswer = JsonConvert.DeserializeObject <CdekResponse>(responseResult);
                if (cdekAnswer.error == null)
                {
                    var shippingOptionRate = cdekAnswer.result.priceByCurrency + AdditionalPrice;
                    var shippingOption     = new ShippingOption
                    {
                        Rate           = shippingOptionRate < 0 ? 0 : shippingOptionRate,
                        Name           = MethodName,
                        DeliveryTime   = cdekAnswer.result.deliveryPeriodMin + "-" + cdekAnswer.result.deliveryPeriodMax + " дней",
                        ShippingPoints = cdekTariff.mode.EndsWith("-Д") ? null : listShippingPoints,
                        Extend         = new ShippingOptionEx
                        {
                            AdditionalData = cdekAnswer.result.tariffId.ToString(),
                            Type           = ExtendedType.Cdek
                        }
                    };
                    listShippingOptions.Add(shippingOption);
                }
                else
                {
                    var errors = cdekAnswer.error.Aggregate("", (current, error) => current + (error.code + " " + error.text));
                    Debug.LogError(new Exception("Cdek: " + errors), false);
                }
            }

            return(listShippingOptions);
        }
コード例 #6
0
ファイル: UPS.cs プロジェクト: AzarinSergey/learn
        private List<ShippingOption> ParseAnswer(RateResponse rateResponse)
        {
            var res = new List<ShippingOption>();
            var enabledServices = EnabledService;
            foreach (var rateDetail in rateResponse.RatedShipment)
            {
                var shippingOption = new ShippingOption();
                if (!String.IsNullOrEmpty(enabledServices) && !enabledServices.Contains(GetServiceName(rateDetail.Service.Code)))
                {
                    continue;
                }

                string serviceName = GetServiceName(rateDetail.Service.Code);
                shippingOption.Name = serviceName;

                var shippingRate = (Rate > 0) ? rateDetail.TotalCharges.MonetaryValue.TryParseFloat() * Rate + Extracharge
                                              : rateDetail.TotalCharges.MonetaryValue.TryParseFloat() + Extracharge;
                shippingOption.Rate = shippingRate;
                res.Add(shippingOption);
            }
            return res;
        }
コード例 #7
0
ファイル: Usps.cs プロジェクト: AzarinSergey/learn
        private List<ShippingOption> ParseResponseMessage(string response)
        {
            string serviceTag = "Service";
            string serviceNameTag = "SvcDescription";
            string rateTag = "Postage";

            if (_isDomenic)
            {
                serviceTag = "Postage";
                serviceNameTag = "MailService";
                rateTag = "Rate";
            }

            var shippingOptions = new List<ShippingOption>();
            using (var sr = new StringReader(response))
            using (var tr = new XmlTextReader(sr))
            {
                do
                {
                    tr.Read();

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

                        do
                        {
                            tr.Read();

                            if ((tr.Name == serviceNameTag) && (tr.NodeType == XmlNodeType.Element))
                            {
                                serviceCode = tr.ReadString().Replace("**", "");
                                int idx1 = serviceCode.IndexOf("&lt;sup&gt;");
                                int idx2 = serviceCode.IndexOf("&lt;/sup&gt;") + 12;

                                if (idx1 >= 0)
                                {
                                    serviceCode = serviceCode.Remove(idx1, idx2 - idx1);
                                }

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

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

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

                        if ((EnabledService.Contains(serviceCode)) && (shippingOptions.Find(s => s.Name == serviceCode) == null))
                        {
                            var shippingRate = (Rate > 0) ? postalRate.TryParseFloat() * Rate + Extracharge
                                                          : postalRate.TryParseFloat() + Extracharge;
                            //if (Rate > 0)
                            //{
                            //    shippingRate *= Rate;
                            //}

                            var shippingOption = new ShippingOption
                                                     {
                                                         Rate = shippingRate,
                                                         Name = serviceCode
                                                     };
                            shippingOptions.Add(shippingOption);
                        }
                    }
                }
                while (!tr.EOF);
            }
            return shippingOptions;
        }