Esempio n. 1
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),
            });
        }
    }
Esempio n. 2
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();
            }
        }