How to get Rates from UPS Rate API?

血红的双手。 提交于 2019-12-20 03:21:27

问题


I am using nopcommerce 3.5. I have added plugin of UPS of TransitInTime and Rate API. I want to get rates by calling UPS Rate API. I want all Rates in dropdown on page load.

So for the first I am using test application using webservices of RateWebReference and in which I get only one Rate but I want Rates for all shipping option.

Here is my code of RateWSClient.cs

  RateService rate = new RateService();
                RateRequest rateRequest = new RateRequest();
                UPSSecurity upss = new UPSSecurity();
                UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSSecurityServiceAccessToken();
                upssSvcAccessToken.AccessLicenseNumber = "CC....";
                upss.ServiceAccessToken = upssSvcAccessToken;
                UPSSecurityUsernameToken upssUsrNameToken = new UPSSecurityUsernameToken();
                upssUsrNameToken.Username = "gi..";
                upssUsrNameToken.Password = "Ch..";
                upss.UsernameToken = upssUsrNameToken;
                rate.UPSSecurityValue = upss;
                RequestType request = new RequestType();
                String[] requestOption = { "Rate" };
                request.RequestOption = requestOption;

                rateRequest.Request = request;
                ShipmentType shipment = new ShipmentType();
                ShipperType shipper = new ShipperType();

                shipper.ShipperNumber = "A65V88";
                RateWSSample.RateWebReference1.AddressType shipperAddress = new RateWSSample.RateWebReference1.AddressType();
                String[] addressLine = { "", "", "" };
                shipperAddress.AddressLine = addressLine;
                shipperAddress.City = "";
                shipperAddress.PostalCode = "30076";
                shipperAddress.StateProvinceCode = "GA";
                shipperAddress.CountryCode = "US";
                shipperAddress.AddressLine = addressLine;
                shipper.Address = shipperAddress;
                shipment.Shipper = shipper;
                ShipFromType shipFrom = new ShipFromType();
                RateWSSample.RateWebReference1.AddressType shipFromAddress = new RateWSSample.RateWebReference1.AddressType();
                shipFromAddress.AddressLine = addressLine;
                shipFromAddress.City = "";
                shipFromAddress.PostalCode = "30076";
                shipFromAddress.StateProvinceCode = "GA";
                shipFromAddress.CountryCode = "US";
                shipFrom.Address = shipFromAddress;
                shipment.ShipFrom = shipFrom;
                ShipToType shipTo = new ShipToType();
                ShipToAddressType shipToAddress = new ShipToAddressType();
                String[] addressLine1 = { "", "", "" };
                shipToAddress.AddressLine = addressLine1;
                shipToAddress.City = "";
                shipToAddress.PostalCode = "92262";
                shipToAddress.StateProvinceCode = "";
                shipToAddress.CountryCode = "US";
                shipTo.Address = shipToAddress;
                shipment.ShipTo = shipTo;
                CodeDescriptionType service = new CodeDescriptionType();

                //Below code uses dummy date for reference. Please udpate as required.
                service.Code = "02";
                shipment.Service = service;
                PackageType package = new PackageType();
                PackageWeightType packageWeight = new PackageWeightType();
                packageWeight.Weight = "125";
                CodeDescriptionType uom = new CodeDescriptionType();
                uom.Code = "LBS";
                uom.Description = "pounds";
                packageWeight.UnitOfMeasurement = uom;
                package.PackageWeight = packageWeight;
                CodeDescriptionType packType = new CodeDescriptionType();
                packType.Code = "02";
                package.PackagingType = packType;
                PackageType[] pkgArray = { package };
                shipment.Package = pkgArray;

               //Shipping Rate Chart

               // ShipmentRatingOptionsType SRO = new ShipmentRatingOptionsType();
                //SRO.RateChartIndicator = "";
                //shipment.ShipmentRatingOptions= SRO;

                //rateRequest.Shipment = shipment;


                ShipmentRatingOptionsType SRO = new ShipmentRatingOptionsType();
                SRO.NegotiatedRatesIndicator = "";
                shipment.ShipmentRatingOptions = SRO;

                rateRequest.Shipment = shipment;

                System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
                Console.WriteLine(rateRequest);
                RateResponse rateResponse = rate.ProcessRate(rateRequest);
                Console.WriteLine("The transaction was a " + rateResponse.Response.ResponseStatus.Description);
                Console.WriteLine("Total Shipment Charges " + rateResponse.RatedShipment[0].TotalCharges.MonetaryValue + rateResponse.RatedShipment[0].TotalCharges.CurrencyCode);
                Console.ReadKey();

回答1:


I have resolved this questions. So If you face this kind of problem don't forget to use

String[] requestOption = { "Shop" };

in place of

String[] requestOption = { "Rate" };

Then you will get rates for all shipping options.



来源:https://stackoverflow.com/questions/31088900/how-to-get-rates-from-ups-rate-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!