예제 #1
0
        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));
        }
예제 #2
0
        public async Task <SubscriptionOrderDto> CreateSubscription(CreateSubscriptionInput input)
        {
            PlanPrice planPrice = _planPriceRepository.Get(input.PlanPriceId);

            if (planPrice.IsNull())
            {
                throw new UserFriendlyException("No existe un PlanPrice con ese Id");
            }

            Plan plan = _planRepository.Get(planPrice.PlanId);

            Subscription actualSubscriptions = _subscriptionRepository.GetSubscriptionPlanActive(input.UserId, plan.ProductId);

            if (actualSubscriptions.IsNotNull())
            {
                throw new UserFriendlyException("El usuario ya esta suscripto a un Plan del mismo producto.");
            }



            Subscription subscription = _subscriptionDomainService.CreateSubscription(plan);

            subscription = _subscriptionRepository.Insert(subscription);



            SubscriptionCycle subscriptionCycle = _subscriptionCycleDomainService.CreateSubscriptionCycle(subscription, DateTime.Now);

            _subscriptionCycleDomainService.PaymentPendingSubscriptionCycle(subscriptionCycle);
            subscriptionCycle = _subscriptionCycleRepository.Insert(subscriptionCycle);


            Order order = _orderDomainService.CreateOrderForSubscription(planPrice, input.UserId, DateTime.Now);

            _orderDomainService.PaymentPendingOrder(order);
            order = _orderRepository.Insert(order);



            SubscriptionCycleOrder subscriptionCycleOrder = _subscriptionCycleOrderDomainService.CreateSubscriptionCycleOrder(subscriptionCycle, 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, plan),
                Domain.ValueObjects.Currency.CurrencyValue.ARS => await _mobbexService.CreateUriForPayment(invoice, order, plan),
                _ => throw new NotImplementedException()
            };

            invoicePaymentProvider = _invoicePaymentProviderRepository.Insert(invoicePaymentProvider);


            return(new SubscriptionOrderDto
            {
                Subscription = _mapper.Map <SubscriptionDto>(subscription),
                SubscriptionCycle = _mapper.Map <SubscriptionCycleDto>(subscriptionCycle),
                SubscriptionCycleOrder = _mapper.Map <SubscriptionCycleOrderDto>(subscriptionCycleOrder),
                Order = _mapper.Map <OrderDto>(order),
                InvoicePaymentProvider = _mapper.Map <InvoicePaymentProviderDto>(invoicePaymentProvider),
            });
        }
    }
예제 #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();
            }
        }