Esempio n. 1
0
        private void PayOrder(InvoicePaymentProvider invoicePaymentProvider)
        {
            InvoiceWebhook invoiceWebhook = _invoiceWebhookDomainService.CreateInvoiceWebhook(invoicePaymentProvider, DateTime.Now);

            try
            {
                Invoice invoice = _invoiceRepository.GetAllIncluding(x => x.Order).Single(x => x.Id == invoicePaymentProvider.InvoceId);

                if (invoice.Order.Status.Status == OrderStatus.OrderStatusValue.PaymentPending)
                {
                    _orderDomainService.PayOrder(invoice.Order);

                    switch (invoice.Order.Type.Type)
                    {
                    case OrderType.OrderTypeValue.Subscription:
                        _eventBus.Trigger(new OrderSubscriptionPayedEventData(invoice.Order));
                        break;

                    case OrderType.OrderTypeValue.RenewSubscription:
                        _eventBus.Trigger(new OrderRenewSubscriptionPayedEventData(invoice.Order));
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Ocurrio un error al Pagar una Orden.", ex);
                _invoiceWebhookDomainService.ChangeToError(invoiceWebhook);
            }
            finally
            {
                _invoiceWebhookRepository.Insert(invoiceWebhook);
            }
        }
        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);
            }
        }