public async Task <OrderDto> UpdateOrder(UpdateOrderInput input) { Order order = _repository.GetAll().Include(x => x.Invoices).ThenInclude(x => x.InvocePaymentProviders).Single(x => x.Id == input.Id); if (order.Status.Status == OrderStatus.OrderStatusValue.Payed) { throw new UserFriendlyException("No se puede editar una orden que ya fue Pagada."); } if (order.Status.Status == OrderStatus.OrderStatusValue.Canceled) { throw new UserFriendlyException("No se puede editar una orden que ya fue Camcelada."); } order = _mapper.Map <UpdateOrderInput, Order>(input, order); _repository.Update(order); var invocePaymentProvider = order.Invoices.Single().InvocePaymentProviders.Single(); if (order.Status.Status == OrderStatus.OrderStatusValue.Payed) { switch (order.Type.Type) { case OrderType.OrderTypeValue.Subscription: _eventBus.Trigger(new OrderSubscriptionPayedEventData(order)); break; case OrderType.OrderTypeValue.RenewSubscription: _eventBus.Trigger(new OrderRenewSubscriptionPayedEventData(order)); break; case OrderType.OrderTypeValue.Extra: _eventBus.Trigger(new OrderExtraPayedEventData(order)); break; default: throw new NotImplementedException(); } switch (order.Currency.Code) { case Domain.ValueObjects.Currency.CurrencyValue.ARS: await _mobbexService.CancelInvoice(invocePaymentProvider); break; case Domain.ValueObjects.Currency.CurrencyValue.USD: await _paypalService.CancelInvoice(invocePaymentProvider); break; default: throw new NotImplementedException(); } } if (order.Status.Status == OrderStatus.OrderStatusValue.PaymentPending) { switch (order.Currency.Code) { case Domain.ValueObjects.Currency.CurrencyValue.ARS: await _mobbexService.CancelInvoice(invocePaymentProvider); var newinvoicePaymentProvider = await _mobbexService.CreateUriForPayment(order.Invoices.Single(), order, string.Empty); invocePaymentProvider.Link = newinvoicePaymentProvider.Link; invocePaymentProvider.Transaction = newinvoicePaymentProvider.Transaction; break; case Domain.ValueObjects.Currency.CurrencyValue.USD: invocePaymentProvider = await _paypalService.UpdateAmountInvoice(invocePaymentProvider, order); break; default: throw new NotImplementedException(); } _invoicePaymentProviderRepository.Update(invocePaymentProvider); } return(_mapper.Map <OrderDto>(order)); }
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); } }