using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Xml.Linq; using System.Web; namespace NetLibrary.OnlineTrade { public class LazadaApi { /// /// 接口版本 /// public readonly string VERSION = "1.0"; #region 读取订单 public List GetOrders(string country, string UserID, string apiKey, DateTime? SDate, out string ErrorMessage) { string Url = ""; List ListModel = new List(); #region 设置市场所在地借口地址 switch (country) { case "印尼": Url = "https://api.sellercenter.lazada.co.id"; break; case "马来": Url = "https://api.sellercenter.lazada.com.my"; break; case "泰国": Url = "https://api.sellercenter.lazada.co.th"; break; case "菲律宾": Url = "https://api.sellercenter.lazada.com.ph"; break; case "新加坡": Url = "https://api.sellercenter.lazada.sg"; break; case "越南": Url = "https://api.sellercenter.lazada.vn"; break; } #endregion var timestamp = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss+08:00"); var stime = SDate.Value.ToString("yyyy-MM-ddTHH:mm:ss+08:00"); var param = new Dictionary(); param.Add("Action", "GetOrders"); param.Add("CreatedAfter", HttpUtility.UrlEncode(stime, Encoding.GetEncoding("UTF-8")).ToUpper()); //param.Add("Status", "pending"); param.Add("UserID", HttpUtility.UrlEncode(UserID, Encoding.GetEncoding("UTF-8"))); param.Add("Timestamp", HttpUtility.UrlEncode(timestamp, Encoding.GetEncoding("UTF-8")).ToUpper()); param.Add("Format", "XML"); param.Add("Version", VERSION); var sign = CreateSignature(param, apiKey); param.Add("Signature", HttpUtility.UrlEncode(sign, Encoding.GetEncoding("UTF-8"))); string surl = Url + "?"; if (param != null) { foreach (var item in param) { surl += item.Key + "=" + item.Value + "&"; } } surl = surl.TrimEnd('&'); string XmlContent = CustomIO.HttpRequest2(surl, "GET", "text/html", null, System.Net.HttpVersion.Version10, null, out ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) { return null; } XElement xmlPage = XElement.Parse(XmlContent); try { foreach (var q in xmlPage.Element("Body").Element("Orders").Elements("Order")) { string OrderNumber = q.Element("OrderNumber").Value; if (q.Element("Statuses").Element("Status").Value != "pending") continue; var param2 = new Dictionary(); param2.Add("Action", "GetOrderItems"); param2.Add("OrderId", HttpUtility.UrlEncode(q.Element("OrderId").Value, Encoding.GetEncoding("UTF-8")).ToUpper()); param2.Add("UserID", HttpUtility.UrlEncode(UserID, Encoding.GetEncoding("UTF-8"))); param2.Add("Timestamp", HttpUtility.UrlEncode(timestamp, Encoding.GetEncoding("UTF-8")).ToUpper()); param2.Add("Format", "XML"); param2.Add("Version", VERSION); var sign2 = CreateSignature(param2, apiKey); param2.Add("Signature", HttpUtility.UrlEncode(sign, Encoding.GetEncoding("UTF-8"))); string surl2 = Url + "?"; if (param != null) { foreach (var item in param) { surl2 += item.Key + "=" + item.Value + "&"; } } surl2 = surl2.TrimEnd('&'); string result2 = CustomIO.HttpRequest2(surl2, "GET", "text/html", null, System.Net.HttpVersion.Version10, null, out ErrorMessage); if (string.IsNullOrEmpty(ErrorMessage) == false) { return null; } XElement DetailXml = XElement.Parse(result2); OrderModel om = new OrderModel(); om.OrderCode = OrderNumber; om.PlatOrderCode = q.Element("OrderId").Value; string customname = ""; if (q.Element("CustomerFirstName") != null) { customname = q.Element("CustomerFirstName").Value; } if (q.Element("CustomerLastName") != null) { if (q.Element("CustomerLastName").Value != "") customname += " " + q.Element("CustomerLastName").Value; } if (customname == "") { ErrorMessage += OrderNumber + " 收件人姓名为空
"; continue; } om.BuyerName = customname; //string price = q.Element("Price").Value; //om.TotalPrice = Convert.ToDecimal(price); if (q.Element("AddressShipping").Element("Phone") != null) { om.BuyerPhone = q.Element("AddressShipping").Element("Phone").Value; } if (q.Element("AddressShipping").Element("Phone2") != null) { om.BuyerMobile = q.Element("AddressShipping").Element("Phone2").Value; } string address = ""; if (q.Element("AddressShipping").Element("Address1") != null) { address = q.Element("AddressShipping").Element("Address1").Value + ","; } if (q.Element("AddressShipping").Element("Address2") != null) { if (q.Element("AddressShipping").Element("Address2").Value != "") address += "" + q.Element("AddressShipping").Element("Address2").Value + ","; } if (q.Element("AddressShipping").Element("Address3") != null) { if (q.Element("AddressShipping").Element("Address3").Value != "") address += "" + q.Element("AddressShipping").Element("Address3").Value + ","; } if (q.Element("AddressShipping").Element("Address4") != null) { if (q.Element("AddressShipping").Element("Address4").Value != "") address += "" + q.Element("AddressShipping").Element("Address4").Value + ","; } if (q.Element("AddressShipping").Element("Address5") != null) { if (q.Element("AddressShipping").Element("Address5").Value != "") address += "" + q.Element("AddressShipping").Element("Address5").Value + ","; } om.BuyerAddr = address; string City = " "; if (q.Element("AddressShipping").Element("City") != null) { if (q.Element("AddressShipping").Element("City").Value != "") City = q.Element("AddressShipping").Element("City").Value; } om.BuyerCity = City; string Region = " "; if (q.Element("AddressShipping").Element("Region") != null) { if (q.Element("AddressShipping").Element("Region").Value != "") Region = q.Element("AddressShipping").Element("Region").Value; } om.BuyerProvince = Region; string PostCode = ""; if (q.Element("AddressShipping").Element("PostCode") != null) { PostCode = q.Element("AddressShipping").Element("PostCode").Value; } om.BuyerZip = PostCode; string Country = " "; if (q.Element("AddressShipping").Element("Country") != null) { Country = q.Element("AddressShipping").Element("Country").Value; } om.BuyerCountry = Country; om.OrderDate = DateTime.Now; om.PayDate = Convert.ToDateTime(q.Element("CreatedAt").Value); string bz = ""; Decimal PaidPrice = 0; List odmlist = new List(); foreach (var item in DetailXml.Element("Body").Element("Orders").Element("Order").Element("OrderItems").Elements("OrderItem")) { OrderDetailModel odm = new OrderDetailModel(); string sku = item.Element("Sku").Value; string[] goodlist = sku.Split('-'); odm.GoodsSKU = sku; odm.MoneyCode = item.Element("Currency").Value; bz = item.Element("Currency").Value; odm.GoodsPrice = Convert.ToDecimal(item.Element("ItemPrice").Value); //PaidPrice += Convert.ToDecimal(item.Element("PaidPrice").Value); PaidPrice += Convert.ToDecimal(item.Element("ItemPrice").Value); odm.GoodsNum = 1; odm.GoodsName = goodlist[0]; //odm.PostInfo = item.Element("OrderItemId").Value; odmlist.Add(odm); //om.ListModel.Add(odm); } om.ListModel = odmlist; om.TotalPrice = PaidPrice; om.MoneyCode = bz; ListModel.Add(om); } } catch { ErrorMessage += "读取失败"; } return ListModel; } #endregion /// /// 生成签名 /// public string CreateSignature(Dictionary dict, string apiKey) { var dictSort = dict.OrderBy(u => u.Key).Select(u => string.Format("{0}={1}", u.Key, u.Value)); var str = string.Join("&", dictSort); return HmacSHA256(str, apiKey).ToLower(); } /// /// HMACSHA256 /// private string HmacSHA256(string data, string key) { var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(key)); byte[] _byte = hmacsha256.ComputeHash(System.Text.Encoding.ASCII.GetBytes(data)); hmacsha256.Clear(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (var item in _byte) { sb.Append(item.ToString("X").PadLeft(2, '0')); } return sb.ToString(); } public string SHA256Encrypt(string password, string name) { string str = ""; var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(password)); hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(name)); foreach (byte test in hmacsha256.Hash) { str += test.ToString("x2"); } return str; } } }