public override Attempt <IShipmentRateQuote> QuoteShipment(IShipment shipment)
        {
            try
            {
                // TODO this should be made configurable
                var visitor = new UPSShipmentLineItemVisitor()
                {
                    UseOnSalePriceIfOnSale = false
                };

                shipment.Items.Accept(visitor);

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

                var quote = _processor.CalculatePrice(shipment, _shipMethod, visitor.TotalWeight, province);
                quote.Result.Rate = AdjustedRate(quote.Result.Rate, province);
                return(quote);
            }
            catch (Exception ex)
            {
                return(Attempt <IShipmentRateQuote> .Fail(
                           new Exception("An error occured during your request : " +
                                         ex.Message +
                                         " Please contact your administrator or try again.")));
            }
        }
        public override Attempt <IShipmentRateQuote> QuoteShipment(IShipment shipment)
        {
            try
            {
                // TODO this should be made configurable
                var visitor = new UPSShipmentLineItemVisitor()
                {
                    UseOnSalePriceIfOnSale = false
                };

                shipment.Items.Accept(visitor);

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

                var collection = GetCollectionFromCache(shipment);

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

                        collection = UpsParseRates(rateXml);

                        _runtimeCache.InsertCacheItem(MakeCacheKey(shipment), () => collection);
                    }
                    catch (Exception ex)
                    {
                        return(Attempt <IShipmentRateQuote> .Fail(
                                   new Exception("An error occured during your request : " +
                                                 ex.Message +
                                                 " Please contact your administrator or try again.")));
                    }
                }
                var shippingPrice = 0M;

                var firstCarrierRate = collection.FirstOrDefault(option => option.Service == _shipMethod.ServiceCode);
                if (firstCarrierRate != null)
                {
                    shippingPrice = firstCarrierRate.Rate;
                }

                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.")));
            }
        }
Пример #3
0
        public override Attempt<IShipmentRateQuote> QuoteShipment(IShipment shipment)
        {
            try
            {
                // TODO this should be made configurable
                var visitor = new UPSShipmentLineItemVisitor() { UseOnSalePriceIfOnSale = false };

                shipment.Items.Accept(visitor);

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

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

                        collection = UpsParseRates(rateXml);

                        _runtimeCache.InsertCacheItem(MakeCacheKey(shipment), () => collection);  
                    }
                    catch (Exception ex)
                    {
                        return Attempt<IShipmentRateQuote>.Fail(
                            new Exception("An error occured during your request : " +
                                          ex.Message +
                                          " Please contact your administrator or try again."));
                    }

                }
                var shippingPrice = 0M;

                var firstCarrierRate = collection.FirstOrDefault(option => option.Service == _shipMethod.ServiceCode);
                if (firstCarrierRate != null)
                    shippingPrice = firstCarrierRate.Rate;
                
                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."));
            }
        }
Пример #4
0
        public override Attempt<IShipmentRateQuote> QuoteShipment(IShipment shipment)
        {
            try
            {
                // TODO this should be made configurable
                var visitor = new UPSShipmentLineItemVisitor() { UseOnSalePriceIfOnSale = false };

                shipment.Items.Accept(visitor);

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

                var quote = _processor.CalculatePrice(shipment, _shipMethod, visitor.TotalWeight, province);
                quote.Result.Rate = AdjustedRate(quote.Result.Rate, province);
                return quote;
            }
            catch (Exception ex)
            {
                return Attempt<IShipmentRateQuote>.Fail(
                           new Exception("An error occured during your request : " +
                                                        ex.Message +
                                                        " Please contact your administrator or try again."));
            }
        }
Пример #5
0
        public override Attempt<IShipmentRateQuote> QuoteShipment(IShipment shipment)
        {
            try
            {
                // TODO this should be made configurable
                var visitor = new UPSShipmentLineItemVisitor() { UseOnSalePriceIfOnSale = false };

                shipment.Items.Accept(visitor);

                var province = ShipMethod.Provinces.FirstOrDefault(x => x.Code == shipment.ToRegion);
                                          
                // JASON: Removing Cache check as the strategy handles this. (However, the Provider is getting quotes to know what methods to return...)
                var collection = GetCollectionFromCache(shipment);

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

                        collection = UpsParseRates(rateXml);

                        //_runtimeCache.InsertCacheItem(MakeCacheKey(shipment), () => collection);  
                    }
                    catch (Exception ex)
                    {
                        return Attempt<IShipmentRateQuote>.Fail(
                            new Exception("An error occured during your request : " +
                                          ex.Message +
                                          " Please contact your administrator or try again."));
                    }

                //}
                var shippingPrice = 0M;

                if (_processorSettings.UpsFreeGroundShipping && _shipMethod.ServiceCode == UPSShippingGatewayProvider.UPSGround)
                {
                    shippingPrice = 0M;
                }
                else
                {
                    var firstCarrierRate = collection.FirstOrDefault(option => option.Service == _shipMethod.ServiceCode);
                    if (firstCarrierRate != null)
                    {
                        shippingPrice = firstCarrierRate.Rate;
                    }
                }
                
                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."));
            }
        }
Пример #6
0
        //Create rating request XML string
        private string RateRequest(IShipment shipment, UPSShipmentLineItemVisitor visitor)
        {
            var sb = new StringBuilder();

            //Build Access Request
            sb.Append("<?xml version='1.0'?>");
            sb.Append("<AccessRequest xml:lang='en-US'>");
            sb.AppendFormat("<AccessLicenseNumber>{0}</AccessLicenseNumber>", _processorSettings.UpsAccessKey);
            sb.AppendFormat("<UserId>{0}</UserId>", _processorSettings.UpsUserName);
            sb.AppendFormat("<Password>{0}</Password>", _processorSettings.UpsPassword);
            sb.Append("</AccessRequest>");

            //Build Rate Request
            sb.Append("<?xml version='1.0'?>");
                sb.Append("<RatingServiceSelectionRequest xml:lang='en-US'>");
                    sb.Append("<Request>");
                        sb.Append("<TransactionReference>");
                            sb.Append("<CustomerContext>Bare Bones Rate Request</CustomerContext>");
                            sb.Append("<XpciVersion>1</XpciVersion>");
                        sb.Append("</TransactionReference>");
                        sb.Append("<RequestAction>Rate</RequestAction>");
                        sb.Append("<RequestOption>Shop</RequestOption>");
                    sb.Append("</Request>");
                    sb.Append("<PickupType>");
                        sb.AppendFormat("<Code>{0}</Code>", _processorSettings.UpsPickupTypeCode);
                    sb.Append("</PickupType>");
                    sb.Append("<CustomerClassification>");
                        sb.AppendFormat("<Code>{0}</Code>", _processorSettings.UpsCustomerClassification);
                    sb.Append("</CustomerClassification>");
                    sb.Append("<Shipment>");
                        sb.Append("<Description>Rate Quote</Description>");
                        sb.Append("<Shipper>");
                            sb.AppendFormat("<Name>{0}</Name>", shipment.FromName);
                            sb.Append("<ShipperNumber>834010</ShipperNumber>"); // Added for negotiated rates
                            sb.Append("<Address>");
                                sb.AppendFormat("<AddressLine1>{0}</AddressLine1>", shipment.FromAddress1);
                                sb.AppendFormat("<StateProvinceCode>{0}</StateProvinceCode>", shipment.FromRegion);
                                sb.AppendFormat("<PostalCode>{0}</PostalCode>", shipment.FromPostalCode);
                                sb.AppendFormat("<CountryCode>{0}</CountryCode>", shipment.FromCountryCode);
                            sb.Append("</Address>");
                        sb.Append("</Shipper>");
                        sb.Append("<ShipTo>");
                            sb.AppendFormat("<Name>{0}</Name>", shipment.ToName);
                            sb.Append("<Address>");
                        //Required to get accurate residential delivery rates
                            sb.Append("<ResidentialAddressIndicator/>"); 
                                sb.AppendFormat("<AddressLine1>{0}</AddressLine1>", shipment.ToAddress1);
                                sb.AppendFormat("<StateProvinceCode>{0}</StateProvinceCode>", shipment.ToRegion);
                                sb.AppendFormat("<PostalCode>{0}</PostalCode>", shipment.ToPostalCode);
                                sb.AppendFormat("<CountryCode>{0}</CountryCode>", shipment.ToCountryCode);
                            sb.Append("</Address>");
                        sb.Append("</ShipTo>");
                        sb.Append("<ShipFrom>");
                            sb.Append("<Address>");
                                sb.AppendFormat("<AddressLine1>{0}</AddressLine1>", shipment.FromAddress1);
                                sb.AppendFormat("<StateProvinceCode>{0}</StateProvinceCode>", shipment.FromRegion);
                                sb.AppendFormat("<PostalCode>{0}</PostalCode>", shipment.FromPostalCode);
                                sb.AppendFormat("<CountryCode>{0}</CountryCode>", shipment.FromCountryCode);
                            sb.Append("</Address>");
                        sb.Append("</ShipFrom>");
                        sb.Append("<Service>");
                            sb.Append("<Code>03</Code>");
                        sb.Append("</Service>");

                        sb.Append("<Package>");
                            sb.Append("<PackagingType>");
                                sb.AppendFormat("<Code>{0}</Code>", _processorSettings.UpsPackagingTypeCode);
                            sb.Append("</PackagingType>");
                                sb.Append("<UnitOfMeasurement>");
                                    sb.Append("<Code>IN</Code>");
                                sb.Append("</UnitOfMeasurement>");
                                sb.AppendFormat("<Length>{0}</Length>", visitor.TotalLength);
                                sb.AppendFormat("<Width>{0}</Width>", visitor.TotalWidth);
                                sb.AppendFormat("<Height>{0}</Height>", visitor.TotalHeight);
                            sb.Append("<PackageWeight>");
                                sb.Append("<UnitOfMeasurement>");
                                    sb.Append("<Code>LBS</Code>");
                                sb.Append("</UnitOfMeasurement>");
                                sb.Append(string.Format("<Weight>{0}</Weight>", (int)Math.Ceiling(visitor.TotalWeight)));
                            sb.Append("</PackageWeight>");
                        sb.Append("<PackageServiceOptions>");
                            sb.Append("<InsuredValue>");
                                sb.Append("<CurrencyCode>USD</CurrencyCode>");
                                sb.Append("<MonetaryValue>0.00</MonetaryValue>");
                            sb.Append("</InsuredValue>");
                        sb.Append("</PackageServiceOptions>");
                    sb.Append("</Package>");
                sb.Append("</Shipment>");
            sb.Append("</RatingServiceSelectionRequest>");

            return sb.ToString();
        }
        //Create rating request XML string
        private string RateRequest(IShipment shipment, UPSShipmentLineItemVisitor visitor)
        {
            var sb = new StringBuilder();

            //Build Access Request
            sb.Append("<?xml version='1.0'?>");
            sb.Append("<AccessRequest xml:lang='en-US'>");
            sb.AppendFormat("<AccessLicenseNumber>{0}</AccessLicenseNumber>", _processorSettings.UpsAccessKey);
            sb.AppendFormat("<UserId>{0}</UserId>", _processorSettings.UpsUserName);
            sb.AppendFormat("<Password>{0}</Password>", _processorSettings.UpsPassword);
            sb.Append("</AccessRequest>");

            //Build Rate Request
            sb.Append("<?xml version='1.0'?>");
            sb.Append("<RatingServiceSelectionRequest xml:lang='en-US'>");
            sb.Append("<Request>");
            sb.Append("<TransactionReference>");
            sb.Append("<CustomerContext>Bare Bones Rate Request</CustomerContext>");
            sb.Append("<XpciVersion>1</XpciVersion>");
            sb.Append("</TransactionReference>");
            sb.Append("<RequestAction>Rate</RequestAction>");
            sb.Append("<RequestOption>Shop</RequestOption>");
            sb.Append("</Request>");
            sb.Append("<PickupType>");
            sb.AppendFormat("<Code>{0}</Code>", _processorSettings.UpsPickupTypeCode);
            sb.Append("</PickupType>");
            sb.Append("<CustomerClassification>");
            sb.AppendFormat("<Code>{0}</Code>", _processorSettings.UpsCustomerClassification);
            sb.Append("</CustomerClassification>");
            sb.Append("<Shipment>");
            sb.Append("<Description>Rate Quote</Description>");
            sb.Append("<Shipper>");
            sb.AppendFormat("<Name>{0}</Name>", shipment.FromName);
            sb.Append("<ShipperNumber>834010</ShipperNumber>");                 // Added for negotiated rates
            sb.Append("<Address>");
            sb.AppendFormat("<AddressLine1>{0}</AddressLine1>", shipment.FromAddress1);
            sb.AppendFormat("<StateProvinceCode>{0}</StateProvinceCode>", shipment.FromRegion);
            sb.AppendFormat("<PostalCode>{0}</PostalCode>", shipment.FromPostalCode);
            sb.AppendFormat("<CountryCode>{0}</CountryCode>", shipment.FromCountryCode);
            sb.Append("</Address>");
            sb.Append("</Shipper>");
            sb.Append("<ShipTo>");
            sb.AppendFormat("<Name>{0}</Name>", shipment.ToName);
            sb.Append("<Address>");
            //Required to get accurate residential delivery rates
            sb.Append("<ResidentialAddressIndicator/>");
            sb.AppendFormat("<AddressLine1>{0}</AddressLine1>", shipment.ToAddress1);
            sb.AppendFormat("<StateProvinceCode>{0}</StateProvinceCode>", shipment.ToRegion);
            sb.AppendFormat("<PostalCode>{0}</PostalCode>", shipment.ToPostalCode);
            sb.AppendFormat("<CountryCode>{0}</CountryCode>", shipment.ToCountryCode);
            sb.Append("</Address>");
            sb.Append("</ShipTo>");
            sb.Append("<ShipFrom>");
            sb.Append("<Address>");
            sb.AppendFormat("<AddressLine1>{0}</AddressLine1>", shipment.FromAddress1);
            sb.AppendFormat("<StateProvinceCode>{0}</StateProvinceCode>", shipment.FromRegion);
            sb.AppendFormat("<PostalCode>{0}</PostalCode>", shipment.FromPostalCode);
            sb.AppendFormat("<CountryCode>{0}</CountryCode>", shipment.FromCountryCode);
            sb.Append("</Address>");
            sb.Append("</ShipFrom>");
            sb.Append("<Service>");
            sb.Append("<Code>03</Code>");
            sb.Append("</Service>");

            sb.Append("<Package>");
            sb.Append("<PackagingType>");
            sb.AppendFormat("<Code>{0}</Code>", _processorSettings.UpsPackagingTypeCode);
            sb.Append("</PackagingType>");
            sb.Append("<UnitOfMeasurement>");
            sb.Append("<Code>IN</Code>");
            sb.Append("</UnitOfMeasurement>");
            sb.AppendFormat("<Length>{0}</Length>", visitor.TotalLength);
            sb.AppendFormat("<Width>{0}</Width>", visitor.TotalWidth);
            sb.AppendFormat("<Height>{0}</Height>", visitor.TotalHeight);
            sb.Append("<PackageWeight>");
            sb.Append("<UnitOfMeasurement>");
            sb.Append("<Code>LBS</Code>");
            sb.Append("</UnitOfMeasurement>");
            sb.Append(string.Format("<Weight>{0}</Weight>", (int)Math.Ceiling(visitor.TotalWeight)));
            sb.Append("</PackageWeight>");
            sb.Append("<PackageServiceOptions>");
            sb.Append("<InsuredValue>");
            sb.Append("<CurrencyCode>USD</CurrencyCode>");
            sb.Append("<MonetaryValue>0.00</MonetaryValue>");
            sb.Append("</InsuredValue>");
            sb.Append("</PackageServiceOptions>");
            sb.Append("</Package>");
            sb.Append("</Shipment>");
            sb.Append("</RatingServiceSelectionRequest>");

            return(sb.ToString());
        }
Пример #8
0
        public override Attempt <IShipmentRateQuote> QuoteShipment(IShipment shipment)
        {
            try
            {
                // TODO this should be made configurable
                var visitor = new UPSShipmentLineItemVisitor()
                {
                    UseOnSalePriceIfOnSale = false
                };

                shipment.Items.Accept(visitor);

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

                // JASON: Removing Cache check as the strategy handles this. (However, the Provider is getting quotes to know what methods to return...)
                var collection = GetCollectionFromCache(shipment);

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

                    collection = UpsParseRates(rateXml);

                    //_runtimeCache.InsertCacheItem(MakeCacheKey(shipment), () => collection);
                }
                catch (Exception ex)
                {
                    return(Attempt <IShipmentRateQuote> .Fail(
                               new Exception("An error occured during your request : " +
                                             ex.Message +
                                             " Please contact your administrator or try again.")));
                }

                //}
                var shippingPrice = 0M;

                if (_processorSettings.UpsFreeGroundShipping && _shipMethod.ServiceCode == UPSShippingGatewayProvider.UPSGround)
                {
                    shippingPrice = 0M;
                }
                else
                {
                    var firstCarrierRate = collection.FirstOrDefault(option => option.Service == _shipMethod.ServiceCode);
                    if (firstCarrierRate != null)
                    {
                        shippingPrice = firstCarrierRate.Rate;
                    }
                }

                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.")));
            }
        }