Exemplo n.º 1
0
        public Attempt<IShipmentRateQuote> CalculatePrice(IShipment shipment, IShipMethod shipMethod, decimal totalWeight, int quantity, IShipProvince province)
        {
            // First sum up the total weight for the shipment.
            // We're assumning that a custom order line property 
            // was set on the order line prior when the product was added to the order line.

            var shippingPrice = 0M;
            try
            {
                var service = new RateService {Url = "https://wsbeta.fedex.com:443/web-services/rate"};
                
                var reply = service.getRates(RateRequest(shipment, totalWeight, quantity));
                                                            
                if (reply.HighestSeverity == NotificationSeverityType.SUCCESS || reply.HighestSeverity == NotificationSeverityType.NOTE || reply.HighestSeverity == NotificationSeverityType.WARNING)
                {
                    var collection = BuildDeliveryOptions(reply, shipment);

                    var firstCarrierRate = collection.FirstOrDefault(option => option.Service.Contains(shipMethod.ServiceCode.Split('-').First()));
                    if (firstCarrierRate != null)
                        shippingPrice = firstCarrierRate.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 });
        }
Exemplo n.º 2
0
        public override Attempt<IShipmentRateQuote> QuoteShipment(IShipment shipment)
        {
            try
            {
                // TODO this should be made configurable
                var visitor = new FedExShipmentLineItemVisitor() { UseOnSalePriceIfOnSale = false };

                shipment.Items.Accept(visitor);

                var province = ShipMethod.Provinces.FirstOrDefault(x => x.Code == shipment.ToRegion);

                var shippingPrice = 0M;
                try
                {
                    var service = new RateService { Url = "https://wsbeta.fedex.com:443/web-services/rate" };

                    var collection = GetCollectionFromCache(shipment);
                    if (collection == null)
                    {
                        var reply = service.getRates(RateRequest(shipment, visitor.TotalWeight, visitor.Quantity));



                        if (reply.HighestSeverity == NotificationSeverityType.SUCCESS || reply.HighestSeverity == NotificationSeverityType.NOTE || reply.HighestSeverity == NotificationSeverityType.WARNING)
                        {
                            collection = BuildDeliveryOptions(reply, shipment);

                            _runtimeCache.InsertCacheItem(MakeCacheKey(shipment), () => collection);       
                        }
                    }

                    var firstCarrierRate = collection.FirstOrDefault(option => option.Service.Contains(_shipMethod.ServiceCode));
                    if (firstCarrierRate != null)
                        shippingPrice = firstCarrierRate.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 });
            }
            catch (Exception ex)
            {
                return Attempt<IShipmentRateQuote>.Fail(
                           new Exception("An error occured during your request : " +
                                                        ex.Message +
                                                        " Please contact your administrator or try again."));
            }
        }