Amazon订单接口是Amazon MWS 开发接口中的一大块,我们可以通过接口调用来获得订单数据。
在调用接口之前,首先我们要获得相关店铺商家的店铺密钥等信息。如下:
在此我将所有信息定义在一个类中,采用序列化的方式,方便存取值。
1 /// <summary> 2 /// 账户信息 3 /// </summary> 4 [Serializable] 5 public class AccountConfig : BaseConfig<AccountConfig> 6 { 7 /// <summary> 8 /// 应用程序名称 9 /// </summary> 10 public string AppName { get; set; } 11 12 /// <summary> 13 /// 应用程序版本 14 /// </summary> 15 public string AppVersion { get; set; } 16 17 /// <summary> 18 /// 账户KeyId 19 /// </summary> 20 public string AccessKeyId { get; set; } 21 22 /// <summary> 23 /// 密钥Id 24 /// </summary> 25 public string SecretAccessKey { get; set; } 26 27 /// <summary> 28 /// 商城Id 29 /// </summary> 30 public string MerchantId { get; set; } 31 32 /// <summary> 33 /// 商城唯一标识Id 34 /// </summary> 35 public string MarketplaceId { get; set; } 36 37 /// <summary> 38 /// 服务端URL 39 /// </summary> 40 public string ServiceUrl { get; set; } 41 }
然后我们回到文档中,我们会发现,请求参数中,有两个参数是必填的:CreatedAfter、MarketplaceId。调用接口的方法很简单,无非是构造完整的对象,其实主要还是保持必填参数的完整性,这次我们拿ListOrder 接口来举例:
1 /// <summary> 2 /// 获得账户信息 3 /// </summary> 4 protected AccountConfig Account 5 { 6 get 7 { 8 return AccountConfig.Instance; 9 } 10 } 11 12 private MarketplaceWebServiceOrdersConfig GetConfig() 13 { 14 var config = new MarketplaceWebServiceOrdersConfig(); 15 config.ServiceURL = Account.ServiceUrl; 16 return config; 17 } 18 19 private MarketplaceWebServiceOrdersClient GetClient() 20 { 21 var config = this.GetConfig(); 22 var client = new MarketplaceWebServiceOrdersClient(Account.AppName, 23 Account.AppVersion, Account.AccessKeyId, Account.SecretAccessKey, config); 24 return client; 25 } 26 27 #region 获得订单列表(OrderList) 28 29 30 private List<Order> GetNextOrderList(ref string nextToken) 31 { 32 var client = this.GetClient(); 33 var request = new ListOrdersByNextTokenRequest(); 34 request.SellerId = Account.MerchantId; 35 request.NextToken = nextToken; 36 37 var response = client.ListOrdersByNextToken(request); 38 var result = response.ListOrdersByNextTokenResult; 39 nextToken = result.NextToken; 40 List<Order> orderList = result.Orders.Order; 41 42 return orderList; 43 } 44 45 /// <summary> 46 /// 获得订单列表(指定Request模式) 47 /// </summary> 48 /// <param name="request">ListOrdersRequest</param> 49 /// <returns></returns> 50 public List<Order> GetOrderList(DateTime createdAfter, DateTime createdBefore) 51 { 52 var request = new ListOrdersRequest(); 53 request.CreatedAfter = AmazonHelper.ConvertToISO8601(createdAfter); 54 request.CreatedBefore = AmazonHelper.ConvertToISO8601(createdBefore); 55 request.MarketplaceId = new MarketplaceIdList(); 56 request.MarketplaceId.Id = new List<string> { Account.MarketplaceId }; 57 request.SellerId = Account.MerchantId; 58 59 var client = GetClient(); 60 var response = client.ListOrders(request); 61 var result = response.ListOrdersResult; 62 var orderList = result.Orders.Order; 63 string nextToken = result.NextToken; 64 65 while (!string.IsNullOrEmpty(nextToken)) 66 { 67 List<Order> tempList = GetNextOrderList(ref nextToken); 68 orderList = orderList.Concat(tempList).ToList(); 69 } 70 71 return orderList; 72 } 73 #endregion
切记,在调用中不要忽视了在请求参数中给 SellerId 赋值。