|
|
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.RateWebReference;
|
|
|
using System.Net;
|
|
|
using Newtonsoft.Json;
|
|
|
using System.Net.Http.Headers;
|
|
|
using System.Net.Http;
|
|
|
using System.Diagnostics.Eventing.Reader;
|
|
|
using NetLibrary;
|
|
|
|
|
|
namespace TradeManageNew
|
|
|
{
|
|
|
public class UPSRate
|
|
|
{
|
|
|
const string client_id = "NeG6fE37MEZAjzzWebCyd1gM78jUa5kt741VJLmiiF0mKx5D";
|
|
|
const string client_secret = "f7jmbk077R94iTzW3gKRBePReNjqR6XtoHxZ5q10cvxmUrxxcG6m0Kdm61oEGhmY";
|
|
|
public static string AccessLicenseNumber = "";
|
|
|
public static string Username = "";
|
|
|
public static string Password = "";
|
|
|
public static string ShipperNumber = "";
|
|
|
public string GetFee(FedexFeeMdNew md)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
|
|
|
RateService rate = new RateService();
|
|
|
RateRequest rateRequest = new RateRequest();
|
|
|
UPSSecurity upss = new UPSSecurity();
|
|
|
UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSSecurityServiceAccessToken();
|
|
|
upssSvcAccessToken.AccessLicenseNumber = AccessLicenseNumber;// "2D7E03BB362512B5";//4D80C6335333C435
|
|
|
upss.ServiceAccessToken = upssSvcAccessToken;
|
|
|
|
|
|
UPSSecurityUsernameToken upssUsrNameToken = new UPSSecurityUsernameToken();
|
|
|
upssUsrNameToken.Username = Username;// "simon@nbboson.com";
|
|
|
upssUsrNameToken.Password = Password;// "Km28862820";
|
|
|
upss.UsernameToken = upssUsrNameToken;
|
|
|
rate.UPSSecurityValue = upss;
|
|
|
RequestType request = new RequestType();
|
|
|
String[] requestOption = { "Rate" };
|
|
|
request.RequestOption = requestOption;
|
|
|
rateRequest.Request = request;
|
|
|
ShipmentType shipment = new ShipmentType();
|
|
|
ShipperType shipper = new ShipperType();
|
|
|
shipper.ShipperNumber = ShipperNumber;// "RE8474";
|
|
|
RateWebReference.AddressType shipperAddress = new RateWebReference.AddressType();
|
|
|
String[] addressLine = {md.SendAddr };// { "5555 main", "4 Case Cour", "Apt 3B" };Id CompanyId SendName SendEnglishName Address EnglishAddress Phone PostCode province city county street provincecode citycode
|
|
|
// 3 1 bosonshop Karmas Far Inc 2611 E LINDSAY PRIVADO 2611 E LINDSAY PRIVADO 9012637906 91761 CA ONTARIO US 315000 CA ONTARIO
|
|
|
shipperAddress.AddressLine = addressLine;
|
|
|
shipperAddress.City = md.SendCity;// "ONTARIO";
|
|
|
shipperAddress.PostalCode = md.SendPostCode;// "91761";
|
|
|
shipperAddress.StateProvinceCode = md.SendProv;// "CA";
|
|
|
shipperAddress.CountryCode = md.SendCountry;// "US";
|
|
|
shipperAddress.AddressLine = addressLine;
|
|
|
shipper.Address = shipperAddress;
|
|
|
shipment.Shipper = shipper;
|
|
|
ShipFromType shipFrom = new ShipFromType();
|
|
|
RateWebReference.ShipAddressType shipFromAddress = new RateWebReference.ShipAddressType();
|
|
|
shipFromAddress.AddressLine = addressLine;
|
|
|
shipFromAddress.City = md.SendCity;// "ONTARIO";
|
|
|
shipFromAddress.PostalCode = md.SendPostCode;//"91761";
|
|
|
shipFromAddress.StateProvinceCode=md.SendProv;// "CA";
|
|
|
shipFromAddress.CountryCode = md.SendCountry;//"US";
|
|
|
shipFrom.Address = shipFromAddress;
|
|
|
shipment.ShipFrom = shipFrom;
|
|
|
ShipToType shipTo = new ShipToType();
|
|
|
ShipToAddressType shipToAddress = new ShipToAddressType();
|
|
|
String[] addressLine1 = {md.RevAddr };// { "10 E. Ritchie Way", "2", "Apt 3B" };
|
|
|
shipToAddress.AddressLine = addressLine1;
|
|
|
shipToAddress.City = md.RevCity;// "RENO";
|
|
|
shipToAddress.PostalCode = md.RevPostCode;// "89509";
|
|
|
shipToAddress.StateProvinceCode = md.RevProv;// "NV";
|
|
|
shipToAddress.CountryCode = md.RevCountry;// "US";
|
|
|
shipTo.Address = shipToAddress;
|
|
|
shipment.ShipTo = shipTo;
|
|
|
ShipmentRatingOptionsType shipmentRatingOptions = new ShipmentRatingOptionsType();
|
|
|
|
|
|
shipmentRatingOptions.NegotiatedRatesIndicator = "1";
|
|
|
shipment.ShipmentRatingOptions = shipmentRatingOptions;
|
|
|
//shipmentRatingOptions.FRSShipmentIndicator = "1";
|
|
|
//shipmentRatingOptions.RateChartIndicator = "1";
|
|
|
//shipmentRatingOptions.UserLevelDiscountIndicator = "1";
|
|
|
|
|
|
|
|
|
CodeDescriptionType service = new CodeDescriptionType();
|
|
|
//92= UPS SurePost® Less than 1 lb 93 = UPS SurePost® 1 lb or Greater 94 = UPS SurePost® BPM 95 = UPS SurePost® Media
|
|
|
|
|
|
//Below code uses dummy date for reference. Please udpate as required.
|
|
|
service.Code = md.PostType;// "03";
|
|
|
shipment.Service = service;
|
|
|
PackageType package = new PackageType();
|
|
|
PackageWeightType packageWeight = new PackageWeightType();
|
|
|
packageWeight.Weight = md.Weight.ToString();// "36";
|
|
|
|
|
|
CodeDescriptionType uom = new CodeDescriptionType();
|
|
|
uom.Code = "LBS";
|
|
|
uom.Description = "pounds";
|
|
|
|
|
|
packageWeight.UnitOfMeasurement = uom;
|
|
|
package.PackageWeight = packageWeight;
|
|
|
|
|
|
CodeDescriptionType packType = new CodeDescriptionType();
|
|
|
packType.Code = md.packType;// "02";
|
|
|
package.PackagingType = packType;
|
|
|
|
|
|
DimensionsType dm = new DimensionsType();
|
|
|
dm.Height = md.Height;
|
|
|
dm.Length = md.Lenght;
|
|
|
dm.Width = md.Width;
|
|
|
CodeDescriptionType uom2 = new CodeDescriptionType();
|
|
|
uom2.Code = "IN";
|
|
|
uom2.Description = "inches";
|
|
|
dm.UnitOfMeasurement = uom2;
|
|
|
package.Dimensions = dm;
|
|
|
//package.Dimensions.Length = md.Lenght;
|
|
|
//package.Dimensions.Width = md.Width;
|
|
|
//package.Dimensions.Height = md.Height;
|
|
|
|
|
|
PackageType[] pkgArray = { package };
|
|
|
shipment.Package = pkgArray;
|
|
|
rateRequest.Shipment = shipment;
|
|
|
|
|
|
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
|
|
|
// System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11; //This line will ensure the latest security protocol for consuming the web service call.
|
|
|
//Console.WriteLine(rateRequest);
|
|
|
rate.Url = "https://onlinetools.ups.com/webservices/Rate";
|
|
|
RateResponse rateResponse = rate.ProcessRate(rateRequest);
|
|
|
string result = "";
|
|
|
result+="The transaction was a " + rateResponse.Response.ResponseStatus.Description+"<br/>";
|
|
|
if(rateResponse.RatedShipment[0].NegotiatedRateCharges!=null)
|
|
|
result += "Total Shipment NegotiatedRateCharges Charges " + rateResponse.RatedShipment[0].NegotiatedRateCharges.TotalCharge.MonetaryValue + rateResponse.RatedShipment[0].NegotiatedRateCharges.TotalCharge.CurrencyCode+"<br/>";
|
|
|
result += "Total Shipment Charges " + rateResponse.RatedShipment[0].TotalCharges.MonetaryValue + rateResponse.RatedShipment[0].TotalCharges.CurrencyCode;
|
|
|
return result;
|
|
|
//Console.WriteLine("The transaction was a " + rateResponse.Response.ResponseStatus.Description);
|
|
|
//Console.WriteLine("Total Shipment Charges " + rateResponse.RatedShipment[0].TotalCharges.MonetaryValue + rateResponse.RatedShipment[0].TotalCharges.CurrencyCode);
|
|
|
//Console.ReadKey();
|
|
|
}
|
|
|
//catch (System.Web.Services.Protocols.SoapException ex)
|
|
|
//{
|
|
|
// Console.WriteLine("");
|
|
|
// Console.WriteLine("---------Rate Web Service returns error----------------");
|
|
|
// Console.WriteLine("---------\"Hard\" is user error \"Transient\" is system error----------------");
|
|
|
// Console.WriteLine("SoapException Message= " + ex.Message);
|
|
|
// Console.WriteLine("");
|
|
|
// Console.WriteLine("SoapException Category:Code:Message= " + ex.Detail.LastChild.InnerText);
|
|
|
// Console.WriteLine("");
|
|
|
// Console.WriteLine("SoapException XML String for all= " + ex.Detail.LastChild.OuterXml);
|
|
|
// Console.WriteLine("");
|
|
|
// Console.WriteLine("SoapException StackTrace= " + ex.StackTrace);
|
|
|
// Console.WriteLine("-------------------------");
|
|
|
// Console.WriteLine("");
|
|
|
//}
|
|
|
//catch (System.ServiceModel.CommunicationException ex)
|
|
|
//{
|
|
|
// Console.WriteLine("");
|
|
|
// Console.WriteLine("--------------------");
|
|
|
// Console.WriteLine("CommunicationException= " + ex.Message);
|
|
|
// Console.WriteLine("CommunicationException-StackTrace= " + ex.StackTrace);
|
|
|
// Console.WriteLine("-------------------------");
|
|
|
// Console.WriteLine("");
|
|
|
|
|
|
//}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
Console.WriteLine("");
|
|
|
Console.WriteLine("-------------------------");
|
|
|
Console.WriteLine(" Generaal Exception= " + ex.Message);
|
|
|
Console.WriteLine(" Generaal Exception-StackTrace= " + ex.StackTrace);
|
|
|
Console.WriteLine("-------------------------");
|
|
|
return "error:"+ex.Message;
|
|
|
}
|
|
|
//finally
|
|
|
//{
|
|
|
// Console.ReadKey();
|
|
|
// return "";
|
|
|
//}
|
|
|
}
|
|
|
|
|
|
public string GetFeeNew(FedexFeeMdNew md)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
//var URL = "https://wwwcie.ups.com/api/rating/v2205/Rate";//测试地址
|
|
|
var URL = "https://onlinetools.ups.com/api/rating/v2205/Rate";//正式地址
|
|
|
var inquiryNumber = DateTime.Now.ToString("yyyyMMddhhmmss");
|
|
|
|
|
|
var token = UPSTokenHelp.GetUPSToken();
|
|
|
// 创建HttpClient实例
|
|
|
HttpClient client = new HttpClient();
|
|
|
// 设置Basic Auth验证
|
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
|
|
//生成一个随机数
|
|
|
var random = new Random().Next(1000, 9990);
|
|
|
// 添加自定义Header
|
|
|
client.DefaultRequestHeaders.Add("transId", inquiryNumber + random.ToString() + "Id");
|
|
|
client.DefaultRequestHeaders.Add("transactionSrc", inquiryNumber + random.ToString() + "Src");
|
|
|
// 创建 HttpRequestMessage 实例
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, URL);
|
|
|
//根据输入商品的长宽高计算运费,不使用SimpleRate计算
|
|
|
var rating = new RateRequestModel()
|
|
|
{
|
|
|
RateRequest = new RateRequestNew
|
|
|
{
|
|
|
Request = new Request
|
|
|
{
|
|
|
RequestOption = "Rate",
|
|
|
SubVersion = "2205",
|
|
|
TransactionReference = new TransactionReference
|
|
|
{
|
|
|
CustomerContext = "Retrieve negotiated rates"
|
|
|
}
|
|
|
},
|
|
|
PickupType = new PickupType
|
|
|
{
|
|
|
Code = "01"
|
|
|
},
|
|
|
Shipment = new Shipment
|
|
|
{
|
|
|
Shipper = new Shipper
|
|
|
{
|
|
|
Name = "",
|
|
|
ShipperNumber = ShipperNumber,
|
|
|
Address = new AddressFrom
|
|
|
{
|
|
|
AddressLine = new List<string>() { md.SendAddr },
|
|
|
City = md.SendCity,
|
|
|
StateProvinceCode = md.SendProv,
|
|
|
PostalCode = md.SendPostCode,
|
|
|
CountryCode = md.SendCountry
|
|
|
}
|
|
|
},
|
|
|
ShipTo = new ShipTo
|
|
|
{
|
|
|
Name = "",
|
|
|
Address = new AddressTo
|
|
|
{
|
|
|
AddressLine = new List<string>() { md.RevAddr },
|
|
|
City = md.RevCity,
|
|
|
StateProvinceCode = md.RevProv,
|
|
|
PostalCode = md.RevPostCode,
|
|
|
CountryCode = md.RevCountry,
|
|
|
ResidentialAddressIndicator = "True"
|
|
|
}
|
|
|
},
|
|
|
ShipFrom = new ShipFrom
|
|
|
{
|
|
|
Name = "",
|
|
|
Address = new AddressFrom
|
|
|
{
|
|
|
AddressLine = new List<string>() { md.SendAddr },
|
|
|
City = md.SendCity,
|
|
|
StateProvinceCode = md.SendProv,
|
|
|
PostalCode = md.SendPostCode,
|
|
|
CountryCode = md.SendCountry
|
|
|
}
|
|
|
},
|
|
|
PaymentDetails = new PaymentDetails
|
|
|
{
|
|
|
ShipmentCharge = new List<ShipmentCharge> { new ShipmentCharge
|
|
|
{
|
|
|
Type = "01",
|
|
|
BillShipper = new BillShipper
|
|
|
{
|
|
|
AccountNumber = ShipperNumber
|
|
|
}
|
|
|
} },
|
|
|
},
|
|
|
ShipmentRatingOptions=new ShipmentRatingOptions
|
|
|
{
|
|
|
TPFCNegotiatedRatesIndicator="Y",
|
|
|
NegotiatedRatesIndicator="Y"
|
|
|
},
|
|
|
|
|
|
Service = new Service
|
|
|
{
|
|
|
Code = md.PostType,
|
|
|
Description = "",
|
|
|
},
|
|
|
NumOfPieces = md.TotalNum,
|
|
|
Package = new List<Package> { new Package {
|
|
|
//SimpleRate=new CodeAndDescription
|
|
|
//{
|
|
|
// Code=sizeCode,
|
|
|
// Description="",
|
|
|
//},
|
|
|
PackagingType = new TypeModel
|
|
|
{
|
|
|
Code = md.packType,
|
|
|
Description = ""
|
|
|
},
|
|
|
Dimensions = new DimensionsNew
|
|
|
{
|
|
|
UnitOfMeasurement = new UnitOfMeasurement
|
|
|
{
|
|
|
Code = "IN",
|
|
|
Description = "Inches"
|
|
|
},
|
|
|
Length = md.Lenght,
|
|
|
Width = md.Width,
|
|
|
Height = md.Height,
|
|
|
},
|
|
|
PackageWeight = new ShipmentTotalWeight
|
|
|
{
|
|
|
UnitOfMeasurement = new UnitOfMeasurement
|
|
|
{
|
|
|
Code = "LBS",
|
|
|
Description = "Pounds",
|
|
|
},
|
|
|
Weight = md.Weight.ToString(),
|
|
|
}
|
|
|
} },
|
|
|
|
|
|
//RateInformation = new RateInformation
|
|
|
//{
|
|
|
// NegotiatedRatesIndicator = true
|
|
|
//}
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
var bodyStr = JsonConvert.SerializeObject(rating);
|
|
|
//var bodyStr = "{\"FreightRateRequest\":{\"ShipFrom\":{\"Name\":\"Test US Shipper\",\"Address\":{\"AddressLine\":\"123 Lane\",\"City\":\"TIMONIUM\",\"StateProvinceCode\":\"MD\",\"PostalCode\":\"21093\",\"CountryCode\":\"US\",\"ResidentialAddressIndicator\":\"\"},\"AttentionName\":\"Test Shipper\",\"Phone\":{\"Number\":\"4444444444\",\"Extension\":\"4444\"},\"EMailAddress\":\"gcc0htq@ups.com\"},\"ShipperNumber\":\"44434A\",\"ShipTo\":{\"Name\":\"Dilbert's Derbies\",\"Address\":{\"AddressLine\":\"555 Main St\",\"City\":\"LOUISVILLE\",\"StateProvinceCode\":\"KY\",\"PostalCode\":\"40201\",\"CountryCode\":\"US\"},\"AttentionName\":\"Dilbert\",\"Phone\":{\"Number\":\"8459865555\"}},\"PaymentInformation\":{\"Payer\":{\"Name\":\"Test US Shipper\",\"Address\":{\"AddressLine\":\"123 Lane\",\"City\":\"LUTHERVILLE TIMONIUM\",\"StateProvinceCode\":\"MD\",\"PostalCode\":\"21093\",\"CountryCode\":\"US\"},\"ShipperNumber\":\"AT0123\",\"AccountType\":\"1\",\"AttentionName\":\"Test Shipper\",\"Phone\":{\"Number\":\"4444444444\",\"Extension\":\"4444\"},\"EMailAddress\":\"gcc0htq@ups.com\"},\"ShipmentBillingOption\":{\"Code\":\"10\"}},\"Service\":{\"Code\":\"308\"},\"Commodity\":{\"Description\":\"FRS-Freight\",\"Weight\":{\"UnitOfMeasurement\":{\"Code\":\"LBS\"},\"Value\":\"150\"},\"Dimensions\":{\"UnitOfMeasurement\":{\"Code\":\"IN\",\"Description\":\" \"},\"Length\":\"9\",\"Width\":\"5\",\"Height\":\"4\"},\"NumberOfPieces\":\"1\",\"PackagingType\":{\"Code\":\"PLT\"},\"FreightClass\":\"60\"},\"DensityEligibleIndicator\":\"\",\"AlternateRateOptions\":{\"Code\":\"3\"},\"PickupRequest\":{\"PickupDate\":\"20170923\"},\"GFPOptions\":{\"GPFAccesorialRateIndicator\":\"\"},\"TimeInTransitIndicator\":\"\"}}";
|
|
|
request.Content = new StringContent(bodyStr, System.Text.Encoding.UTF8, "application/json");
|
|
|
|
|
|
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
|
|
|
// 发送 POST 请求并返回响应结果
|
|
|
var response = client.SendAsync(request).Result;
|
|
|
var result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
|
|
var resObj = JsonConvert.DeserializeObject<RateResponseModel>(result);
|
|
|
if (resObj.RateResponse == null)
|
|
|
return "ErrorMessage:" + result;
|
|
|
|
|
|
var ratedPackage = JsonConvert.SerializeObject(resObj.RateResponse.RatedShipment.RatedPackage);
|
|
|
|
|
|
if (resObj.RateResponse!=null && resObj.RateResponse.RatedShipment!=null && resObj.RateResponse.RatedShipment.TotalCharges!=null)
|
|
|
{
|
|
|
//var resultStr = "";
|
|
|
if(resObj.RateResponse.RatedShipment.NegotiatedRateCharges!=null && resObj.RateResponse.RatedShipment.NegotiatedRateCharges.TotalCharge != null)
|
|
|
{
|
|
|
return resObj.RateResponse.RatedShipment.NegotiatedRateCharges.TotalCharge.MonetaryValue + "&" + ratedPackage;
|
|
|
//resultStr += "Total Shipment NegotiatedRateCharges Charges " + resObj.RateResponse.RatedShipment.NegotiatedRateCharges.TotalCharge.MonetaryValue + resObj.RateResponse.RatedShipment.NegotiatedRateCharges.TotalCharge.CurrencyCode + "<br/>";
|
|
|
}
|
|
|
//resultStr += "Total Shipment Charges " + resObj.RateResponse.RatedShipment.TotalCharges.MonetaryValue + resObj.RateResponse.RatedShipment.TotalCharges.CurrencyCode;
|
|
|
//return resultStr + "||" + test;
|
|
|
|
|
|
}
|
|
|
return "ErrorMessage:" + ratedPackage;
|
|
|
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
return "ErrorMessage:" + ex.Message;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
} |