public static ShippingRate RatePackage(FedExGlobalServiceSettings globals, ILogger logger, FedExServiceSettings settings, IShipment package) { var result = new ShippingRate(); // Get ServiceType var currentServiceType = (ServiceType)settings.ServiceCode; // Get PackageType var currentPackagingType = (PackageType)settings.Packaging; // Set max weight by service var carCode = GetCarrierCode(currentServiceType); // set the negotiated rates to true, or the setting in the local settings var useNegotiatedRates = !settings.ContainsKey("UseNegotiatedRates") || bool.Parse(settings["UseNegotiatedRates"]); result.EstimatedCost = RateSinglePackage(globals, logger, package, currentServiceType, currentPackagingType, carCode, useNegotiatedRates); return(result); }
private static decimal RateSinglePackage(FedExGlobalServiceSettings globalSettings, ILogger logger, IShipment pak, ServiceType service, PackageType packaging, CarrierCodeType carCode, bool useNegotiatedRates) { var result = 0m; try { // Auth Header Data var req = new RateRequest { WebAuthenticationDetail = new WebAuthenticationDetail { UserCredential = new WebAuthenticationCredential { Key = globalSettings.UserKey, Password = globalSettings.UserPassword } } }; req.ClientDetail = new ClientDetail { AccountNumber = globalSettings.AccountNumber, MeterNumber = globalSettings.MeterNumber, IntegratorId = "Hotcakes" }; req.Version = new VersionId(); // Basic Transaction Data req.TransactionDetail = new TransactionDetail { CustomerTransactionId = Guid.NewGuid().ToString() }; req.ReturnTransitAndCommit = false; req.CarrierCodes = new CarrierCodeType[1] { carCode }; // Shipment Details req.RequestedShipment = new RequestedShipment { LabelSpecification = new LabelSpecification { ImageType = ShippingDocumentImageType.PDF, LabelFormatType = LabelFormatType.COMMON2D, CustomerSpecifiedDetail = new CustomerSpecifiedLabelDetail() }, RateRequestTypes = new[] { RateRequestType.LIST }, DropoffType = GetDropOffType(globalSettings.DefaultDropOffType), PackagingType = GetPackageType(packaging), TotalWeight = new Weight { Value = Math.Round(pak.Items.Sum(y => y.BoxWeight), 1), Units = pak.Items[0].BoxWeightType == Shipping.WeightType.Kilograms ? WeightUnits.KG : WeightUnits.LB }, PackageCount = pak.Items.Count.ToString(), RequestedPackageLineItems = new RequestedPackageLineItem[pak.Items.Count] }; /* * Possible values for RateRequestType include: LIST and ACCOUNT. * ACCOUNT will return negotiated rates, but LIST will return both regular and negotiated rates. * http://www.fedex.com/us/developer/product/WebServices/MyWebHelp_March2010/Content/WS_Developer_Guide/Rate_Services.htm */ //req.RequestedShipment.RateRequestTypes = new[] { RateRequestType.ACCOUNT }; // Uncomment these lines to get insured values passed in // //var totalValue = pak.Items.Sum(y => y.BoxValue); //req.RequestedShipment.TotalInsuredValue = new Money(); //req.RequestedShipment.TotalInsuredValue.Amount = totalValue; //req.RequestedShipment.TotalInsuredValue.Currency = "USD"; for (var i = 0; i < pak.Items.Count; i++) { req.RequestedShipment.RequestedPackageLineItems[i] = new RequestedPackageLineItem { GroupNumber = "1", GroupPackageCount = (i + 1).ToString(), Weight = new Weight { Value = pak.Items[i].BoxWeight, Units = pak.Items[i].BoxWeightType == Shipping.WeightType.Kilograms ? WeightUnits.KG : WeightUnits.LB } }; req.RequestedShipment.RequestedPackageLineItems[i].Dimensions = new Dimensions { Height = pak.Items[i].BoxHeight.ToString(), Length = pak.Items[i].BoxLength.ToString(), Width = pak.Items[i].BoxWidth.ToString(), Units = pak.Items[i].BoxLengthType == LengthType.Centimeters ? LinearUnits.CM : LinearUnits.IN }; } req.RequestedShipment.Recipient = new Party { Address = new FedExRateServices.Address { City = pak.DestinationAddress.City, CountryCode = GetCountryCode(pak.DestinationAddress.CountryData), PostalCode = pak.DestinationAddress.PostalCode } }; if (pak.DestinationAddress.CountryData.IsoCode == "US" || pak.DestinationAddress.CountryData.IsoCode == "CA") { req.RequestedShipment.Recipient.Address.StateOrProvinceCode = pak.DestinationAddress.RegionBvin; } else { req.RequestedShipment.Recipient.Address.StateOrProvinceCode = string.Empty; } req.RequestedShipment.Recipient.Address.StreetLines = new string[2] { pak.DestinationAddress.Street, pak.DestinationAddress.Street2 }; switch (service) { case ServiceType.GROUNDHOMEDELIVERY: req.RequestedShipment.Recipient.Address.Residential = true; req.RequestedShipment.Recipient.Address.ResidentialSpecified = true; break; case ServiceType.FEDEXGROUND: req.RequestedShipment.Recipient.Address.Residential = false; req.RequestedShipment.Recipient.Address.ResidentialSpecified = true; break; default: req.RequestedShipment.Recipient.Address.ResidentialSpecified = false; break; } req.RequestedShipment.Shipper = new Party { AccountNumber = globalSettings.AccountNumber, Address = new FedExRateServices.Address { City = pak.SourceAddress.City, CountryCode = GetCountryCode(pak.SourceAddress.CountryData), PostalCode = pak.SourceAddress.PostalCode, Residential = false } }; if (pak.SourceAddress.CountryData.IsoCode == "US" || // US or Canada pak.SourceAddress.CountryData.IsoCode == "CA") { req.RequestedShipment.Shipper.Address.StateOrProvinceCode = pak.SourceAddress.RegionBvin; } else { req.RequestedShipment.Shipper.Address.StateOrProvinceCode = string.Empty; } req.RequestedShipment.Shipper.Address.StreetLines = new string[2] { pak.SourceAddress.Street, pak.SourceAddress.Street2 }; var svc = new FedExRateServices.RateService { Url = globalSettings.UseDevelopmentServiceUrl ? FedExConstants.DevRateServiceUrl : FedExConstants.LiveRateServiceUrl }; var res = svc.getRates(req); if (res.HighestSeverity == NotificationSeverityType.ERROR || res.HighestSeverity == NotificationSeverityType.FAILURE) { if (globalSettings.DiagnosticsMode) { foreach (var err in res.Notifications) { logger.LogMessage("FEDEX", err.Message, EventLogSeverity.Debug); } } result = 0m; } else { result = 0m; var lookingForService = GetServiceType(service); var matchingResponse = res.RateReplyDetails.FirstOrDefault(y => y.ServiceType == lookingForService); if (matchingResponse != null) { if (useNegotiatedRates) { var matchedRate = matchingResponse.RatedShipmentDetails.FirstOrDefault( y => y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_ACCOUNT_PACKAGE || y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_ACCOUNT_SHIPMENT); if (matchedRate != null) { result = matchedRate.ShipmentRateDetail.TotalNetCharge.Amount; if (globalSettings.DiagnosticsMode) { logger.LogMessage("FEDEX SHIPMENT", "Negotiated rates were found and are currently being used.", EventLogSeverity.Information); } } else { matchedRate = matchingResponse.RatedShipmentDetails.FirstOrDefault( y => y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_LIST_PACKAGE || y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_LIST_SHIPMENT); result = matchedRate.ShipmentRateDetail.TotalNetCharge.Amount; if (globalSettings.DiagnosticsMode) { logger.LogMessage("FEDEX SHIPMENT", "No negotiated rates were found. Public rates are being shown. You should update your account information, or uncheck the ‘Use Negotiated Rates’ checkbox.", EventLogSeverity.Information); } else { logger.LogMessage("FEDEX SHIPMENT", "No negotiated rates were found.", EventLogSeverity.Information); } } } else { var matchedRate = matchingResponse.RatedShipmentDetails.FirstOrDefault( y => y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_LIST_PACKAGE || y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_LIST_SHIPMENT); if (matchedRate != null) { result = matchedRate.ShipmentRateDetail.TotalNetCharge.Amount; if ( matchingResponse.RatedShipmentDetails.Any( y => y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_ACCOUNT_PACKAGE || y.ShipmentRateDetail.RateType == ReturnedRateType.PAYOR_ACCOUNT_SHIPMENT)) { if (globalSettings.DiagnosticsMode) { logger.LogMessage("FEDEX SHIPMENT", "We also found negotiated rates for your account. You should consider checking the ‘Use Negotiated Rates’ checkbox.", EventLogSeverity.Information); } } } } } } } catch (Exception ex) { result = 0m; logger.LogException(ex); } return(result); }
public FedExProvider(FedExGlobalServiceSettings globalSettings, ILogger logger) { _Logger = logger; GlobalSettings = globalSettings; Settings = new FedExServiceSettings(); }