Example #1
0
        public async Task HandleEventAsync(OrderRenewSubscriptionPayedEventData eventData)
        {
            Order orderToUpdate = _orderRepository.Get(eventData.Entity.Id);

            PlanPrice planPrice = _planPriceRepository.GetByOrder(eventData.Entity);

            SubscriptionCycleOrder subscriptionCycleOrder = null;
            SubscriptionCycle      subscriptionCycle      = null;

            subscriptionCycleOrder = _subscriptionCycleOrderRepository.GetAll().Single(x => x.OrderId == orderToUpdate.Id);
            subscriptionCycle      = _subscriptionCycleRepository.Get(subscriptionCycleOrder.SubscriptionCycleId);

            _subscriptionCycleDomainService.ActiveSubscriptionCycle(subscriptionCycle, DateTime.Now, planPrice.Plan.Duration);
            _subscriptionCycleRepository.Update(subscriptionCycle);

            Invoice invoice = _invoiceRepository.GetAllIncluding(x => x.InvocePaymentProviders).Single(x => x.OrderId == eventData.Entity.Id);

            _invoiceDomainService.PayInvoice(invoice);
            _invoiceRepository.Update(invoice);

            Notification notification = _notificationDomainService.CreateNotification(orderToUpdate);

            _notificationDomainService.SetOrderPayed(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);
            }
        }
        public async Task HandleEventAsync(OrderSubscriptionPayedEventData eventData)
        {
            PlanPrice planPrice = _planPriceRepository.GetByOrder(eventData.Entity);

            IEnumerable <Order> paymentPendingOrders = _orderRepository.GetPendingPayments(planPrice.Plan, eventData.Entity.UserId);

            foreach (Order order in paymentPendingOrders)
            {
                Order orderToUpdate = null;
                SubscriptionCycleOrder subscriptionCycleOrder = null;
                SubscriptionCycle      subscriptionCycle      = null;
                Subscription           subscription           = null;


                if (order.Id == eventData.Entity.Id)
                {
                    orderToUpdate = _orderRepository.Get(eventData.Entity.Id);

                    subscriptionCycleOrder = _subscriptionCycleOrderRepository.GetAll().Single(x => x.OrderId == orderToUpdate.Id);
                    subscriptionCycle      = _subscriptionCycleRepository.Get(subscriptionCycleOrder.SubscriptionCycleId);
                    subscription           = _subscriptionRepository.Get(subscriptionCycle.SubscriptionId);

                    _orderDomainService.PayOrder(orderToUpdate);
                    _subscriptionDomainService.ActiveSubscription(subscription);
                    _subscriptionCycleDomainService.ActiveSubscriptionCycle(subscriptionCycle, DateTime.Now, planPrice.Plan.Duration);

                    Invoice invoice = _invoiceRepository.Single(x => x.OrderId == order.Id);
                    _invoiceDomainService.PayInvoice(invoice);
                    _invoiceRepository.Update(invoice);

                    Notification notification = _notificationDomainService.CreateNotification(orderToUpdate);
                    _notificationDomainService.SetOrderPayed(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);
                    }
                }
                else
                {
                    orderToUpdate = _orderRepository.Get(order.Id);

                    subscriptionCycleOrder = _subscriptionCycleOrderRepository.GetAll().Single(x => x.OrderId == orderToUpdate.Id);
                    subscriptionCycle      = _subscriptionCycleRepository.Get(subscriptionCycleOrder.SubscriptionCycleId);
                    subscription           = _subscriptionRepository.Get(subscriptionCycle.SubscriptionId);

                    _subscriptionDomainService.CancelSubscription(subscription);
                    _subscriptionCycleDomainService.CancelSubscriptionCycle(subscriptionCycle);
                    _orderDomainService.CancelOrder(orderToUpdate);

                    Invoice invoice = _invoiceRepository.GetAllIncluding(x => x.InvocePaymentProviders).Single(x => x.OrderId == order.Id);
                    _invoiceDomainService.CancelInvoice(invoice);
                    _invoiceRepository.Update(invoice);

                    PaymentProvider.PaymentProviderValue paymentProviderValue = order.Currency.Code == Currency.CurrencyValue.USD ? PaymentProvider.PaymentProviderValue.Paypal : PaymentProvider.PaymentProviderValue.Mobbex;
                    InvoicePaymentProvider invoicePaymentProvider             = invoice.InvocePaymentProviders.Single(x => x.PaymentProvider.Provider == paymentProviderValue);

                    switch (order.Currency.Code)
                    {
                    case Currency.CurrencyValue.ARS:
                        await _mobbexService.CancelInvoice(invoicePaymentProvider);

                        break;

                    case Currency.CurrencyValue.USD:
                        await _paypalService.CancelInvoice(invoicePaymentProvider);

                        break;

                    default:
                        throw new NotImplementedException();
                    }
                }

                _subscriptionRepository.Update(subscription);
                _subscriptionCycleRepository.Update(subscriptionCycle);
                _orderRepository.Update(orderToUpdate);
            }
        }
Example #3
0
        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();
            }
        }