/// <summary>
        /// Execute task
        /// </summary>
        /// <returns>The <see cref="Task"/></returns>
        public async Task ExecuteAsync()
        {
            var stores = await _storeService.GetAllStoresAsync();

            foreach (var store in stores)
            {
                var validationResult = await _openPayService.ValidateAsync(store.Id);

                if (!validationResult.IsValid)
                {
                    await _logger.ErrorAsync($"{Defaults.SystemName}: Cannot update the status of the orders in the store '{store.Name}' when background task was processed.{Environment.NewLine}{string.Join(Environment.NewLine, validationResult.Errors)}");

                    continue;
                }

                // get all non-paid orders including previous month
                var orders = (await _orderService.SearchOrdersAsync(
                                  storeId: store.Id,
                                  createdFromUtc: DateTime.UtcNow.AddMonths(-1),
                                  psIds: new List <int>
                {
                    (int)PaymentStatus.Pending,
                    (int)PaymentStatus.Authorized
                }))?.Where(o => o.PaymentMethodSystemName == Defaults.SystemName);

                if (orders?.Any() == true)
                {
                    var openPayPaymentSettings = await _settingService.LoadSettingAsync <OpenPayPaymentSettings>(store.Id);

                    _openPayApi.ConfigureClient(openPayPaymentSettings);

                    foreach (var order in orders)
                    {
                        var result = await _openPayService.CaptureOrderAsync(order);

                        if (string.IsNullOrEmpty(result.OrderId))
                        {
                            await _logger.ErrorAsync($"{Defaults.SystemName}: Cannot update the status of the order '{order.CustomOrderNumber}' in the store '{store.Name}' when background task was processed.{Environment.NewLine}{string.Join(Environment.NewLine, result.Errors)}");
                        }
                        else if (_orderProcessingService.CanMarkOrderAsPaid(order))
                        {
                            order.CaptureTransactionId = result.OrderId;
                            await _orderProcessingService.MarkOrderAsPaidAsync(order);
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Execute task
        /// </summary>
        /// <returns>The <see cref="Task"/></returns>
        public async Task ExecuteAsync()
        {
            var stores = await _storeService.GetAllStoresAsync();

            foreach (var store in stores)
            {
                var validationResult = await _openPayService.ValidateAsync(store.Id);

                if (!validationResult.IsValid)
                {
                    await _logger.ErrorAsync($"{Defaults.SystemName}: Cannot get the order limits for the store '{store.Name}' when background task was processed.{Environment.NewLine}{string.Join(Environment.NewLine, validationResult.Errors)}");

                    continue;
                }

                var openPayPaymentSettings = await _settingService.LoadSettingAsync <OpenPayPaymentSettings>(store.Id);

                _openPayApi.ConfigureClient(openPayPaymentSettings);

                try
                {
                    var limits = await _openPayApi.GetOrderLimitsAsync();

                    openPayPaymentSettings.MinOrderTotal = limits.MinPrice / 100;
                    openPayPaymentSettings.MaxOrderTotal = limits.MaxPrice / 100;

                    await _settingService.SaveSettingOverridablePerStoreAsync(openPayPaymentSettings, x => x.MinOrderTotal, stores.Count > 1, store.Id, false);

                    await _settingService.SaveSettingOverridablePerStoreAsync(openPayPaymentSettings, x => x.MaxOrderTotal, stores.Count > 1, store.Id, false);

                    await _settingService.ClearCacheAsync();
                }
                catch (ApiException ex)
                {
                    await _logger.ErrorAsync($"{Defaults.SystemName}: Cannot get the order limits for the store '{store.Name}' when background task was processed.", ex);
                }
            }
        }