|
@@ -0,0 +1,179 @@
|
|
|
+using iTextSharp.text.pdf;
|
|
|
+using RestSharp;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Net.Http;
|
|
|
+using System.Net.Http.Headers;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using XYY.Model.Standard.Channel;
|
|
|
+using XYY.Model.Standard.Order;
|
|
|
+using XYY.Service.Standard.ChannelApi.Base;
|
|
|
+
|
|
|
+namespace XYY.Service.Standard.ChannelApi.DeDhl
|
|
|
+{
|
|
|
+ public class DeDhlApi : IBaseApi
|
|
|
+ {
|
|
|
+ public string ServiceName => "DeDHL";
|
|
|
+
|
|
|
+ public Task<List<BagDeclareErrosResult>> BagDeclare(BagDeclareRequest bagDeclareRequest)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Task CannelOrders(List<string> systemNO)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Task CannelOrdersByModel(logistics_ChannelCancel logistics_ChannelCancel)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Task<List<string>> Confims(List<OrderConfimRequest> order, Logistics_Channel channel)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ public OrderRequest GetOrderRequest(Order_Order order, List<Order_OrderGoods> goods, Logistics_Channel channel, ExtendArg extendArg)
|
|
|
+ {
|
|
|
+ string billNumber = channel.ApiSecret.Split(":")[1];
|
|
|
+
|
|
|
+ OrderRequest orderRequest = new OrderRequest();
|
|
|
+ orderRequest.profile = "STANDARD_GRUPPENPROFIL";
|
|
|
+ orderRequest.shipments = new List<ShipmentsItem> { new ShipmentsItem {
|
|
|
+
|
|
|
+ billingNumber = billNumber,
|
|
|
+ product=channel.ServiceChannelCode,
|
|
|
+ refNo=order.SystemNo.Replace("XYY","").Replace("YQ",""),
|
|
|
+ shipper = new Shipper{
|
|
|
+ city=channel.SenderCity,
|
|
|
+ postalCode=channel.SenderZipCode,
|
|
|
+ phone=channel.SenderPhone,
|
|
|
+ country="DEU",
|
|
|
+ addressHouse=channel.SenderCounty,
|
|
|
+ addressStreet = channel.SenderStreet,
|
|
|
+ additionalAddressInformation1 =null,
|
|
|
+ email = channel.SenderEmail,
|
|
|
+ name1 = channel.SenderName
|
|
|
+ },consignee = new Consignee
|
|
|
+ {
|
|
|
+ name1 =order.ReceiverName,
|
|
|
+ addressStreet=order.ReceiverStreet,
|
|
|
+ city=order.ReceiverCity,
|
|
|
+ email=order.ReceiverEmail,
|
|
|
+ postalCode=order.ReceiverZipCode,
|
|
|
+ phone=order.ReceiverPhone,
|
|
|
+ additionalAddressInformation1=null,
|
|
|
+ country="DEU"
|
|
|
+ },
|
|
|
+ details = new Details
|
|
|
+ {
|
|
|
+ weight = new Weight
|
|
|
+ {
|
|
|
+ uom="g",
|
|
|
+ value = order.CustomerWeight
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ return orderRequest;
|
|
|
+
|
|
|
+ }
|
|
|
+ public string Base64Encode(string plainText)
|
|
|
+ {
|
|
|
+ var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
|
|
|
+ return System.Convert.ToBase64String(plainTextBytes);
|
|
|
+ }
|
|
|
+ public async Task<UploadResult> DataUpload(Order_Order order, List<Order_OrderGoods> goods, Logistics_Channel channel, ExtendArg arg)
|
|
|
+ {
|
|
|
+ //apikey = gkpuser:gkppass
|
|
|
+ string baseUrl = channel.ApiHost;
|
|
|
+ var client = new HttpClient();
|
|
|
+ string apiKey = channel.ApiSecret.Split(':')[0];
|
|
|
+ var orderRequest = GetOrderRequest(order, goods, channel, arg);
|
|
|
+ string base64Auth = Base64Encode(channel.ApiKey);
|
|
|
+ string json = Newtonsoft.Json.JsonConvert.SerializeObject(orderRequest);
|
|
|
+ RestClient restClient = new RestClient();
|
|
|
+ RestRequest restRequest = new RestRequest($"{baseUrl}orders");
|
|
|
+ restRequest.Method = Method.POST;
|
|
|
+ restRequest.AddHeader("Accept-Language", "en-US");
|
|
|
+ restRequest.AddHeader("Authorization", $"Basic {base64Auth}");
|
|
|
+ restRequest.AddHeader("dhl-api-key", apiKey);
|
|
|
+ restRequest.AddJsonBody(json);
|
|
|
+ var response = restClient.Execute(restRequest);
|
|
|
+ if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
|
|
+ {
|
|
|
+ var resultData = Newtonsoft.Json.JsonConvert.DeserializeObject<ResultData>(response.Content);
|
|
|
+ var resultItem = resultData.items[0];
|
|
|
+ return new UploadResult
|
|
|
+ {
|
|
|
+ IsSuccess = true,
|
|
|
+ DownloadUrl = resultItem.label.b64,
|
|
|
+ TrackingNumber = resultItem.shipmentNo
|
|
|
+ };
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var resultData = Newtonsoft.Json.JsonConvert.DeserializeObject<ResultData>(response.Content);
|
|
|
+ if (resultData == null)
|
|
|
+ throw new Exception("下家服务器访问异常");
|
|
|
+ throw new Exception(resultData.status.detail);
|
|
|
+ }
|
|
|
+ //var request = new HttpRequestMessage
|
|
|
+ //{
|
|
|
+ // Method = HttpMethod.Post,
|
|
|
+ // RequestUri = new Uri($"{baseUrl}/orders"),
|
|
|
+ // Headers =
|
|
|
+ // {
|
|
|
+ // { "Accept-Language", "en-US" },
|
|
|
+ // { "Authorization", $"Basic {channel.ApiKey}"},
|
|
|
+ // {"dhl-api-key",apiKey}
|
|
|
+ // },
|
|
|
+ // Content = new StringContent(json)
|
|
|
+ // {
|
|
|
+ // Headers =
|
|
|
+ // {
|
|
|
+ // ContentType = new MediaTypeHeaderValue("application/json")
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //};
|
|
|
+ //try
|
|
|
+ //{
|
|
|
+ // using (var response = await client.SendAsync(request))
|
|
|
+ // {
|
|
|
+ // response.EnsureSuccessStatusCode();
|
|
|
+ // var body = await response.Content.ReadAsStringAsync();
|
|
|
+ // Console.WriteLine(body);
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ //catch (HttpRequestException ex)
|
|
|
+ //{
|
|
|
+ // ex.
|
|
|
+ //}
|
|
|
+ //catch (Exception ex)
|
|
|
+ //{
|
|
|
+
|
|
|
+ //}
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<byte[]> DrawPdfLabel(UploadResult result, Logistics_Channel channel, Order_Order order, List<Order_OrderGoods> orderGoods)
|
|
|
+ {
|
|
|
+ string pdfURL = result.DownloadUrl;
|
|
|
+ if (string.IsNullOrEmpty(pdfURL))
|
|
|
+ {
|
|
|
+ throw new Exception("PDF 地址不应为空");
|
|
|
+ }
|
|
|
+ byte[] data = Convert.FromBase64String(pdfURL);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Task<string> GetTrackingNumber(string SystemNo, string TrackingNumber, string ServiceOrderNo, Logistics_Channel channel)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|