亚马逊API之订单下载

时间:2024-04-17 11:21:30

介绍:本文介绍通过API下载亚马逊平台的订单。

第一步:下载亚马逊平台的客户端库,网址:https://developer.amazonservices.com.cn/doc/orders/orders/v20110101/cSharp.html,解压后,重新生成DLL

第二步:在自己的项目中引用DLL,撰写调用代码。订单下载涉及两个接口:ListOrders和ListOrderItems

下面直接上代码:

参数:

 //卖家编号

        public string Merchant = "XXX";
        //商城编号
        public string MarketplaceID = "XXX";
        //AWS访问密钥
        public string AWSAccessKeyID = "XXX";
        //密钥
        public string Key = "XXX";
        public string applicationName = "XXX";
        public string applicationVersion = "1.0";

 调用ListOrders和ListOrderItems,下载订单信息

/// <summary>
        
/// 订单下载
        
/// </summary>
        private void Down()
        {
            try
            {
                ListOrdersRequest req = new ListOrdersRequest();
                req.CreatedAfter = DateTime.Now.AddDays(-3);
                OrderStatusList statusList=new OrderStatusList();
                //待发货
                List<OrderStatusEnum> status=new List<OrderStatusEnum> {OrderStatusEnum.Unshipped,OrderStatusEnum.PartiallyShipped};
                statusList.Status=status;
                req.OrderStatus = statusList;
                //卖家编号
                req.SellerId = Merchant;
                MarketplaceIdList m=new MarketplaceIdList();
                List<string> mID=new List<string>();
                mID.Add(MarketplaceID);
                m.Id=mID;
                req.MarketplaceId = m;

                MarketplaceWebServiceOrdersConfig config = new MarketplaceWebServiceOrdersConfig();
                config.ServiceURL = "https://mws.amazonservices.com.cn/Orders/2011-01-01";
                config.SetUserAgent(applicationName, applicationVersion);
                MarketplaceWebServiceOrdersClient client = new MarketplaceWebServiceOrdersClient(applicationName, applicationVersion, AWSAccessKeyID, Key, config);

                ListOrdersResponse response = client.ListOrders(req);
                OrderList orders = response.ListOrdersResult.Orders;
                List<Order> orderList=orders.Order;
                string otherSOID = "";
                string sql = "";
                int isImported = 0;
                decimal soAmt = 0;
                //申通
                int expressSysNo = 3;
                //网银在线
                int paySysNo = 3;
                //运费
                decimal expressFee = 0;
                //买家用户名
                string customerUserID = "";
                string receiveName = "";
                string province = "";
                string city = "";
                string district = "";
                string address = "";
                string zip = "";
                string tel = "";
                string mobile = "";
                string customerNote = "";
                int customerSysNo = 0;
                //亚马逊
                int platformSysNo = 16;
                int areaSysNo = 0;
                int salesManSysNo = 1;

                foreach (Order so in orderList)
                {
                    #region 订单
                    //第三方订单号
                    isImported = 1;
                    otherSOID = so.AmazonOrderId;
                    #region 检查第三方订单号是否存在
                    
                    
                    #endregion

                    

                    //订单金额(暂时不确定是否包含运费)
                    decimal.TryParse(so.OrderTotal.Amount, out soAmt);

                    customerUserID = so.BuyerName;
                    //收货人姓名
                    receiveName = so.ShippingAddress.Name;
                    address = so.ShippingAddress.AddressLine1+so.ShippingAddress.AddressLine2+so.ShippingAddress.AddressLine3;
                    province = so.ShippingAddress.StateOrRegion;
                    city = so.ShippingAddress.City;
                    district = so.ShippingAddress.County;
                    zip = so.ShippingAddress.PostalCode;
                    tel = so.ShippingAddress.Phone;

                    

                    try
                    {
                         List<OrderItem> items=DownOrderItem(otherSOID);
                        #region 写销售单的主表数据
                        

                        

                        #endregion

                        #region 产品
                        //订单明细
                        
                        

                        foreach (OrderItem item in items)
                        {

                            //产品编码
                            string productID = item.SellerSKU;
                            //产品小计金额(即ItemPrice 的值等于商品的售价 x 商品的订购数量。)
                            decimal itemPrice = 0;
                            decimal.TryParse(item.ItemPrice.Amount, out itemPrice);
                            //数量
                            decimal qty = item.QuantityOrdered;

                            //售价
                            decimal productSalePrice = 0;
                            productSalePrice = itemPrice/qty;
                            
                            //折后售价
                            decimal currentPrice = 0;
                            currentPrice = productSalePrice*(decimal)0.93;

                            int productSysNo = 0;
                            //系统原价
                            decimal oldPrice = 0;

                            

                            




                        }
                        #endregion

                        writelog(otherSOID + "下载成功");



                    }
                    catch (Exception err2)
                    {
                        writelog(err2.Message);
                    }





                    #endregion
                }





            }
            catch (Exception err)
            {
                writelog(err.Message);
            }
            
        }

        /// <summary>
        
/// 根据第三方订单号获取订单明细
        
/// </summary>
        
/// <param name="otherSOID"></param>
        
/// <returns></returns>
        private List<OrderItem> DownOrderItem(string otherSOID)
        {
            List<OrderItem> resualt = new List<OrderItem>();
            try
            {
                #region 获取明细
                ListOrderItemsRequest req = new ListOrderItemsRequest();
                req.SellerId = Merchant;
                req.AmazonOrderId = otherSOID;

                MarketplaceWebServiceOrdersConfig config = new MarketplaceWebServiceOrdersConfig();
                config.ServiceURL = "https://mws.amazonservices.com.cn/Orders/2011-01-01";
                config.SetUserAgent(applicationName, applicationVersion);
                MarketplaceWebServiceOrdersClient client = new MarketplaceWebServiceOrdersClient(applicationName, applicationVersion, AWSAccessKeyID, Key, config);

                ListOrderItemsResponse response = client.ListOrderItems(req);
                resualt=response.ListOrderItemsResult.OrderItems.OrderItem;

                #endregion
            }
            catch (Exception err)
            {
                writelog(err.Message);
            }
            return resualt;

        }