/// <summary> /// Handles the specified command. /// </summary> /// <param name="command">The command.</param> public async void Handle(AmazonGetOrders3dPartyCommand command) { InfoAccumulator info = new InfoAccumulator(); ListOrdersRequest request = new ListOrdersRequest { MWSAuthToken = command.AuthorizationToken, MarketplaceId = new List <string>(command.MarketplaceId), SellerId = command.SellerId, CreatedAfter = command.DateFrom.ToUniversalTime() }; //get first page ListOrdersResponse response = await AmazonService.Orders.ListOrders(request); List <IEnumerable <AmazonOrderItemAndPayments> > results = new List <IEnumerable <AmazonOrderItemAndPayments> >(); if (response.IsSetListOrdersResult()) { //process first page var orders = CreateOrders(response.ListOrdersResult); results.Add(orders); //get next pages string nextToken = response.ListOrdersResult.NextToken; while (StringUtils.IsNotEmpty(nextToken)) { var nextRequest = new ListOrdersByNextTokenRequest { MWSAuthToken = command.AuthorizationToken, NextToken = nextToken, SellerId = command.SellerId }; //get next page ListOrdersByNextTokenResponse nextResponse = await AmazonService.Orders.ListOrdersByNextToken(nextRequest); if (nextResponse != null && nextResponse.IsSetListOrdersByNextTokenResult()) { //process next page results var nextOrders = nextResponse.ListOrdersByNextTokenResult.Orders.Select(CreateAmazonOrderItem); results.Add(nextOrders); nextToken = nextResponse.ListOrdersByNextTokenResult.NextToken; } else { nextToken = null; } } } SendReply(info, command, resp => resp.OrderPayments = results.SelectMany(o => o).ToArray()); }
/// <summary> /// Gets orders from resources. Contains code to get real orders. /// </summary> /// <param name="container">The container.</param> /// <returns></returns> private static Task <AmazonGetOrders3dPartyCommandResponse> GetAmazonOrders(IContainer container, Context ctx) { if (Resources.AmazonOrdersPayments.IsNotEmpty() && !ctx.IsGetRealOrders) { var response = new AmazonGetOrders3dPartyCommandResponse(); response.OrderPayments = SerializationUtils.DeserializeBinaryJson <IEnumerable <AmazonOrderItemAndPayments> >(Resources.AmazonOrdersPayments); return(CreateCompletedTask(response)); } var getOrders = container.GetInstance <AmazonGetOrdersSendRecieve>(); var command = new AmazonGetOrders3dPartyCommand { SellerId = securityInfo.MerchantId, MarketplaceId = securityInfo.MarketplaceId, AuthorizationToken = "amzn.mws.68a5cceb-b60a-b3f6-291c-08d4595ae879", DateFrom = DateTime.UtcNow.AddMonths(-1) }; return(getOrders.SendAsync(ThirdPartiesService.EndpointName, command)); }
/// <summary> /// Handles the specified command. /// </summary> /// <param name="command">The command.</param> public async void Handle(AmazonRegisterCustomerCommand command) { InfoAccumulator info = new InfoAccumulator(); AmazonGetOrders3dPartyCommand request = new AmazonGetOrders3dPartyCommand { SellerId = command.SellerId, AuthorizationToken = command.AuthorizationToken, MarketplaceId = command.MarketplaceId, DateFrom = DateTime.UtcNow.AddYears(-1) //TODO }; var response = await GetOrdersSendReceive.SendAsync(Config.Address, request); int customerId; try { customerId = CustomerIdEncryptor.DecryptCustomerId(command.CustomerId, command.CommandOriginator); } catch (Exception ex) { Log.Error(ex.Message); info.AddError("Invalid request"); SendReply(info, command, resp => resp.CustomerId = command.CustomerId); return; } int customerMarketPlaceId = 0; //TODO int marketPlaceHistoryId = 0; //TODO AmazonOrder order = new AmazonOrder { Created = DateTime.UtcNow, CustomerMarketPlaceId = customerMarketPlaceId, CustomerMarketPlaceUpdatingHistoryRecordId = marketPlaceHistoryId }; int orderId = (int)AmazonOrdersQueries.SaveOrder(order); if (orderId < 1) { throw new Exception("could not save amazon order"); } bool res = AmazonOrdersQueries.SaveOrdersPayments(response.OrderPayments, orderId); if (!res) { throw new Exception("could not save order/order's payments"); } //Get top N order items IEnumerable <AmazonOrderItem> items = AmazonOrdersQueries.GetTopNOrderItems(customerMarketPlaceId); //Get order details of top N items AmazonGetOrdersDetails3PartyCommand detailsRequestCommand = new AmazonGetOrdersDetails3PartyCommand { SellerId = command.SellerId, AuthorizationToken = command.AuthorizationToken, OrdersIds = items.Where(o => o.AmazonOrderId.HasValue) .Select(o => o.AmazonOrderId.ToString()) }; AmazonGetOrdersDetails3PartyCommandResponse detailsResponse = await GetOrderDetailsSendReceive.SendAsync(Config.Address, detailsRequestCommand); var skuCollection = detailsResponse.OrderDetailsByOrderId.Values .SelectMany(o => o) .Select(o => o.SellerSKU); //Get categories of top N order items var getProductCategoriesCommand = new AmazonGetProductCategories3dPartyCommand { SellerId = command.SellerId, AuthorizationToken = command.AuthorizationToken, SellerSKUs = skuCollection }; var productCategoriesResponse = await GetProductCategoriesSendReceive.SendAsync(Config.Address, getProductCategoriesCommand); IDictionary <string, IList <AmazonOrderItemDetail> > orderIdToOrderDetails = detailsResponse.OrderDetailsByOrderId; //save order item details var insertedOrderDetails = AmazonOrdersQueries.OrderDetails.SaveOrderDetails(orderIdToOrderDetails.Values.SelectMany(o => o)); IDictionary <string, IEnumerable <AmazonProductCategory> > skuToCategory = productCategoriesResponse.CategoriesBySku; //upserts categories IDictionary <AmazonProductCategory, int> categoryToId = AmazonOrdersQueries.Categories.UpsertCategories(productCategoriesResponse.CategoriesBySku.Values.SelectMany(o => o)); res = AmazonOrdersQueries.Categories.SaveCategoryOrderDetailsMapping(CreateOrderIdToOrderDetailsIdMappings(insertedOrderDetails, skuToCategory, categoryToId)); if (!res) { throw new Exception("could not save category order details mapping"); } }