コード例 #1
0
        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);
            }
        }