using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Xml.Linq; namespace NetLibrary.OnlineTrade { public class LazadaApi { #region 读取订单 public List GetOrders(string shopname, string Apikey, string headurl, out string ErrorMessage) { List ListModel = new List(); string timenow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(" ", "T").Replace(":", "%3A") + "%2B08%3A00"; ; string timeBegin = DateTime.Now.AddDays(-10).ToString("yyyy-MM-dd hh:mm:ss").Replace("/", "-").Replace(" ", "T").Replace(":", "%3A") + "%2B08%3A00"; string temp = "Action=GetOrders&CreatedAfter=" + timeBegin + "&Format=XML&Status=pending&Timestamp=" + timenow + "&UserID=heegrandlazada%40outlook.com&Version=1.0"; string str = SHA256Encrypt(Apikey, temp); string URL = headurl + "/?Action=GetOrders&CreatedAfter=" + timeBegin + "&Format=XML&Status=pending&Timestamp=" + timenow + "&UserID=heegrandlazada%40outlook.com&Version=1.0&Signature=" + str; string XmlContent = CustomIO.HttpRequest2(URL, "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; string timenow2 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("/", "-").Replace(" ", "T").Replace(":", "%3A") + "%2B08%3A00"; ; string temp2 = "Action=GetMultipleOrderItems&Format=XML&OrderIdList=%5B" + q.Element("OrderId").Value + "%5D&Timestamp=" + timenow2 + "&UserID=heegrandlazada%40outlook.com&Version=1.0"; string str2 = SHA256Encrypt(Apikey, temp2); string URL2 = headurl + "/?Action=GetMultipleOrderItems&Format=XML&OrderIdList=%5B" + q.Element("OrderId").Value + "%5D&Timestamp=" + timenow2 + "&UserID=heegrandlazada%40outlook.com&Version=1.0&Signature=" + str2; string result2 = CustomIO.HttpRequest2(URL2, "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 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; } } }