public async Task <InvoicePaymentProvider> CreateUriForPayment(Invoice invoice, Order order, Plan plan) { PlanPrice price = plan.PlanPrices.Single(x => x.Currency.Code == Currency.CurrencyValue.ARS); var contract = new CheckoutCreateContract { Total = price.Price, Currency = Currency.CurrencyValue.ARS.ToString(), Reference = invoice.Id.ToString(), Description = plan.Description, ReturnUrl = _mobbexOptions.ReturnUrl + "?invoiceId=" + invoice.Id, Webhook = _mobbexOptions.WebhookUrl + "?invoiceId=" + invoice.Id, Test = _mobbexOptions.Environment == Domain.Enums.PaymentProviderEnvironment.Sandbox, Options = new SubscriptionOptionsDto() }; var response = await _apiGateway.PostAsync <CheckoutContract, CheckoutCreateContract>(ENDPOINT, contract); return(new InvoicePaymentProvider { Link = new System.Uri(response.Data.Url), Transaction = response.Data.Id, InvoceId = invoice.Id, PaymentProvider = new PaymentProvider(PaymentProvider.PaymentProviderValue.Mobbex) }); }
public PlanPriceDto GetByOrder(Guid orderId) { Order order = _orderRepository.Get(orderId); PlanPrice planprice = _planPriceRepository.GetByOrder(order); PlanPriceDto planDto = _mapper.Map <PlanPriceDto>(planprice); return(planDto); }
public PlanPriceDto UpdatePrice(CreatePlanPriceInput input) { PlanPrice planPrice = _mapper.Map <PlanPrice>(input); PlanPrice currentPlanPrice = _planPriceRepository.GetByPlan(input.PlanId, planPrice.Currency); planPrice = _mapper.Map <CreatePlanPriceInput, PlanPrice>(input, currentPlanPrice); _planPriceRepository.Update(planPrice); return(_mapper.Map <PlanPriceDto>(planPrice)); }
public Order CreateOrderForRenewSubscription(PlanPrice planPrice, Guid userId, DateTime creationDate) { return(new Order { CreationTime = creationDate, Currency = planPrice.Currency, Status = new OrderStatus(OrderStatus.OrderStatusValue.Created), TotalAmount = planPrice.Price, UserId = userId, Type = new OrderType(OrderType.OrderTypeValue.RenewSubscription) }); }
public PlanPriceDto AddPrice(CreatePlanPriceInput input) { PlanPrice planPrice = _mapper.Map <PlanPrice>(input); PlanPrice currentPlanPrice = _planPriceRepository.GetByPlan(input.PlanId, planPrice.Currency); if (currentPlanPrice.IsNotNull()) { throw new UserFriendlyException("Un plan no puede tener 2 precios de la misma moneda."); } _planPriceRepository.Insert(planPrice); return(_mapper.Map <PlanPriceDto>(planPrice)); }
public async Task HandleEventAsync(OrderRenewSubscriptionPayedEventData eventData) { Order orderToUpdate = _orderRepository.Get(eventData.Entity.Id); PlanPrice planPrice = _planPriceRepository.GetByOrder(eventData.Entity); SubscriptionCycleOrder subscriptionCycleOrder = null; SubscriptionCycle subscriptionCycle = null; subscriptionCycleOrder = _subscriptionCycleOrderRepository.GetAll().Single(x => x.OrderId == orderToUpdate.Id); subscriptionCycle = _subscriptionCycleRepository.Get(subscriptionCycleOrder.SubscriptionCycleId); _subscriptionCycleDomainService.ActiveSubscriptionCycle(subscriptionCycle, DateTime.Now, planPrice.Plan.Duration); _subscriptionCycleRepository.Update(subscriptionCycle); Invoice invoice = _invoiceRepository.GetAllIncluding(x => x.InvocePaymentProviders).Single(x => x.OrderId == eventData.Entity.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); } }
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), }); } }
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); } }
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(); } }