示例#1
0
        private FreightRateRequest BuildUPSFreightRateRequestForShipment(IShipment shipment)
        {
            FreightRateRequest freightRateRequest = new FreightRateRequest();
            RequestType        request            = new RequestType();

            String[] requestOption = { "1" }; //Ground
            request.RequestOption      = requestOption;
            freightRateRequest.Request = request;

            /** ****************ShipFrom******************************* */
            ShipFromType shipFrom        = new ShipFromType();
            AddressType  shipFromAddress = new AddressType();

            String[] shipFromAddressLines = { shipment.SourceAddress.Street, shipment.SourceAddress.Street2 };
            shipFromAddress.AddressLine       = shipFromAddressLines;
            shipFromAddress.City              = shipment.SourceAddress.City;
            shipFromAddress.StateProvinceCode = shipment.SourceAddress.RegionBvin;
            shipFromAddress.PostalCode        = shipment.SourceAddress.PostalCode;
            shipFromAddress.CountryCode       = shipment.SourceAddress.CountryData.IsoCode;
            shipFrom.Address = shipFromAddress;

            freightRateRequest.ShipFrom = shipFrom;
            /** ****************ShipFrom******************************* */

            /** ****************ShipTo*************************************** */
            ShipToType  shipTo        = new ShipToType();
            AddressType shipToAddress = new AddressType();

            String[] shipToAddressLines = { shipment.DestinationAddress.Street, shipment.DestinationAddress.Street2 };
            shipToAddress.AddressLine       = shipToAddressLines;
            shipToAddress.City              = shipment.DestinationAddress.City;
            shipToAddress.StateProvinceCode = shipment.DestinationAddress.RegionBvin;
            shipToAddress.PostalCode        = shipment.DestinationAddress.PostalCode;
            shipToAddress.CountryCode       = shipment.DestinationAddress.CountryData.IsoCode;
            shipTo.Address = shipToAddress;


            freightRateRequest.ShipTo = shipTo;
            /** ****************ShipTo*************************************** */

            /** ***************PaymentInformationType************************* */

            PaymentInformationType paymentInfo = new PaymentInformationType();
            PayerType payer = new PayerType();

            payer.AttentionName = Settings.PayerName; //Required PayerName
            payer.Name          = Settings.PayerName;


            AddressType payerAddress = new AddressType();

            String[] payerAddressLines = { shipment.DestinationAddress.Street };
            payerAddress.AddressLine       = payerAddressLines;
            payerAddress.City              = shipment.DestinationAddress.City;
            payerAddress.StateProvinceCode = shipment.DestinationAddress.RegionBvin;
            payerAddress.PostalCode        = shipment.DestinationAddress.PostalCode;
            payerAddress.CountryCode       = shipment.DestinationAddress.CountryData.IsoCode;
            payer.Address     = payerAddress;
            paymentInfo.Payer = payer;

            RateCodeDescriptionType shipBillOption = new RateCodeDescriptionType();

            shipBillOption.Code                   = ((int)GlobalSettings.BillingOption).ToString();
            shipBillOption.Description            = GlobalSettings.BillingOption.ToString();
            paymentInfo.ShipmentBillingOption     = shipBillOption;
            freightRateRequest.PaymentInformation = paymentInfo;

            /** ***************PaymentInformationType************************* */

            /** ***************Service************************************** */
            RateCodeDescriptionType service = new RateCodeDescriptionType();

            service.Code               = Settings.ServiceCodeFilter[0].Code;
            service.Description        = Settings.ServiceCodeFilter[0].DisplayName;
            freightRateRequest.Service = service;
            /** ***************Service************************************** */

            /** **************HandlingUnitOne************************************* */
            HandlingUnitType handUnitType = new HandlingUnitType();

            handUnitType.Quantity = shipment.Items.Count.ToString();
            RateCodeDescriptionType rateCodeDescType = new RateCodeDescriptionType();

            rateCodeDescType.Code              = GlobalSettings.HandleOneUnitType.ToString();
            rateCodeDescType.Description       = GlobalSettings.HandleOneUnitType.ToString();
            handUnitType.Type                  = rateCodeDescType;
            freightRateRequest.HandlingUnitOne = handUnitType;
            /** **************HandlingUnitOne************************************* */

            /** **************Commodity************************************* */
            // Optimize Packages for Weight
            var optimizedPackages = OptimizeSingleGroup(shipment);
            var ignoreDimensions  = GlobalSettings.IgnoreDimensions;

            List <CommodityType> commodityArray = new List <CommodityType>();

            foreach (var pak in optimizedPackages)
            {
                //Prepare Commodity Item
                CommodityType commodity = WriteSingleCommidityPackage(pak, ignoreDimensions);
                commodityArray.Add(commodity);
            }
            freightRateRequest.Commodity = commodityArray.ToArray();

            /** **************Commodity************************************* */

            return(freightRateRequest);
        }
示例#2
0
        private CommodityType WriteSingleCommidityPackage(IShippable pak, bool ignoreDimensions)
        {
            decimal dGirth  = 0;
            decimal dLength = 0;
            decimal dHeight = 0;
            decimal dwidth  = 0;

            CommodityType      commodity = new CommodityType();
            CommodityValueType commValue = new CommodityValueType();

            commodity.NumberOfPieces = pak.QuantityOfItemsInBox.ToString();

            RateCodeDescriptionType packagingType = new RateCodeDescriptionType();

            packagingType.Code        = ((int)GlobalSettings.DefaultPackaging).ToString();
            packagingType.Description = GlobalSettings.DefaultPackaging.ToString();
            commodity.PackagingType   = packagingType;

            WeightType            weight            = new WeightType();
            UnitOfMeasurementType unitOfMeasurement = new UnitOfMeasurementType();

            unitOfMeasurement.Code        = (pak.BoxWeightType == Hotcakes.Shipping.WeightType.Pounds ? "LBS" : "KGS");
            unitOfMeasurement.Description = (pak.BoxWeightType == Hotcakes.Shipping.WeightType.Pounds ? "LBS" : "KGS");

            weight.UnitOfMeasurement = unitOfMeasurement;
            if (pak.BoxWeight > 0)
            {
                weight.Value = Math.Round(pak.BoxWeight, 1).ToString();
            }
            else
            {
                weight.Value = "1";
            }

            commodity.Weight       = weight;
            commodity.Description  = "Weight";
            commodity.FreightClass = GlobalSettings.FreightClass;


            var dimensions = new List <DimensionAmount>();

            if (pak.BoxLengthType == LengthType.Centimeters)
            {
                dimensions.Add(new DimensionAmount(Conversions.CentimetersToInches(pak.BoxLength)));
                dimensions.Add(new DimensionAmount(Conversions.CentimetersToInches(pak.BoxWidth)));
                dimensions.Add(new DimensionAmount(Conversions.CentimetersToInches(pak.BoxHeight)));
            }
            else
            {
                dimensions.Add(new DimensionAmount(pak.BoxLength));
                dimensions.Add(new DimensionAmount(pak.BoxWidth));
                dimensions.Add(new DimensionAmount(pak.BoxWidth));
            }

            var sorted = (from d in dimensions
                          orderby d.Amount descending
                          select d.Amount).ToList();

            dLength = sorted[0];
            dwidth  = sorted[1];
            dHeight = sorted[2];

            dGirth = dwidth + dwidth + dHeight + dHeight;
            //Dimensions can be skipped in latest UPS specs
            if (!ignoreDimensions)
            {
                if (dLength > 0 | dHeight > 0 | dwidth > 0)
                {
                    DimensionsType commondityDimensions = new DimensionsType();

                    commondityDimensions.Height = Math.Round(dHeight, 2).ToString(CultureInfo.InvariantCulture);
                    commondityDimensions.Width  = Math.Round(dwidth, 2).ToString(CultureInfo.InvariantCulture);
                    commondityDimensions.Length = Math.Round(dLength, 2).ToString(CultureInfo.InvariantCulture);

                    UnitOfMeasurementType unitOfMeasurementDimention = new UnitOfMeasurementType();
                    unitOfMeasurementDimention.Code        = "IN";
                    unitOfMeasurementDimention.Description = "Inch";
                    commondityDimensions.UnitOfMeasurement = unitOfMeasurementDimention;

                    commodity.Dimensions = commondityDimensions;
                }
            }
            return(commodity);
        }
示例#3
0
        static void Main()
        {
            try
            {
                FreightRateService freightRateService = new FreightRateService();
                FreightRateRequest freightRateRequest = new FreightRateRequest();
                RequestType        request            = new RequestType();
                String[]           requestOption      = { "RateChecking Option" };
                request.RequestOption      = requestOption;
                freightRateRequest.Request = request;

                /** ****************ShipFrom******************************* */
                ShipFromType shipFrom             = new ShipFromType();
                AddressType  shipFromAddress      = new AddressType();
                String[]     shipFromAddressLines = { "ShipFrom address" };
                shipFromAddress.AddressLine       = shipFromAddressLines;
                shipFromAddress.City              = "ShipFrom city";
                shipFromAddress.StateProvinceCode = "ShipFrom state province code";
                shipFromAddress.PostalCode        = "ShipFrom postal code";
                shipFromAddress.CountryCode       = "ShipFrom country code";
                shipFrom.Address            = shipFromAddress;
                shipFrom.AttentionName      = "ShipFrom attention name";
                shipFrom.Name               = "ShipFrom Name";
                freightRateRequest.ShipFrom = shipFrom;
                /** ****************ShipFrom******************************* */

                /** ****************ShipTo*************************************** */
                ShipToType  shipTo             = new ShipToType();
                AddressType shipToAddress      = new AddressType();
                String[]    shipToAddressLines = { "ShipTo address line" };
                shipToAddress.AddressLine       = shipToAddressLines;
                shipToAddress.City              = "ShipTo city";
                shipToAddress.StateProvinceCode = "ShipTo state province code";
                shipToAddress.PostalCode        = "ShipTo postal code";
                shipToAddress.CountryCode       = "ShipTo country code";
                shipTo.Address            = shipToAddress;
                shipTo.AttentionName      = "ShipTo attention name";
                shipTo.Name               = "ShipTo Name";
                freightRateRequest.ShipTo = shipTo;
                /** ****************ShipTo*************************************** */

                /** ***************PaymentInformationType************************* */
                PaymentInformationType paymentInfo = new PaymentInformationType();
                PayerType payer = new PayerType();
                payer.AttentionName = "Payer attention name";
                payer.Name          = "Payer name";
                payer.ShipperNumber = "Payer shipper number";
                AddressType payerAddress      = new AddressType();
                String[]    payerAddressLines = { "Payer address line" };
                payerAddress.AddressLine       = payerAddressLines;
                payerAddress.City              = "Payer city";
                payerAddress.StateProvinceCode = "Payer state province code";
                payerAddress.PostalCode        = "Payer postal code";
                payerAddress.CountryCode       = "Payer country code";
                payer.Address     = payerAddress;
                paymentInfo.Payer = payer;
                RateCodeDescriptionType shipBillOption = new RateCodeDescriptionType();
                shipBillOption.Code                   = "Ship bill option";
                shipBillOption.Description            = "Ship bill description";
                paymentInfo.ShipmentBillingOption     = shipBillOption;
                freightRateRequest.PaymentInformation = paymentInfo;
                /** ***************PaymentInformationType************************* */

                //Below code use dummy data for referenced. Please update as required


                /** ***************Service************************************** */
                RateCodeDescriptionType service = new RateCodeDescriptionType();
                service.Code               = "309";
                service.Description        = "UPS Ground Freight";
                freightRateRequest.Service = service;
                /** ***************Service************************************** */


                /** **************Commodity************************************* */
                CommodityType      commodity = new CommodityType();
                CommodityValueType commValue = new CommodityValueType();
                commValue.CurrencyCode   = "USD";
                commValue.MonetaryValue  = "5670";
                commodity.CommodityValue = commValue;
                commodity.NumberOfPieces = "20";

                RateCodeDescriptionType packagingType = new RateCodeDescriptionType();
                packagingType.Code        = "BAG";
                packagingType.Description = "BAG";
                commodity.PackagingType   = packagingType;
                WeightType            weight            = new WeightType();
                UnitOfMeasurementType unitOfMeasurement = new UnitOfMeasurementType();
                unitOfMeasurement.Code        = "LBS";
                unitOfMeasurement.Description = "Pounds";
                weight.UnitOfMeasurement      = unitOfMeasurement;
                weight.Value          = "200";
                commodity.Weight      = weight;
                commodity.Description = "LCD TVS";

                CommodityValueType commodityValue = new CommodityValueType();
                commodityValue.CurrencyCode  = "USD";
                commodityValue.MonetaryValue = "100";
                commodity.CommodityValue     = commodityValue;
                commodity.Description        = "LCD TVS";
                commodity.FreightClass       = "60";
                CommodityType[] commodityArray = { commodity };
                freightRateRequest.Commodity = commodityArray;
                /** **************Commodity************************************* */


                /** **************HandlingUnitOne************************************* */
                HandlingUnitType handUnitType = new HandlingUnitType();
                handUnitType.Quantity = "1";
                RateCodeDescriptionType rateCodeDescType = new RateCodeDescriptionType();
                rateCodeDescType.Code              = "SKD";
                rateCodeDescType.Description       = "SKID";
                handUnitType.Type                  = rateCodeDescType;
                freightRateRequest.HandlingUnitOne = handUnitType;

                /** **************HandlingUnitOne************************************* */


                UPSSecurity upss = new UPSSecurity();
                UPSSecurityServiceAccessToken upsSvcToken = new UPSSecurityServiceAccessToken();
                upsSvcToken.AccessLicenseNumber = "Your License Number";
                upss.ServiceAccessToken         = upsSvcToken;
                UPSSecurityUsernameToken upsSecUsrnameToken = new UPSSecurityUsernameToken();
                upsSecUsrnameToken.Username         = "******";
                upsSecUsrnameToken.Password         = "******";
                upss.UsernameToken                  = upsSecUsrnameToken;
                freightRateService.UPSSecurityValue = upss;

                System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
                Console.WriteLine(freightRateRequest);
                FreightRateResponse freightRateResponse = freightRateService.ProcessFreightRate(freightRateRequest);
                Console.WriteLine("Response code: " + freightRateResponse.Response.ResponseStatus.Code);
                Console.WriteLine("Response description: " + freightRateResponse.Response.ResponseStatus.Description);
                Console.ReadKey();
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                Console.WriteLine("");
                Console.WriteLine("---------Freight Rate Web Service returns error----------------");
                Console.WriteLine("---------\"Hard\" is user error \"Transient\" is system error----------------");
                Console.WriteLine("SoapException Message= " + ex.Message);
                Console.WriteLine("");
                Console.WriteLine("SoapException Category:Code:Message= " + ex.Detail.LastChild.InnerText);
                Console.WriteLine("");
                Console.WriteLine("SoapException XML String for all= " + ex.Detail.LastChild.OuterXml);
                Console.WriteLine("");
                Console.WriteLine("SoapException StackTrace= " + ex.StackTrace);
                Console.WriteLine("-------------------------");
                Console.WriteLine("");
            }
            catch (System.ServiceModel.CommunicationException ex)
            {
                Console.WriteLine("");
                Console.WriteLine("--------------------");
                Console.WriteLine("CommunicationException= " + ex.Message);
                Console.WriteLine("CommunicationException-StackTrace= " + ex.StackTrace);
                Console.WriteLine("-------------------------");
                Console.WriteLine("");
            }
            catch (Exception ex)
            {
                Console.WriteLine("");
                Console.WriteLine("-------------------------");
                Console.WriteLine(" General Exception= " + ex.Message);
                Console.WriteLine(" General Exception-StackTrace= " + ex.StackTrace);
                Console.WriteLine("-------------------------");
            }
            finally
            {
                Console.ReadKey();
            }
        }