コード例 #1
0
        public static string BuildSoapRequest(UPSFreightSettings settings, string bodyElement)
        {
            StringBuilder xml = new StringBuilder();

            xml.Append(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" ");
            xml.Append(@"xmlns:v1=""http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0"" ");
            xml.Append(@"xmlns:v11=""http://www.ups.com/XMLSchema/XOLTWS/FreightRate/v1.0"" ");
            xml.Append(@"xmlns:SOAP-ENC=""http://schemas.xmlsoap.org/soap/encoding/"" ");
            xml.Append(@"xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" ");
            xml.Append(@"xmlns:v12=""http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0"">");

            xml.Append("<soapenv:Header>");
            xml.Append("<v1:UPSSecurity>");
            xml.Append("<v1:UsernameToken>");
            xml.Append("<v1:Username>Tusharupendoventures</v1:Username>");
            xml.Append("<v1:Password>pendo@123</v1:Password>");
            xml.Append("</v1:UsernameToken>");
            xml.Append("<v1:ServiceAccessToken>");
            xml.Append("<v1:AccessLicenseNumber>4A24R5</v1:AccessLicenseNumber>");
            xml.Append("</v1:ServiceAccessToken>");
            xml.Append("</v1:UPSSecurity>");
            xml.Append("</soapenv:Header>");
            xml.Append("<soapenv:Body>");
            xml.Append(bodyElement);
            xml.Append("</soapenv:Body>");
            xml.Append("</soapenv:Envelope>");

            return(xml.ToString());
        }
コード例 #2
0
        /// -----------------------------------------------------------------------------
        /// <summary>
        ///     builds XML access key for UPS requests
        /// </summary>
        /// <param name="settings"></param>
        /// <returns></returns>
        /// <remarks>
        /// </remarks>
        /// -----------------------------------------------------------------------------
        public static string BuildAccessKey(UPSFreightSettings settings)
        {
            var sXML      = string.Empty;
            var strWriter = new StringWriter();
            var xw        = new XmlTextWriter(strWriter)
            {
                Formatting  = Formatting.Indented,
                Indentation = 3
            };

            xw.WriteStartDocument();

            //--------------------------------------------
            // Agreement Request
            xw.WriteStartElement("AccessRequest");

            xw.WriteElementString("AccessLicenseNumber", settings.License);
            xw.WriteElementString("UserId", settings.UserID);
            xw.WriteElementString("Password", settings.Password);

            xw.WriteEndElement();
            // End Agreement Request
            //--------------------------------------------

            xw.WriteEndDocument();
            xw.Flush();
            xw.Close();

            sXML = strWriter.GetStringBuilder().ToString();

            xw = null;

            return(sXML);
        }
コード例 #3
0
ファイル: UPSFreightService.cs プロジェクト: wncoder/core
        // Gets all available rates regardless of settings
        private List <IShippingRate> GetAllShippingRatesForShipment(IShipment shipment)
        {
            var rates     = new List <IShippingRate>();
            var hasErrors = false;

            try
            {
                var sErrorMessage = string.Empty;
                var sErrorCode    = string.Empty;

                var sURL = string.Concat(UPSLIVESERVER, "FreightRate");

                // Build XML
                var settings = new UPSFreightSettings
                {
                    UserID    = GlobalSettings.Username,
                    Password  = GlobalSettings.Password,
                    ServerUrl = UPSLIVESERVER,
                    License   = GlobalSettings.LicenseNumber
                };


                var sXML = string.Empty;

                FreightRateRequest freightRateRequest = BuildUPSFreightRateRequestForShipment(shipment);
                FreightRateService rateService        = new FreightRateService();

                //Set Web Service URL
                rateService.Url = sURL;

                //Set Security Settings For Web Service
                UPSSecurity upss = new UPSSecurity();
                UPSSecurityServiceAccessToken upsSvcToken = new UPSSecurityServiceAccessToken();
                upsSvcToken.AccessLicenseNumber = settings.License;
                upss.ServiceAccessToken         = upsSvcToken;
                UPSSecurityUsernameToken upsSecUsrnameToken = new UPSSecurityUsernameToken();
                upsSecUsrnameToken.Username  = settings.UserID;
                upsSecUsrnameToken.Password  = settings.Password;
                upss.UsernameToken           = upsSecUsrnameToken;
                rateService.UPSSecurityValue = upss;

                var sStatusCode = "-1";

                try
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12; //Set for SSL Webservice Call
                    FreightRateResponse freightRateResponse = rateService.ProcessFreightRate(freightRateRequest);  //Send For Processing

                    if (freightRateResponse.Response.ResponseStatus.Code == "1")                                   //Sucess
                    {
                        sStatusCode = "1";
                        var r = new ShippingRate
                        {
                            DisplayName   = Settings.ServiceCodeFilter[0].DisplayName,
                            EstimatedCost = decimal.Parse(freightRateResponse.TotalShipmentCharge.MonetaryValue, NumberStyles.Currency, CultureInfo.InvariantCulture),
                            ServiceCodes  = Settings.ServiceCodeFilter[0].Code,
                            ServiceId     = Id
                        };
                        rates.Add(r);
                    }
                }
                catch (SoapException soapex) //Handle SOAP Exception
                {
                    _Logger.LogException(soapex);

                    var mex = new ShippingServiceMessage();

                    if (soapex.Detail != null)
                    {
                        mex.SetError("Exception", string.Concat(soapex.Detail.InnerText, " | ", soapex.Source));
                    }
                    else
                    {
                        mex.SetError("Exception", string.Concat(soapex.Message, " | ", soapex.Source));
                    }

                    _Messages.Add(mex);

                    return(rates);
                }
                catch (Exception Exx)
                {
                    _Logger.LogException(Exx);

                    var mex = new ShippingServiceMessage();

                    mex.SetError("Exception", string.Concat(Exx.Message, " | ", Exx.Source));

                    _Messages.Add(mex);

                    return(rates);
                }

                if (sStatusCode != "1")
                {
                    hasErrors = true;
                }
            }

            catch (Exception ex)
            {
                _Logger.LogException(ex);

                var m = new ShippingServiceMessage();

                m.SetError("Exception", string.Concat(ex.Message, " | ", ex.StackTrace));

                _Messages.Add(m);
            }

            if (hasErrors)
            {
                rates = new List <IShippingRate>();
            }

            return(rates);
        }