public async Task CreateNewSubscriptionCycles() { var subscriptionCycles = _subscriptionCycleRepository.GetSoonExpires(DateTime.Now, _clientOptions.OrderDaysToExpire); foreach (var cycle in subscriptionCycles) { using var unitOfWork = UnitOfWorkManager.Begin(); SubscriptionCycle newCycle = _subscriptionCycleDomainService.CreateSubscriptionCycle(cycle.Subscription, DateTime.Now); _subscriptionCycleDomainService.PaymentPendingSubscriptionCycle(newCycle); _subscriptionCycleRepository.Insert(newCycle); Order oldOrder = _orderRepository.GetBySubscriptionCycle(cycle); PlanPrice planPrice = cycle.Subscription.Plan.PlanPrices.Single(x => x.Currency.Code == oldOrder.Currency.Code); Order order = _orderDomainService.CreateOrderForRenewSubscription(planPrice, oldOrder.UserId, DateTime.Now); _orderDomainService.PaymentPendingOrder(order); order = _orderRepository.Insert(order); SubscriptionCycleOrder subscriptionCycleOrder = _subscriptionCycleOrderDomainService.CreateSubscriptionCycleOrder(newCycle, order); _subscriptionCycleOrderRepository.Insert(subscriptionCycleOrder); Invoice invoice = _invoiceDomainService.CreateInvoice(order, DateTime.Now); _invoiceDomainService.ActiveInvoice(invoice); _invoiceRepository.Insert(invoice); InvoicePaymentProvider invoicePaymentProvider = planPrice.Currency.Code switch { Domain.ValueObjects.Currency.CurrencyValue.USD => await _paypalService.CreateUriForPayment(invoice, order, cycle.Subscription.Plan), Domain.ValueObjects.Currency.CurrencyValue.ARS => await _mobbexService.CreateUriForPayment(invoice, order, cycle.Subscription.Plan), _ => throw new NotImplementedException() }; _invoicePaymentProviderRepository.Insert(invoicePaymentProvider); Notification notification = _notificationDomainService.CreateNotification(order); _notificationDomainService.SetNewSubscribeCycle(notification); _noticationRepository.Insert(notification); NotificationDto notificationDto = _mapper.Map <NotificationDto>(notification); HttpResponseMessage httpResponse = await _httpClient.PostAsJsonAsync(_clientOptions.NotificactionUrl, notificationDto); if (httpResponse.IsSuccessStatusCode) { _noticationRepository.Delete(notification); } else { _notificationDomainService.AddAttempt(notification); _noticationRepository.Update(notification); } unitOfWork.Complete(); } }