using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Services.Protocols; using System.IO; using NetLibrary.Log; using NetLibrary.Data; using System.Data.Common; using System.Data; using TradeManageNew.WebReference2; using System.Net; namespace TradeManageNew { public class FedexRateNew { public static string Key = ""; public static string Password = ""; public static string AccountNumber = ""; public static string MeterNumber = ""; public static string PayAccountNumber = ""; public static string PostInfo = ""; public static FedexFeeMdNew ormd = new FedexFeeMdNew(); public static string FeeHtml = ""; public static string HubId = ""; public static decimal Fee = 0; public static int PostId= 0; public static int Addr2 = 0; public string GetFee(FedexFeeMdNew md, out string OutError) { ormd = md; FeeHtml = ""; Fee = 0; string error = ""; //ShipmentPackage shipmentPackage = new ShipmentPackage(); //shipmentPackage.DeveloperKey = Key; //shipmentPackage.DeveloperPassword = Password; //shipmentPackage.DeveloperAccountNumber = AccountNumber; //shipmentPackage.DeveloperMeterNumber = MeterNumber; ////发货地 //shipmentPackage.OriginCity = md.SendCity;// "COMMERCE"; //shipmentPackage.OriginStateProvince = md.SendProv;// "CA"; //shipmentPackage.OriginPostalCode = md.SendPostCode;// "90040"; //shipmentPackage.OriginCountryCode = md.SendCountry;// "US"; ////目的地 //shipmentPackage.DestinationCity = md.RevCity;// "New York"; //shipmentPackage.DestinationStateProvince = md.RevProv;// "NY"; //shipmentPackage.DestinationPostalCode = md.RevPostCode;// "10021"; //shipmentPackage.DestinationCountryCode = md.RevCountry;// "US"; //if (md.WeightKg != null && md.WeightKg.Value > 0) // shipmentPackage.WeightUnits = RateAvailableServiceWebServiceClient.RateServiceWebReference.WeightUnits.KG; //else // shipmentPackage.WeightUnits = RateAvailableServiceWebServiceClient.RateServiceWebReference.WeightUnits.LB; //if (md.WeightKg != null && md.WeightKg.Value > 0) // shipmentPackage.Weight = md.WeightKg.Value; //else // shipmentPackage.Weight = md.Weight.Value * Convert.ToDecimal(0.0625); RateRequest request = CreateRateRequest(); // RateService service = new RateService(); //if (usePropertyFile()) //{ // service.Url = getProperty("endpoint"); //} service.Url = "https://ws.fedex.com:443/web-services/rate"; OutError = ""; try { // Call the web service passing in a RateRequest and returning a RateReply ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11; RateReply reply = service.getRates(request); if (reply.HighestSeverity == NotificationSeverityType.SUCCESS || reply.HighestSeverity == NotificationSeverityType.NOTE || reply.HighestSeverity == NotificationSeverityType.WARNING) { ShowRateReply(reply); } else OutError = ShowNotifications(reply); } catch (SoapException e) { OutError = e.Detail.InnerText; // Console.WriteLine(e.Detail.InnerText); } catch (Exception e) { OutError = e.Message; // Console.WriteLine(e.Message); } return FeeHtml; } private static RateRequest CreateRateRequest() { // Build the RateRequest RateRequest request = new RateRequest(); // request.WebAuthenticationDetail = new WebAuthenticationDetail(); request.WebAuthenticationDetail.UserCredential = new WebAuthenticationCredential(); request.WebAuthenticationDetail.UserCredential.Key = Key;// "JFnBk9SNRj1JeegZ"; // Replace "XXX" with the Key request.WebAuthenticationDetail.UserCredential.Password = Password;// "KE6pVQBrPQWrcXEdv3Gtya6Zy"; // Replace "XXX" with the Password //if (usePropertyFile()) //Set values from a file for testing purposes //{ // request.WebAuthenticationDetail.UserCredential.Key = getProperty("key"); // request.WebAuthenticationDetail.UserCredential.Password = getProperty("password"); //} // request.ClientDetail = new ClientDetail(); request.ClientDetail.AccountNumber = AccountNumber;// "464127968"; // Replace "XXX" with the client's account number request.ClientDetail.MeterNumber = MeterNumber;// "112179445"; // Replace "XXX" with the client's meter number //if (usePropertyFile()) //Set values from a file for testing purposes //{ // request.ClientDetail.AccountNumber = getProperty("accountnumber"); // request.ClientDetail.MeterNumber = getProperty("meternumber"); //} // request.TransactionDetail = new TransactionDetail(); request.TransactionDetail.CustomerTransactionId = "***SmartPost Rate Request using VC#***"; // This is a reference field for the customer. Any value can be used and will be provided in the response. // request.Version = new VersionId(); // request.ReturnTransitAndCommit = true; request.ReturnTransitAndCommitSpecified = true; if (ormd.PostType == "SMART_POST") { request.CarrierCodes = new CarrierCodeType[1]; request.CarrierCodes[0] = CarrierCodeType.FXSP; // FXSP is for SmartPost } else //if (ormd.PostType == "FEDEX_GROUND") { request.CarrierCodes = new CarrierCodeType[1]; request.CarrierCodes[0] = CarrierCodeType.FDXG; // FXSP is for SmartPost } // SetShipmentDetails(request); // return request; } private static void SetShipmentDetails(RateRequest request) { request.RequestedShipment = new RequestedShipment(); request.RequestedShipment.ShipTimestamp = DateTime.Now; // Shipping date and time request.RequestedShipment.ShipTimestampSpecified = true; request.RequestedShipment.DropoffType = DropoffType.REGULAR_PICKUP; if (ormd.PostType == "SMART_POST") request.RequestedShipment.ServiceType = ServiceType.SMART_POST; // Service type is SmartPost else if (ormd.PostType == "FEDEX_GROUND") request.RequestedShipment.ServiceType = ServiceType.FEDEX_GROUND; // Service type is SmartPost else if (ormd.PostType == "GROUND_HOME_DELIVERY") request.RequestedShipment.ServiceType = ServiceType.GROUND_HOME_DELIVERY; else if (ormd.PostType == "FEDEX_2_DAY") request.RequestedShipment.ServiceType = ServiceType.FEDEX_2_DAY; // Service type is FEDEX_2_DAY else if (ormd.PostType == "Freight_Economy") request.RequestedShipment.ServiceType = ServiceType.FEDEX_FREIGHT_ECONOMY; // Service type is FEDEX_2_DAY if (ormd.PostType == "GROUND_HOME_DELIVERY") request.RequestedShipment.ServiceTypeSpecified = false; else request.RequestedShipment.ServiceTypeSpecified = true; request.RequestedShipment.PackagingType = PackagingType.YOUR_PACKAGING; request.RequestedShipment.PackagingTypeSpecified = true; // SetOrigin(request); // SetDestination(request); SetSmartPostDetails(request); // SetPackageLineItems(request); // request.RequestedShipment.PackageCount = "1"; } private static void SetOrigin(RateRequest request) { request.RequestedShipment.Shipper = new Party(); request.RequestedShipment.Shipper.Address = new Address(); request.RequestedShipment.Shipper.Address.StreetLines = new string[1] { ormd.SendAddr }; request.RequestedShipment.Shipper.Address.City = ormd.SendCity;// "COMMERCE"; request.RequestedShipment.Shipper.Address.StateOrProvinceCode = ormd.SendProv;// "CA"; request.RequestedShipment.Shipper.Address.PostalCode = ormd.SendPostCode;//"90040"; request.RequestedShipment.Shipper.Address.CountryCode = ormd.SendCountry;//"US"; } private static void SetDestination(RateRequest request) { request.RequestedShipment.Recipient = new Party(); request.RequestedShipment.Recipient.Address = new Address(); request.RequestedShipment.Recipient.Address.StreetLines = new string[1] { ormd.RevAddr }; request.RequestedShipment.Recipient.Address.City = ormd.RevCity;// "Morgantown"; request.RequestedShipment.Recipient.Address.StateOrProvinceCode = ormd.RevProv;//"WV"; request.RequestedShipment.Recipient.Address.PostalCode = ormd.RevPostCode;//"26501"; request.RequestedShipment.Recipient.Address.CountryCode = ormd.RevCountry;// "US"; if (ormd.PostType == "GROUND_HOME_DELIVERY") { request.RequestedShipment.Recipient.Address.Residential = true; request.RequestedShipment.Recipient.Address.ResidentialSpecified = true; request.RequestedShipment.Recipient.Contact = new Contact(); if (ormd.RevName != null && ormd.RevName != "") request.RequestedShipment.Recipient.Contact.PersonName = ormd.RevName; else request.RequestedShipment.Recipient.Contact.PersonName = "jacky"; if (ormd.RevPhone != null && ormd.RevPhone != "") request.RequestedShipment.Recipient.Contact.PhoneNumber = ormd.RevPhone; else request.RequestedShipment.Recipient.Contact.PhoneNumber = "2392276058"; } } private static void SetSmartPostDetails(RateRequest request) { request.RequestedShipment.SmartPostDetail = new SmartPostShipmentDetail(); request.RequestedShipment.SmartPostDetail.Indicia = SmartPostIndiciaType.PARCEL_SELECT; request.RequestedShipment.SmartPostDetail.IndiciaSpecified = true; request.RequestedShipment.SmartPostDetail.AncillaryEndorsement = SmartPostAncillaryEndorsementType.ADDRESS_CORRECTION; // Replace with a valid ancillary endorsement type request.RequestedShipment.SmartPostDetail.AncillaryEndorsementSpecified = true; request.RequestedShipment.SmartPostDetail.HubId = HubId;// "5902"; // Replace "XXX" with the hub id //if (usePropertyFile()) //Set values from a file for testing purposes //{ // request.RequestedShipment.SmartPostDetail.HubId = getProperty("hubid"); //} } private static void SetPackageLineItems(RateRequest request) { request.RequestedShipment.RequestedPackageLineItems = new RequestedPackageLineItem[1]; request.RequestedShipment.RequestedPackageLineItems[0] = new RequestedPackageLineItem(); request.RequestedShipment.RequestedPackageLineItems[0].SequenceNumber = "1"; // package sequence number request.RequestedShipment.RequestedPackageLineItems[0].GroupPackageCount = "1"; // package weight request.RequestedShipment.RequestedPackageLineItems[0].Weight = new Weight(); request.RequestedShipment.RequestedPackageLineItems[0].Weight.Units = WeightUnits.LB; request.RequestedShipment.RequestedPackageLineItems[0].Weight.UnitsSpecified = true; if (ormd.OrderId == 0) request.RequestedShipment.RequestedPackageLineItems[0].Weight.Value = ormd.Weight.Value; else request.RequestedShipment.RequestedPackageLineItems[0].Weight.Value = ormd.Weight.Value * Convert.ToDecimal(0.0625); request.RequestedShipment.RequestedPackageLineItems[0].Weight.ValueSpecified = true; // package dimensions request.RequestedShipment.RequestedPackageLineItems[0].Dimensions = new Dimensions(); request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Length = ormd.Lenght;// "50"; request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Width = ormd.Width;// "20"; request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Height = ormd.Height;// "3"; request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Units = LinearUnits.IN; request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.UnitsSpecified = true; } private static void ShowRateReply(RateReply reply) { //Console.WriteLine("RateReply details:"); foreach (RateReplyDetail rateReplyDetail in reply.RateReplyDetails) { if (rateReplyDetail.ServiceTypeSpecified) FeeHtml += "Service Type:" + rateReplyDetail.ServiceType + "
"; if (rateReplyDetail.PackagingTypeSpecified) FeeHtml += "Packaging Type:" + rateReplyDetail.ServiceType + "
"; // Console.WriteLine(); foreach (RatedShipmentDetail shipmentDetail in rateReplyDetail.RatedShipmentDetails) { ShowShipmentRateDetails(shipmentDetail); // Console.WriteLine(); } // ShowDeliveryDetails(rateReplyDetail); // Console.WriteLine("**********************************************************"); } } private static void ShowShipmentRateDetails(RatedShipmentDetail shipmentDetail) { if (shipmentDetail == null) return; if (shipmentDetail.ShipmentRateDetail == null) return; ShipmentRateDetail rateDetail = shipmentDetail.ShipmentRateDetail; // Console.WriteLine("--- Shipment Rate Detail ---"); // FeeHtml += "RateType:" + rateDetail.RateType; if (rateDetail.TotalBillingWeight != null) FeeHtml += "Total Billing Weight:" + rateDetail.TotalBillingWeight.Value.ToString() + shipmentDetail.ShipmentRateDetail.TotalBillingWeight.Units.ToString() + "
"; if (rateDetail.TotalBaseCharge != null) FeeHtml += "Total Base Charge:" + rateDetail.TotalBaseCharge.Amount.ToString() + rateDetail.TotalBaseCharge.Currency.ToString() + "
"; if (rateDetail.TotalFreightDiscounts != null) FeeHtml += "Total Freight Discounts:" + rateDetail.TotalFreightDiscounts.Amount.ToString() + rateDetail.TotalFreightDiscounts.Currency.ToString() + "
"; if (rateDetail.TotalSurcharges != null) FeeHtml += "Total Surcharges:" + rateDetail.TotalSurcharges.Amount.ToString() + rateDetail.TotalSurcharges.Currency.ToString() + "
"; if (rateDetail.Surcharges != null) { // Individual surcharge for each package foreach (Surcharge surcharge in rateDetail.Surcharges) FeeHtml += surcharge.SurchargeType + " surcharge:" + surcharge.Amount.Amount + surcharge.Amount.Currency + "
"; } if (rateDetail.TotalNetCharge != null) { if (Fee > 0 && rateDetail.TotalNetCharge.Amount > Fee) { Fee = 0; //多个费用 } else { Fee = rateDetail.TotalNetCharge.Amount * 1.15m; //2024-01-07,将Fedex预估运费改为Fedex接口返回费用*1.15 FeeHtml += "Total Net Charge:" + rateDetail.TotalNetCharge.Amount*1.15m + rateDetail.TotalNetCharge.Currency; UpdateFee(); } } } #region 修改fee public static void UpdateFee() { try { if (ormd.OrderId != null && ormd.OrderId > 0 && ormd.IsHWC==1) { string tsql = ""; tsql = @" Update [DT_OrderInfo] set Fee1=@Fee*1.05,FeeLog=@FeeHtml,FeeDate=getdate() where OrderId=@OrderId "; Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@OrderId", DbType.Int32, ormd.OrderId); db.AddInParameter(cmd, "@Fee", DbType.Decimal, Fee); db.AddInParameter(cmd, "@FeeHtml", DbType.String, FeeHtml); db.ExecuteNonQuery(cmd); } else if (ormd.OrderId != null && ormd.OrderId > 0&&ormd.OrderGoodsId != null && ormd.OrderGoodsId > 0) { string tsql = ""; if (PostId == 237) { tsql = @" Update [DT_SpareFee] set [Fee11]=@Fee where Id=@Id "; if (ormd.PostType == "FEDEX_GROUND") tsql = @" Update [DT_SpareFee] set [Fee22]=@Fee where Id=@Id"; if (ormd.PostType == "GROUND_HOME_DELIVERY") tsql = @" Update [DT_SpareFee] set [Fee33]=@Fee where Id=@Id"; if (ormd.PostType == "FEDEX_2_DAY") tsql = @" Update [DT_SpareFee] set [Fee44]=@Fee where Id=@Id"; } else { tsql = @" Update [DT_SpareFee] set [Fee1]=@Fee where Id=@Id "; if (ormd.PostType == "FEDEX_GROUND") tsql = @" Update [DT_SpareFee] set [Fee2]=@Fee where Id=@Id"; if (ormd.PostType == "GROUND_HOME_DELIVERY") tsql = @" Update [DT_SpareFee] set [Fee3]=@Fee where Id=@Id"; if (ormd.PostType == "FEDEX_2_DAY") tsql = @" Update [DT_SpareFee] set [Fee4]=@Fee where Id=@Id"; } Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@Id", DbType.Int32, ormd.OrderGoodsId); db.AddInParameter(cmd, "@Fee", DbType.Decimal, Fee); db.AddInParameter(cmd, "@FeeHtml", DbType.String, FeeHtml); db.ExecuteNonQuery(cmd); } else if (ormd.OrderId != null && ormd.OrderId > 0) { string tsql = ""; if (PostId == 237) { tsql = @" Update [DT_OrderInfo] set Fee11=@Fee,FeeLog=@FeeHtml where OrderId=@OrderId "; if (ormd.PostType == "FEDEX_GROUND") tsql = @" Update [DT_OrderInfo] set Fee12=@Fee,FeeLog=@FeeHtml where OrderId=@OrderId"; if (ormd.PostType == "GROUND_HOME_DELIVERY") tsql = @" Update [DT_OrderInfo] set Fee13=@Fee,FeeLog=@FeeHtml where OrderId=@OrderId"; if (ormd.PostType == "FEDEX_2_DAY") tsql = @" Update [DT_OrderInfo] set Fee14=@Fee,FeeLog=@FeeHtml where OrderId=@OrderId"; } else { tsql = @" Update [DT_OrderInfo] set Fee6=@Fee,FeeLog=@FeeHtml where OrderId=@OrderId "; if (ormd.PostType == "FEDEX_GROUND") tsql = @" Update [DT_OrderInfo] set Fee7=@Fee,FeeLog=@FeeHtml where OrderId=@OrderId"; if (ormd.PostType == "GROUND_HOME_DELIVERY") tsql = @" Update [DT_OrderInfo] set Fee8=@Fee,FeeLog=@FeeHtml where OrderId=@OrderId"; if (ormd.PostType == "FEDEX_2_DAY") tsql = @" Update [DT_OrderInfo] set Fee10=@Fee,FeeLog=@FeeHtml where OrderId=@OrderId"; } Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@OrderId", DbType.Int32, ormd.OrderId); db.AddInParameter(cmd, "@Fee", DbType.Decimal, Fee); db.AddInParameter(cmd, "@FeeHtml", DbType.String, FeeHtml); db.ExecuteNonQuery(cmd); } else if (ormd.OrderGoodsId != null && ormd.OrderGoodsId > 0) { string tsql = @" Update [DT_OrderGoods] set Fee6=@Fee where Id=@Id declare @OrderId int select @OrderId=OrderId from DT_OrderGoods where Id=@Id update DT_OrderInfo set Fee6=( select SUM(isnull(fee6,0)) from DT_OrderGoods where OrderId=@OrderId) where OrderId=@OrderId "; if (ormd.PostType == "FEDEX_GROUND") tsql = @" Update [DT_OrderGoods] set Fee7=@Fee where Id=@Id declare @OrderId int select @OrderId=OrderId from DT_OrderGoods where Id=@Id update DT_OrderInfo set Fee7=( select SUM(isnull(fee7,0)) from DT_OrderGoods where OrderId=@OrderId) where OrderId=@OrderId "; if (ormd.PostType == "GROUND_HOME_DELIVERY") tsql = @" Update [DT_OrderGoods] set Fee8=@Fee where Id=@Id declare @OrderId int select @OrderId=OrderId from DT_OrderGoods where Id=@Id update DT_OrderInfo set Fee8=( select SUM(isnull(fee8,0)) from DT_OrderGoods where OrderId=@OrderId) where OrderId=@OrderId "; if (ormd.PostType == "FEDEX_2_DAY") tsql = @" Update [DT_OrderGoods] set Fee9=@Fee where Id=@Id declare @OrderId int select @OrderId=OrderId from DT_OrderGoods where Id=@Id update DT_OrderInfo set Fee10=( select SUM(isnull(fee8,0)) from DT_OrderGoods where OrderId=@OrderId) where OrderId=@OrderId "; Database db = DatabaseFactory.CreateDatabase(); DbCommand cmd = db.GetSqlStringCommand(tsql); db.AddInParameter(cmd, "@Id", DbType.Int32, ormd.OrderGoodsId); db.AddInParameter(cmd, "@Fee", DbType.Decimal, Fee); db.AddInParameter(cmd, "@FeeHtml", DbType.String, FeeHtml); db.ExecuteNonQuery(cmd); } } catch (Exception ex) { ErrorFollow.TraceWrite("UpdateFee", "", ex.Message); } } #endregion private static void ShowDeliveryDetails(RateReplyDetail rateDetail) { if (rateDetail.DeliveryTimestampSpecified) Console.WriteLine("Delivery timestamp: " + rateDetail.DeliveryTimestamp.ToString()); if (rateDetail.TransitTimeSpecified) Console.WriteLine("Transit time: " + rateDetail.TransitTime); } private static string ShowNotifications(RateReply reply) { // Console.WriteLine("Notifications"); string a = ""; for (int i = 0; i < reply.Notifications.Length; i++) { Notification notification = reply.Notifications[i]; a += "Code:" + notification.Code + ";Message:" + notification.Message + ";Source:" + notification.Source; //Console.WriteLine("Notification no. {0}", i); //Console.WriteLine(" Severity: {0}", notification.Severity); //Console.WriteLine(" Code: {0}", notification.Code); //Console.WriteLine(" Message: {0}", notification.Message); //Console.WriteLine(" Source: {0}", notification.Source); } return a; } private static bool usePropertyFile() //Set to true for common properties to be set with getProperty function. { return getProperty("usefile").Equals("True"); } private static String getProperty(String propertyname) //Sets common properties for testing purposes. { try { String filename = "C:\\filepath\\filename.txt"; if (System.IO.File.Exists(filename)) { System.IO.StreamReader sr = new System.IO.StreamReader(filename); do { String[] parts = sr.ReadLine().Split(','); if (parts[0].Equals(propertyname) && parts.Length == 2) { return parts[1]; } } while (!sr.EndOfStream); } Console.WriteLine("Property {0} set to default 'XXX'", propertyname); return "XXX"; } catch (Exception e) { Console.WriteLine("Property {0} set to default 'XXX'", propertyname); return "XXX"; } } } public class FedexFeeMdNew { public int? IsHWC { get; set; } public string Height { get; set; } public string Lenght { get; set; } public int? OrderId { get; set; } public int? OrderGoodsId { get; set; } public string PostType { get; set; } public string RevAddr { get; set; } public string RevCity { get; set; } public string RevCountry { get; set; } public string RevName { get; set; } public string RevPhone { get; set; } public string RevPostCode { get; set; } public string RevProv { get; set; } public string SendAddr { get; set; } public string SendName { get; set; } public string SendCity { get; set; } public string SendCountry { get; set; } public string SendPostCode { get; set; } public string SendProv { get; set; } public decimal? Weight { get; set; } public decimal? WeightKg { get; set; } public string Width { get; set; } public string packType { get; set; } public string TotalNum { get; set; } } }