using System.Collections.Generic;
using System.Threading.Tasks;
using System.Security.Cryptography;
static ShopeeApiSetting _shopeeApiSetting = null;
static IShopeePartnerApiClient _apiClient = null;
public static void Main()
_shopeeApiSetting = LoadConfig();
_apiClient = RestService.For<IShopeePartnerApiClient>(_shopeeApiSetting.ApiUrl);
var orderNo = new List<string>() {};
public async Task<ShopeeApiOrderStatusResponse> GetOrderInfoAsync(string orderCode)
ShopeeApiOrderStatusResponse res = null;
foreach (var shopId in _shopeeApiSetting.ShopIdList)
var requestModel = CreateGetOrderInfoRequestModel(orderCode, shopId);
res = await _apiClient.GetOrderInfoAsync(GenerateRequestToken(requestModel), requestModel);
if ((res.Errors == null || (res.Errors?.Length ?? 0) == 0) && (res.Orders != null || (res.Orders?.Length ?? 0) > 0)) { break; }
private ShopeeApiOrderStatusRequest CreateGetOrderInfoRequestModel(string orderCode, long shopeId)
return new ShopeeApiOrderStatusRequest
PartnerId = _shopeeApiSetting.PartnerId,
Timestamp = DateTime.UtcNow.ToUnixTimestamp(),
OrderIdList = new string[] { orderCode }
public static ShopeeApiSetting LoadConfig()
return new ShopeeApiSetting() {
ApiUrl = "https://partner.shopeemobile.com",
ShopIdList = new long[] { 72135979, 178191615 },
PartnerKey = "e38f6c8a9f7dad0ffcd4d36f26b7602c20a2be3fc6f55f27ed9ec1cd1c09104f",
private string GenerateRequestToken(object requestObject)
MethodInfo method = typeof(IShopeePartnerApiClient).GetMethod("GetOrderInfoAsync");
object[] attributes = method.GetCustomAttributes(true);
foreach (var attr in attributes)
if (attr is HttpMethodAttribute attribute)
requestPath = attribute.Path;
requestUrl = _shopeeApiSetting.ApiUrl + requestPath;
var stringToSign = requestUrl + "|" + Newtonsoft.Json.JsonConvert.SerializeObject(requestObject);
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(_shopeeApiSetting.PartnerKey);
HMACSHA256 hmacsha1 = new HMACSHA256(keyByte);
byte[] messageBytes = encoding.GetBytes(stringToSign);
byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
for (int i = 0; i < hashmessage.Length; i++)
signed += hashmessage[i].ToString("X2");
public class ShopeeApiSetting
public bool IsDev { get; set; }
public string ApiUrl { get; set; }
public long PartnerId { get; set; }
public long[] ShopIdList { get; set; }
public string PartnerKey { get; set; }
public interface IShopeePartnerApiClient
[Post("/api/v1/orders/detail")]
Task<ShopeeApiOrderStatusResponse> GetOrderInfoAsync([Header("Authorization")] string accessToken, [Body]ShopeeApiOrderStatusRequest order);
[Post("/api/v1/orders/detail")]
Task<HttpResponseMessage> TestGetOrderInfoAsync([Header("Authorization")] string accessToken, [Body] ShopeeApiOrderStatusRequest order);
public class ShopeeApiOrderStatusRequest
[AliasAs("ordersn_list"), JsonProperty(PropertyName = "ordersn_list")]
public string[] OrderIdList { get; set; }
[AliasAs("partner_id"), JsonProperty(PropertyName = "partner_id")]
public long PartnerId { get; set; }
[AliasAs("shopid"), JsonProperty(PropertyName = "shopid")]
public long ShopId { get; set; }
[AliasAs("timestamp"), JsonProperty(PropertyName = "timestamp")]
public long Timestamp { get; set; }
public class ShopeeApiOrderStatusResponse
[AliasAs("shop_id"), JsonProperty(PropertyName = "shop_id")]
public long ShopId { get; set; }
[AliasAs("errors"), JsonProperty(PropertyName = "errors")]
public string[] Errors { get; set; }
[AliasAs("orders"), JsonProperty(PropertyName = "orders")]
public ShopeeApiOrderInfo[] Orders { get; set; }
[AliasAs("request_id"), JsonProperty(PropertyName = "request_id")]
public string RequestId { get; set; }
public class ShopeeApiOrderInfo
[AliasAs("order_status"), JsonProperty(PropertyName = "order_status")]
public string OrderStatusAsText { get; set; }
[AliasAs("ordersn"), JsonProperty(PropertyName = "ordersn")]
public string OrderId { get; set; }
[AliasAs("buyer_username"), JsonProperty(PropertyName = "buyer_username")]
public string BuyerUsername { get; set; }
[AliasAs("items"), JsonProperty(PropertyName = "items")]
public ShopeeApiItemInfo[] Items { get; set; }
public class ShopeeApiItemInfo
[AliasAs("item_id"), JsonProperty(PropertyName = "item_id")]
public long ItemId { get; set; }
[AliasAs("variation_quantity_purchased"), JsonProperty(PropertyName = "variation_quantity_purchased")]
public long Amount { get; set; }
[AliasAs("variation_id"), JsonProperty(PropertyName = "variation_id")]
public string VariationId { get; set; }
public string FullItemSkuId
return (VariationId != "0") ? String.Join("_", (new List<string>() { ItemId.ToString(), VariationId }).Where(x => !String.IsNullOrEmpty(x))) : ItemId.ToString();
public static class UnixTimestampExtension
public static long ToUnixTimestamp(this DateTime dt)
return (long)(dt.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
public static long ToUnixTimestamp(this DateTimeOffset dt)
return (long)(dt.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;