Example #1
0
        private List <IShippingRate> RatePackages(List <DomesticPackage> packages)
        {
            var rates = new List <IShippingRate>();

            var req = new DomesticRequest();

            if (!string.IsNullOrWhiteSpace(GlobalSettings.UserId))
            {
                req.UserId = GlobalSettings.UserId;
            }
            req.Packages = packages;

            var svc = new DomesticService();
            var res = svc.ProcessRequest(req);

            if (GlobalSettings.DiagnosticsMode)
            {
                _Logger.LogMessage("US Postal Request: " + svc.LastRequest);
                _Logger.LogMessage("US Postal Response: " + svc.LastResponse);
            }

            var hasErrors = res.Errors.Count > 0;

            foreach (var possibleResponse in DomesticPackageServiceResponse.FindAll())
            {
                var AllPackagesRated = true;
                var totalRate        = 0m;

                foreach (var p in res.Packages)
                {
                    var found =
                        p.Postages.FirstOrDefault(y => y.MailServiceClassId == possibleResponse.XmlClassId);
                    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     = Id,
                        ServiceCodes  = (int)possibleResponse.ServiceType + "-" + possibleResponse.XmlClassId,
                        DisplayName   = "USPS:" + possibleResponse.XmlName
                    });
                }
            }

            return(rates);
        }
        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);
        }