How to get Rates from UPS Rate API?

2020-04-14 01:42发布

问题:

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.