예제 #1
0
        public static IEnumerable <ShippingRule> ToModel(this RateResponse response, Currency currency)
        {
            response.ValidateResponse();

            var rules = new List <ShippingRule>();

            var order = response.Order[0];

            foreach (var quote in order.Quotes)
            {
                var method = new ShippingRule
                {
                    id       = quote.method,
                    name     = quote.Service,
                    cost     = quote.Cost.value.ToString(),
                    currency = quote.Cost.currency
                };
                if (currency.code != "USD")
                {
                    method.cost =
                        CurrencyConverter.Instance.Convert("USD", currency.code, quote.Cost.value).ToString("n" +
                                                                                                            currency.
                                                                                                            decimalCount);
                }

                var min      = quote.DeliveryEstimate.Minimum.value;
                var max      = quote.DeliveryEstimate.Maximum.value;
                var timeunit = quote.DeliveryEstimate.Minimum.units;
                if (timeunit == "days")
                {
                    timeunit = "day";
                }
                else
                {
                    Syslog.Write(string.Format("Unknown timeunit: {0}", timeunit));
                }

                if (min == max)
                {
                    if (min == 1)
                    {
                        method.deliveryEstimate = string.Format("1 {0}", timeunit.Substring(0, timeunit.Length));
                    }
                    else
                    {
                        method.deliveryEstimate = string.Format("{0} {1}s", min, timeunit);
                    }
                }
                else
                {
                    method.deliveryEstimate = string.Format("{0} to {1} {2}s", min, max, timeunit);
                }

                rules.Add(method);
            }
            return(rules);
        }
예제 #2
0
        public static Cost GetCost(this RateResponse response, string shippingMethod)
        {
            response.ValidateResponse();

            var order = response.Order[0];
            var quote = order.Quotes.SingleOrDefault(x => x.method == shippingMethod);

            if (quote == null)
            {
                Syslog.Write("Unable to find quote for method " + shippingMethod);
                return(null);
            }

            return(quote.Cost);
        }