public static ShippingResponse GetRate(string dataType = "json") { try { if (dataType.ToUpper() == "JSON" || dataType.ToUpper() == "JSONP") { ValidateJSON(); } else { ValidateXML(); } RateRequest standardRequest = CreateRateRequest(_auth); RateRequest freightRequest = CreateRateRequest(_auth); standardRequest.RequestedShipment.FreightShipmentDetail = null; freightRequest.RequestedShipment.RequestedPackageLineItems = null; RateService service = new RateService(_environment); // Initializes the service try { // Call the web service passing in a RateRequest and returing a RateReply decimal totalweight = standardRequest.RequestedShipment.RequestedPackageLineItems.Sum(x => x.Weight.Value); RateReply reply = new RateReply(); if (totalweight < 90) { reply = service.getRates(standardRequest); } else if (totalweight >= 150) { //reply = service.getRates(freightRequest); reply = service.getRates(standardRequest); } else { // make both calls reply = service.getRates(standardRequest); /*RateReply freightreply = service.getRates(freightRequest); List<Notification> allnotifications = new List<Notification>(); List<RateReplyDetail> alldetails = new List<RateReplyDetail>(); if (standardreply.HighestSeverity == NotificationSeverityType.SUCCESS || standardreply.HighestSeverity == NotificationSeverityType.NOTE || standardreply.HighestSeverity == NotificationSeverityType.WARNING) { reply.HighestSeverity = standardreply.HighestSeverity; allnotifications.AddRange(standardreply.Notifications.ToList<Notification>()); alldetails.AddRange(standardreply.RateReplyDetails.ToList<RateReplyDetail>()); reply.TransactionDetail = standardreply.TransactionDetail; reply.Version = standardreply.Version; } if (freightreply.HighestSeverity == NotificationSeverityType.SUCCESS || freightreply.HighestSeverity == NotificationSeverityType.NOTE || freightreply.HighestSeverity == NotificationSeverityType.WARNING) { reply.HighestSeverity = freightreply.HighestSeverity; allnotifications.AddRange(freightreply.Notifications.ToList<Notification>()); alldetails.AddRange(freightreply.RateReplyDetails.ToList<RateReplyDetail>()); reply.TransactionDetail = freightreply.TransactionDetail; reply.Version = freightreply.Version; } reply.Notifications = allnotifications.ToArray<Notification>(); reply.RateReplyDetails = alldetails.ToArray<RateReplyDetail>();*/ } ShippingResponse resp = new ShippingResponse(); // Check if the call was successful if (reply.HighestSeverity == NotificationSeverityType.SUCCESS || reply.HighestSeverity == NotificationSeverityType.NOTE || reply.HighestSeverity == NotificationSeverityType.WARNING) { List<ShipmentRateDetails> details = new List<ShipmentRateDetails>(); details = GetRateDetails(reply); resp = new ShippingResponse { Status = "OK", Status_Description = "", Result = details }; } else { List<ShipmentRateDetails> details = new List<ShipmentRateDetails>(); ShipmentRateDetails detail = new ShipmentRateDetails(); detail.Notifications = ShowNotifications(reply); details.Add(detail); resp = new ShippingResponse { Status = "ERROR", Status_Description = "", Result = details }; } return resp; } catch (SoapException e) { throw new Exception(e.Detail.InnerText); } catch (Exception e) { throw new Exception(e.Message); } } catch (Exception e) { ShippingResponse resp2 = new ShippingResponse { Status = "ERROR", Status_Description = e.Message, Result = null }; return resp2; } }
private static List<ShipmentRateDetails> GetRateDetails(RateReply reply) { List<ShipmentRateDetails> details = new List<ShipmentRateDetails>(); if (reply.RateReplyDetails != null) { foreach (RateReplyDetail replyDetail in reply.RateReplyDetails) { ShipmentRateDetails detail = new ShipmentRateDetails(); if (replyDetail.ServiceTypeSpecified) { detail.ServiceType = replyDetail.ServiceType.ToString(); } if (replyDetail.PackagingTypeSpecified) { detail.PackagingType = replyDetail.PackagingType.ToString(); } List<RateDetail> rateDetails = new List<RateDetail>(); foreach (RatedShipmentDetail shipmentDetail in replyDetail.RatedShipmentDetails) { ShipmentRateDetail shipDetail = shipmentDetail.ShipmentRateDetail; RateDetail rateDetail = new RateDetail(); rateDetail.RateType = shipDetail.RateType.ToString(); if (shipDetail.TotalBillingWeight != null) { rateDetail.TotalBillingWeight = new KeyValuePair<decimal, string>(shipDetail.TotalBillingWeight.Value, shipmentDetail.ShipmentRateDetail.TotalBillingWeight.Units.ToString()); } if (shipDetail.TotalBaseCharge != null) { rateDetail.TotalBaseCharge = new KeyValuePair<decimal, string>(shipDetail.TotalBaseCharge.Amount, shipDetail.TotalBaseCharge.Currency); } if (shipDetail.TotalFreightDiscounts != null) { rateDetail.TotalFreightDiscounts = new KeyValuePair<decimal, string>(shipDetail.TotalFreightDiscounts.Amount, shipDetail.TotalFreightDiscounts.Currency); } if (shipDetail.TotalSurcharges != null) { rateDetail.TotalFreightDiscounts = new KeyValuePair<decimal, string>(shipDetail.TotalSurcharges.Amount, shipDetail.TotalSurcharges.Currency); } if (shipDetail.Surcharges != null) { List<ShippingSurcharge> shippingSurcharges = new List<ShippingSurcharge>(); foreach (Surcharge detailSurcharge in shipDetail.Surcharges) { ShippingSurcharge surcharge = new ShippingSurcharge { Type = detailSurcharge.SurchargeType.ToString(), Charge = new KeyValuePair<decimal, string>(detailSurcharge.Amount.Amount, detailSurcharge.Amount.Currency) }; shippingSurcharges.Add(surcharge); } rateDetail.Surcharges = shippingSurcharges; } if (shipDetail.TotalNetCharge != null) { rateDetail.NetCharge = new KeyValuePair<decimal, string>(shipDetail.TotalNetCharge.Amount, shipDetail.TotalNetCharge.Currency); } rateDetails.Add(rateDetail); } detail.Rates = rateDetails; detail.TransitTime = replyDetail.TransitTime.ToString(); detail.Notifications = ShowNotifications(reply); details.Add(detail); } } return details; }