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.

398 lines
16 KiB
C#

2 months ago
using NetLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.IO;
namespace NetLibrary.OnlineTrade
{
public class WishApi
{
JavaScriptSerializer JsonConvert = null;
public WishApi()
{
//验证服务器证书回调自动验证
ServicePointManager.ServerCertificateValidationCallback += ValidateServerCertificate;
JsonConvert = new JavaScriptSerializer();
}
private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
public List<OrderModel> GetOrders(string Token, out string ErrorMessage)
{
ErrorMessage = "";
string URL = "https://merchant.wish.com/api/v1/order/get-fulfill?start=0&limit=50&key=" + Token;
string ErrMessage="";
string XmlContent = CustomIO.HttpRequest2(URL, "GET", "text/html", null, System.Net.HttpVersion.Version10,null, out ErrMessage);
if (string.IsNullOrEmpty(ErrMessage) == false)
{
ErrorMessage = ErrMessage;
return null;
}
WishOrderResult Model = JsonConvert.Deserialize<WishOrderResult>(XmlContent);
if (string.IsNullOrEmpty(Model.message) == false)
{
ErrorMessage = Model.message;
return null;
}
List<OrderModel> ListModel = new List<OrderModel>();
foreach (var item in Model.data)
{
OrderModel model = item.Order.ToOrderModel();
ListModel.Add(model);
}
if (Model.paging!=null&&string.IsNullOrEmpty(Model.paging.next) == false)
{
while (true)
{
XmlContent = CustomIO.HttpRequest(Model.paging.next, "POST", out ErrMessage);
if (string.IsNullOrEmpty(ErrMessage) == false)
{
ErrorMessage = ErrMessage;
return null;
}
Model = JsonConvert.Deserialize<WishOrderResult>(XmlContent);
if (string.IsNullOrEmpty(Model.message) == false)
{
ErrorMessage = Model.message;
return null;
}
foreach (var item in Model.data)
{
OrderModel model = item.Order.ToOrderModel();
ListModel.Add(model);
}
if (Model.data == null || Model.paging == null || Model.data.Count< 50) break;
}
}
return ListModel;
}
public List<OrderModel> GetOrdersNew(string Token, out string ErrorMessage)
{
ErrorMessage = "";
string URL = "https://merchant.wish.com/api/v2/order/multi-get?access_token=" + Token + "&start=0&count=50";
string ErrMessage = "";
string XmlContent = CustomIO.HttpRequest2(URL, "GET", "text/html", null, System.Net.HttpVersion.Version10, null, out ErrMessage);
if (string.IsNullOrEmpty(ErrMessage) == false)
{
ErrorMessage = ErrMessage;
return null;
}
WishOrderResult Model = JsonConvert.Deserialize<WishOrderResult>(XmlContent);
if (string.IsNullOrEmpty(Model.message) == false)
{
ErrorMessage = Model.message;
return null;
}
List<OrderModel> ListModel = new List<OrderModel>();
foreach (var item in Model.data)
{
OrderModel model = item.Order.ToOrderModel();
ListModel.Add(model);
}
if (Model.paging != null && string.IsNullOrEmpty(Model.paging.next) == false)
{
while (true)
{
XmlContent = CustomIO.HttpRequest(Model.paging.next, "POST", out ErrMessage);
if (string.IsNullOrEmpty(ErrMessage) == false)
{
ErrorMessage = ErrMessage;
return null;
}
Model = JsonConvert.Deserialize<WishOrderResult>(XmlContent);
if (string.IsNullOrEmpty(Model.message) == false)
{
ErrorMessage = Model.message;
return null;
}
foreach (var item in Model.data)
{
OrderModel model = item.Order.ToOrderModel();
ListModel.Add(model);
}
if (Model.data == null || Model.paging == null || Model.data.Count < 50) break;
}
}
return ListModel;
}
public bool UpTrackTOWish(string key, string orderno, string ysfs, string trackno, string ship_note)
{
try
{
//string key = "";
// var checkkey = cl.Where(o => o.group == group);
// if (checkkey.Count() == 0) return false;
// key = checkkey.First().key;
string result = GetBack(key, orderno, ysfs, trackno, ship_note);
if (result.Contains("success"))
return true;
else return false;
}
catch
{
return false;
}
}
private string GetBack(string key, string orderno, string ysfs, string trackno, string ship_note)
{
string URL = "";
if (ship_note == "")
{
URL = "https://merchant.wish.com/api/v1/order/fulfill-one?tracking_provider=" + ysfs + "&tracking_number=" + trackno + "&id=" + orderno + "&key=" + key;
}
else URL = "https://merchant.wish.com/api/v1/order/fulfill-one?tracking_provider=" + ysfs + "&tracking_number=" + trackno + "&id=" + orderno + "&key=" + key + "&ship_note=" + ship_note;
//string URL = "https://merchant.wish.com/api/v1/order/fulfill-one?tracking_provider=" + ysfs + "&tracking_number=" + trackno + "&id=" + orderno + "&key=" + key;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "text/html";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; qihu theworld)";
request.Headers.Add("Accept-Language: zh-CN");
request.Headers["Pragma"] = "no-cache"; //禁用缓存
request.Headers["Cache-Control"] = "no-cache"; //禁用缓存
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string josn = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
response.Close();
return josn;
}
public bool ModifyTrackTOWish(string key, string orderno, string ysfs, string trackno, string ship_note)
{
try
{
// string key = "";
//var checkkey = cl.Where(o => o.group == group);
// if (checkkey.Count() == 0) return false;
// key = checkkey.First().key;
if (ysfs == "DeYou")
ysfs = "DHL";
if (ysfs == "China Post Air Mail")
ysfs = "ChinaAirPost";
if (ysfs == "EUB")
ysfs = "EPacket";
if (ysfs == "SF Express")
ysfs = "SFExpress";
string result = GetModifyBack(key, orderno, ysfs, trackno, ship_note);
if (result.Contains("success"))
return true;
else return false;
}
catch(Exception ex)
{
return false;
}
}
private string GetModifyBack(string key, string orderno, string ysfs, string trackno, string ship_note)
{
string URL = "";
if(ship_note=="")
{
URL = "https://merchant.wish.com/api/v1/order/fulfill-one?tracking_provider=" + ysfs + "&tracking_number=" + trackno + "&id=" + orderno + "&key=" + key;
}
else URL = "https://merchant.wish.com/api/v1/order/fulfill-one?tracking_provider=" + ysfs + "&tracking_number=" + trackno + "&id=" + orderno + "&key=" + key + "&ship_note=" + ship_note;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "text/html";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; qihu theworld)";
request.Headers.Add("Accept-Language: zh-CN");
request.Headers["Pragma"] = "no-cache"; //禁用缓存
request.Headers["Cache-Control"] = "no-cache"; //禁用缓存
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string josn = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
response.Close();
return josn;
}
private string GetModify(string key, string orderno, string ysfs, string trackno, string ship_note)
{
string URL = "";
if (ship_note == "")
{
URL = "https://merchant.wish.com/api/v1/order/modify-tracking?tracking_provider=" + ysfs + "&tracking_number=" + trackno + "&id=" + orderno + "&key=" + key;
}
else URL = "https://merchant.wish.com/api/v1/order/modify-tracking?tracking_provider=" + ysfs + "&tracking_number=" + trackno + "&id=" + orderno + "&key=" + key + "&ship_note=" + ship_note;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "text/html";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; qihu theworld)";
request.Headers.Add("Accept-Language: zh-CN");
request.Headers["Pragma"] = "no-cache"; //禁用缓存
request.Headers["Cache-Control"] = "no-cache"; //禁用缓存
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string josn = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
response.Close();
return josn;
}
public string GetOneOrder(string key, string orderno)
{
string URL = "https://merchant.wish.com/api/v1/order?" + "id=" + orderno + "&key=" + key;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "text/html";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; qihu theworld)";
request.Headers.Add("Accept-Language: zh-CN");
request.Headers["Pragma"] = "no-cache"; //禁用缓存
request.Headers["Cache-Control"] = "no-cache"; //禁用缓存
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string josn = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
response.Close();
return josn;
}
}
public class WishOrderResult
{
public string message { get; set; }
public int code { get; set; }
public List<WishOrder> data { get; set; }
public WishPaging paging { get; set; }
}
public class WishOrder
{
public WishOrder_Order Order { get; set; }
}
public class WishOrder_Order
{
public string sku { get; set; }
public string buyer_id { get; set; }
public string last_updated { get; set; }
public string product_id { get; set; }
public string order_time { get; set; }
public string quantity { get; set; }
public string color { get; set; }
public string price { get; set; }
public string shipping_cost { get; set; }
public string shipping { get; set; }
public WishOrder_ShippingDetail ShippingDetail { get; set; }
public string order_id { get; set; }
public string state { get; set; }
public string cost { get; set; }
public string variant_id { get; set; }
public string order_total { get; set; }
public string days_to_fulfill { get; set; }
public string product_image_url { get; set; }
public string product_name { get; set; }
public string transaction_id { get; set; }
public string size { get; set; }
public DateTime? GetDateTime(string dt)
{
if (string.IsNullOrEmpty(dt) == true) return null;
dt = dt.Replace("T", " ");
dt = dt.Remove(dt.Length - 1);
return Convert.ToDateTime(dt);
}
#region 转换成统一的订单对象
public OrderModel ToOrderModel()
{
OrderModel model = new OrderModel();
model.OrderCode = this.order_id;
model.TotalPrice =Convert.ToDecimal(this.order_total);
model.MoneyCode = null;
model.OrderDate = this.GetDateTime(this.order_time);
model.BuyerID = this.buyer_id;
model.BuyerName = this.ShippingDetail.name;
model.BuyerCountry = this.ShippingDetail.country;
model.BuyerAddr = this.ShippingDetail.street_address1;
if (this.ShippingDetail.street_address2 != null && this.ShippingDetail.street_address2 != "")
model.BuyerAddr +=" "+this.ShippingDetail.street_address2;
model.BuyerPhone = this.ShippingDetail.phone_number;
model.BuyerMobile = "";
model.BuyerMail = "";
model.BuyerFax = "";
model.BuyerZip = this.ShippingDetail.zipcode;
model.BuyerProvince = this.ShippingDetail.state;
model.BuyerCity = this.ShippingDetail.city;
model.BuyerArea = "";
model.OrderRemark = ""; //memo
model.LeaveWord = "";
model.PayDate = this.GetDateTime(this.last_updated);
model.OutOrderDate = null;
model.ListModel = new List<OrderDetailModel>();
OrderDetailModel model2 = new OrderDetailModel();
model2.GoodsNum =Convert.ToInt32(this.quantity);
model2.GoodsPrice = Convert.ToDecimal(this.price);
model2.MoneyCode = null;
model2.GoodsName = this.product_name;
model2.GoodsSKU = this.sku;
model2.PostInfo = ""; //快递信息
model.ListModel.Add(model2);
return model;
}
#endregion
}
public class WishOrder_ShippingDetail
{
public string phone_number { get; set; }
public string city { get; set; }
public string state { get; set; }
public string name { get; set; }
public string country { get; set; }
public string zipcode { get; set; }
public string street_address1 { get; set; }
public string street_address2 { get; set; }
}
public class WishPaging
{
public string next { get; set; }
}
}