Exemplo n.º 1
0
        public DomesticResponse ValidateRequest(DomesticRequest request)
        {
            DomesticResponse result = new DomesticResponse();

            if (request == null)
            {
                result.Errors.Add(new USPSError()
                {
                    Description = "Request was null"
                });
                return(result);
            }

            if (request.Packages.Count < 1)
            {
                result.Errors.Add(new USPSError()
                {
                    Description = "Request requires at least one package."
                });
            }

            if (request.UserId.Trim().Length < 1)
            {
                result.Errors.Add(new USPSError()
                {
                    Description = "UserId is Required for Requests"
                });
            }

            return(result);
        }
Exemplo n.º 2
0
        private List<IShippingRate> RatePackages(List<DomesticPackage> packages)
        {

            List<IShippingRate> rates = new List<IShippingRate>();

            DomesticRequest req = new DomesticRequest();
            req.Packages = packages;            

            DomesticService svc = new DomesticService();
            DomesticResponse res = svc.ProcessRequest(req);
                        
            
            if (this.GlobalSettings.DiagnosticsMode)
            {
                _Logger.LogMessage("US Postal Request: " + svc.LastRequest);
                _Logger.LogMessage("US Postal Response: " + svc.LastResponse);                                
            }

            bool hasErrors = (res.Errors.Count > 0);
                                                    
            foreach (DomesticPackageServiceResponse possibleResponse in DomesticPackageServiceResponse.FindAll())            
            {
                bool AllPackagesRated = true;
                decimal totalRate = 0m;

                foreach (DomesticPackage p in res.Packages)
                {
                    DomesticPostage found = p.Postages.Where(y => y.MailServiceClassId == possibleResponse.XmlClassId).FirstOrDefault();
                    if (found == null)
                    {
                        AllPackagesRated = false;
                        break;
                    }

                    totalRate += found.Rate;
                }

                if (AllPackagesRated && totalRate > 0)
                {
                    // Rate is good to go for all packages
                    rates.Add(new ShippingRate() { EstimatedCost = totalRate, ServiceId = this.Id, ServiceCodes = ((int)possibleResponse.ServiceType).ToString(), 
                                                    DisplayName = "USPS:" + possibleResponse.XmlName});
                }            
            }
            
            
            return rates;
        }
Exemplo n.º 3
0
        public DomesticResponse ProcessRequest(DomesticRequest request)
        {
            this.LastResponse = string.Empty;
            this.LastRequest  = string.Empty;

            // Validate Request First
            DomesticResponse result = ValidateRequest(request);

            if (result.Errors.Count > 0)
            {
                return(result);
            }

            try
            {
                string sURL = request.ApiUrl;
                sURL += "?API=RateV4&XML=";

                // Build XML
                string requestXml = string.Empty;

                StringWriter  sw = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
                XmlTextWriter xw = new XmlTextWriter(sw);

                xw.Formatting = Formatting.None;

                // Start Request
                xw.WriteStartElement("RateV4Request");
                xw.WriteAttributeString("USERID", request.UserId);
                xw.WriteElementString("Revision", request.Revision);

                foreach (DomesticPackage pak in request.Packages)
                {
                    pak.WriteToXml(ref xw);
                }

                //End Rate Request
                xw.WriteEndElement();
                xw.Flush();
                xw.Close();
                requestXml = sw.GetStringBuilder().ToString();
                if (!requestXml.StartsWith("<"))
                {
                    requestXml = requestXml.Substring(1, requestXml.Length - 1);
                }

                // Diagnostics
                this.LastRequest = requestXml;

                string sResponse  = string.Empty;
                string dataToSend = sURL + System.Web.HttpUtility.UrlEncode(requestXml);

                sResponse = readHtmlPage(dataToSend);

                // Diagnostics
                this.LastResponse = sResponse;

                result = new DomesticResponse(sResponse);
            }
            catch (Exception ex)
            {
                result.Errors.Add(new USPSError()
                {
                    Description = ex.Message + " " + ex.StackTrace, Source = "BV Exception"
                });
            }
            return(result);
        }
Exemplo n.º 4
0
        public DomesticResponse ProcessRequest(DomesticRequest request)
        {
            this.LastResponse = string.Empty;
            this.LastRequest = string.Empty;

            // Validate Request First
            DomesticResponse result = ValidateRequest(request);
            if (result.Errors.Count > 0) return result;

            try
            {

                string sURL = request.ApiUrl;
                sURL += "?API=RateV4&XML=";

                // Build XML
                string requestXml = string.Empty;

                StringWriter sw = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
                XmlTextWriter xw = new XmlTextWriter(sw);

                xw.Formatting = Formatting.None;

                // Start Request
                xw.WriteStartElement("RateV4Request");
                xw.WriteAttributeString("USERID", request.UserId);
                xw.WriteElementString("Revision", request.Revision);

                foreach (DomesticPackage pak in request.Packages)
                {
                    pak.WriteToXml(ref xw);
                }

                //End Rate Request
                xw.WriteEndElement();
                xw.Flush();
                xw.Close();
                requestXml = sw.GetStringBuilder().ToString();
                if (!requestXml.StartsWith("<"))
                {
                    requestXml = requestXml.Substring(1, requestXml.Length - 1);
                }

                // Diagnostics
                this.LastRequest = requestXml;
                
                string sResponse = string.Empty;
                string dataToSend = sURL + System.Web.HttpUtility.UrlEncode(requestXml);

                sResponse = readHtmlPage(dataToSend);

                // Diagnostics
                this.LastResponse = sResponse;                

                result = new DomesticResponse(sResponse);
            }
            catch (Exception ex)
            {
                result.Errors.Add(new USPSError() { Description = ex.Message + " " + ex.StackTrace, Source = "BV Exception" });
            }
            return result;
        }
Exemplo n.º 5
0
        public DomesticResponse ValidateRequest(DomesticRequest request)
        {
            DomesticResponse result = new DomesticResponse();

            if (request == null)
            {
                result.Errors.Add(new USPSError() { Description = "Request was null" });
                return result;
            }

            if (request.Packages.Count < 1)
            {
                result.Errors.Add(new USPSError() { Description = "Request requires at least one package." });            
            }

            if (request.UserId.Trim().Length < 1)
            {
                result.Errors.Add(new USPSError() { Description = "UserId is Required for Requests" });
            }            

            return result;
        }