You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
17 KiB
C#

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 NetLibrary.WebReference1;
namespace NetLibrary.Express
{
public class FedexRateHome
{
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 FedexFeeMd ormd = new FedexFeeMd();
public static string FeeHtml = "";
public static string HubId = "";
public static decimal Fee = 0;
public string GetFee(FedexFeeMd 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
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;
// request.CarrierCodes = new CarrierCodeType[1];
// request.CarrierCodes[0] = CarrierCodeType.FXSP; // 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 == "GROUND_HOME_DELIVERY")
request.RequestedShipment.ServiceType = ServiceType.GROUND_HOME_DELIVERY; // Service type is SmartPost
request.RequestedShipment.ServiceTypeSpecified = false;
request.RequestedShipment.PackagingType = PackagingType.YOUR_PACKAGING;
request.RequestedShipment.PackagingTypeSpecified = false;
//
SetOrigin(request);
//
SetDestination(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";
}
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;
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 + "<br/>";
if (rateReplyDetail.PackagingTypeSpecified)
FeeHtml += "Packaging Type:" + rateReplyDetail.ServiceType + "<br/>";
// 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() + "<br/>";
if (rateDetail.TotalBaseCharge != null) FeeHtml += "Total Base Charge:" + rateDetail.TotalBaseCharge.Amount.ToString() + rateDetail.TotalBaseCharge.Currency.ToString() + "<br/>";
if (rateDetail.TotalFreightDiscounts != null) FeeHtml += "Total Freight Discounts:" + rateDetail.TotalFreightDiscounts.Amount.ToString() + rateDetail.TotalFreightDiscounts.Currency.ToString() + "<br/>";
if (rateDetail.TotalSurcharges != null) FeeHtml += "Total Surcharges:" + rateDetail.TotalSurcharges.Amount.ToString() + rateDetail.TotalSurcharges.Currency.ToString() + "<br/>";
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 + "<br/>";
}
if (rateDetail.TotalNetCharge != null)
{
Fee = rateDetail.TotalNetCharge.Amount;
FeeHtml += "Total Net Charge:" + rateDetail.TotalNetCharge.Amount + rateDetail.TotalNetCharge.Currency;
UpdateFee();
}
}
#region 修改tocken
public static void UpdateFee()
{
try
{
string tsql = @"
Update [DT_OrderInfo] set Fee8=@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);
}
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 FedexFeeMd
{
public string Height { get; set; }
public string Lenght { get; set; }
public int? OrderId { 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 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; }
}
}