Exemple #1
0
        /// <summary>
        /// Default public contructor. All properties are set via the config file
        /// </summary>
        public AmazonIntegration()
        {
            // Verify that the settings in the config file are setup correctly.
            if (string.IsNullOrWhiteSpace(_AccessKeyId))
            {
                throw new InvalidOperationException("AWSAccessKeyId setting in the config file can't be whitespace, blank or null");
            }
            if (string.IsNullOrWhiteSpace(_SecretAccessKey))
            {
                throw new InvalidOperationException("AWSSecretAccessKey setting in the config file can't be whitespace, blank or null");
            }
            if (string.IsNullOrWhiteSpace(_ApplicationName))
            {
                throw new InvalidOperationException("AWSApplicationName setting in the config file can't be whitespace, blank or null");
            }
            if (string.IsNullOrWhiteSpace(_ApplicationVersion))
            {
                throw new InvalidOperationException("AWSApplicationVersion setting in the config file can't be whitespace, blank or null");
            }
            if (string.IsNullOrWhiteSpace(_MerchantId))
            {
                throw new InvalidOperationException("AWSMerchantId setting in the config file can't be whitespace, blank or null");
            }
            if (string.IsNullOrWhiteSpace(_MarketplaceId))
            {
                throw new InvalidOperationException("AWSMarketplaceId setting in the config file can't be whitespace, blank or null");
            }
            if (string.IsNullOrWhiteSpace(_TemporaryFileDirectory))
            {
                throw new InvalidOperationException("TempFileDirectory setting in the config file can't be whitespace, blank or null");
            }

            var config = new MarketplaceWebServiceConfig();

            // Set configuration to use US marketplace
            config.ServiceURL = "https://mws.amazonservices.com";
            // Set the HTTP Header for user agent for the application.
            config.SetUserAgentHeader(
                _ApplicationName,
                _ApplicationVersion,
                "C#");
            _AmazonClient = new MarketplaceWebServiceClient(_AccessKeyId, _SecretAccessKey, config);

            // Setup the orders service client
            var ordersConfig = new MarketplaceWebServiceOrdersConfig();

            ordersConfig.ServiceURL = "https://mws.amazonservices.com/Orders/2011-01-01";
            ordersConfig.SetUserAgent(_ApplicationName, _ApplicationVersion);
            _AmazonOrdersClient = new MarketplaceWebServiceOrdersClient(
                _ApplicationName, _ApplicationVersion, _AccessKeyId, _SecretAccessKey, ordersConfig);
        }
Exemple #2
0
        private List <MarketplaceOrder> getMarketplaceOrdersData(List <string> orderIds)
        {
            // init the Amazon web service
            var config = new MarketplaceWebServiceOrdersConfig {
                ServiceURL = "https://mws.amazonservices.com"
            };

            config.SetUserAgent(_ApplicationName, _Version);
            _ordersClient = new MarketplaceWebServiceOrdersClient(_credential.AccessKeyId, _credential.SecretKey, config);
            var results = new List <MarketplaceOrder>();

            try
            {
                // create the request object
                var request = new GetOrderRequest
                {
                    AmazonOrderId = orderIds,
                    SellerId      = _credential.MerchantId
                };

                // send the request
                var orderResponse = _ordersClient.GetOrder(request);
                var ordersResult  = orderResponse.GetOrderResult.Orders;
                if (!ordersResult.Any())
                {
                    return(null);
                }

                foreach (var item in ordersResult)
                {
                    results.Add(convertOrderResponseToMarketplaceOrder(item));
                }

                return(results);
            }
            catch (Exception ex)
            {
                _logger.LogError(LogEntryType.AmazonOrdersProvider,
                                 string.Format("{0} - Error in getting latest order data for Order Id: {1}. Error Message: {2}", ChannelName, string.Join(",", orderIds), EisHelper.GetExceptionMessage(ex)),
                                 ex.StackTrace);
                return(null);
            }
        }
Exemple #3
0
        public IEnumerable <MarketplaceOrder> GetMarketplaceOrders(DateTime createdAfter)
        {
            var marketplaceOrders = new List <MarketplaceOrder>();

            try
            {
                // init the Amazon web service
                var config = new MarketplaceWebServiceOrdersConfig {
                    ServiceURL = "https://mws.amazonservices.com"
                };
                config.SetUserAgent(_ApplicationName, _Version);
                _ordersClient = new MarketplaceWebServiceOrdersClient(_credential.AccessKeyId, _credential.SecretKey, config);

                Console.WriteLine("{0} fetching orders for {1}...", ChannelName, createdAfter);

                // create ListOrdersRequest object
                var listOrdersRequest = new ListOrdersRequest
                {
                    SellerId      = _credential.MerchantId,
                    MarketplaceId = new List <string> {
                        { _credential.MarketplaceId }
                    },
                    LastUpdatedAfter = createdAfter.Date
                };
                var listOrdersResponse = _ordersClient.ListOrders(listOrdersRequest);
                var ordersResult       = listOrdersResponse.ListOrdersResult.Orders;
                var nextToken          = listOrdersResponse.ListOrdersResult.NextToken;
                ListOrdersByNextTokenResponse nextOrderResponse = null;

                Console.WriteLine("{0} retrieved {1} orders and {2} next results page.", ChannelName, listOrdersResponse.ListOrdersResult.Orders.Count, !string.IsNullOrWhiteSpace(nextToken) ? "HAS" : "NO");

                do
                {
                    if (nextOrderResponse != null)
                    {
                        ordersResult = nextOrderResponse.ListOrdersByNextTokenResult.Orders;
                        nextToken    = nextOrderResponse.ListOrdersByNextTokenResult.NextToken;
                        Console.WriteLine("{0} retrieved {1} next orders and {2} next results page.", ChannelName, ordersResult.Count, !string.IsNullOrWhiteSpace(nextToken) ? "HAS" : "NO");
                    }

                    // Convert the orders to the marketplace contracts and get the items.
                    for (int i = 0; i < ordersResult.Count; ++i)
                    {
                        // The maximum request quota for ListOrderItems is 30 after that it restores at 2 per second.
                        // So if the order's that are being pulled exceed 30, then sleep for 2 seconds each one.
                        if (i > 30)
                        {
                            Thread.Sleep(2000);
                        }

                        marketplaceOrders.Add(convertOrderResponseToMarketplaceOrder(ordersResult[i]));
                    }

                    // do a rquest for the next page result of orders if next token is not null
                    if (!string.IsNullOrWhiteSpace(nextToken))
                    {
                        // pause at least 1 minute, this is the restore rate for ListOrdersByNextToken for every 6 quota
                        Thread.Sleep(61000);

                        var nextTokenRequest = new ListOrdersByNextTokenRequest {
                            SellerId = _credential.MerchantId, NextToken = nextToken
                        };
                        nextOrderResponse = _ordersClient.ListOrdersByNextToken(nextTokenRequest);
                    }
                } while (!string.IsNullOrWhiteSpace(nextToken));

                Console.WriteLine("{0} done fetching orders: {1} items", ChannelName, marketplaceOrders.Count);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in getting orders for {0}! Error message: {1}", ChannelName, ex.Message);
                _logger.LogError(LogEntryType.AmazonOrdersProvider,
                                 string.Format("Error in retrieving orders for Amazon. Error message: {0}", ex.Message),
                                 ex.StackTrace);

                _emailService.SendEmailAdminException(subject: "Amazon - Get Marketplace Orders Error",
                                                      exParam: ex,
                                                      useDefaultTemplate: true,
                                                      url: "GetMarketplaceOrders Method",
                                                      userName: "******");
            }

            return(marketplaceOrders);
        }