Exemplo n.º 1
0
        private async Task ChangePricePlan(string userId, string newPricePlanId, string rootFolder, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(userId) || string.IsNullOrWhiteSpace(newPricePlanId))
            {
                throw new Exception($"Unable to change the membership as either UserId: '{userId}' is missing or NewPricePlan '{newPricePlanId}' is missing");
            }

            var user = await _userRepository.GetUserById(userId, cancellationToken);

            if (user == null)
            {
                throw new Exception($"User does with '{userId}' identifier does not exists.");
            }

            var newPricePlan = await _pricePlanRepository.GetPricePlanById(newPricePlanId, cancellationToken);

            if (newPricePlan == null)
            {
                throw new Exception($"Unable to find the price plan with '{newPricePlanId}' identifier.");
            }

            if (!newPricePlan.Price.Equals(0))
            {
                var paymentIntent = await _paymentService.CollectPayment(user.PaymentCustomerId, newPricePlan.Price, cancellationToken);

                user.PaymentIntentId = paymentIntent.Id;

                _logger.Information($"Collected payment of '{paymentIntent.Amount}' in minimum unit for user '{user.Id}'.");

                var nextInvoiceNumberDao = await _invoiceNumberRepository.GetNextInvoiceNumber(cancellationToken);

                var invoiceNumber = _invoiceHelper.CalculateInvoiceNumber(nextInvoiceNumberDao.Number);
                try
                {
                    await _invoiceHelper.SendInvoiceAsEmailAttachment(user, paymentIntent, newPricePlan.Title, user.MembershipRenewDate, paymentIntent.Amount, invoiceNumber, rootFolder, cancellationToken);
                }
                catch (Exception ex)
                {
                    _logger.Error($"Unable to send and invoice for an user {user.Id}. Exception: {ex.Message}");
                }

                var paymentDao = new ProcessedPaymentDao
                {
                    UserId           = user.Id,
                    PaymentServiceId = paymentIntent.Id,
                    Amount           = paymentIntent.Amount,
                    PricePlanId      = newPricePlan.Id,
                    NoOfStamps       = newPricePlan.NoOfStamps,
                    Created          = paymentIntent.Created,
                    InvoiceNumber    = invoiceNumber
                };

                await _paymentRepository.CreatePaymentReceived(paymentDao, cancellationToken);
            }

            _logger.Information($"Previous remaining stamp for the user '{user.Id}' is '{user.RemainingTimeStamps}'.");

            user.CurrentPricePlanId   = newPricePlan.Id;
            user.PendingPricePlanId   = null;
            user.RemainingTimeStamps  = newPricePlan.NoOfStamps;
            user.MembershipRenewDate  = user.MembershipRenewDate.AddMonths(1);
            user.MembershipRenewEpoch = user.MembershipRenewDate.Date.ToEpoch();

            await _userRepository.UpdateUser(user, cancellationToken);
        }
Exemplo n.º 2
0
        public void Calling_CalculateInvoiceNumber_With_1_ShouldReturn_AA00001()
        {
            var actualValue = _invoiceHelper.CalculateInvoiceNumber(1);

            Assert.AreEqual($"TS{DateTime.UtcNow:yyMMdd}AA0001", actualValue);
        }
Exemplo n.º 3
0
        public async Task ProcessPayment(string userId, string paymentIntentId, string pricePlanId, CancellationToken cancellationToken)
        {
            Guard.Argument(userId, nameof(userId)).NotNull().NotEmpty().NotWhiteSpace();
            Guard.Argument(paymentIntentId, nameof(paymentIntentId)).NotNull().NotEmpty().NotWhiteSpace();
            Guard.Argument(pricePlanId, nameof(pricePlanId)).NotNull().NotEmpty().NotWhiteSpace();

            var user = await _userRepository.GetUserById(userId, cancellationToken);

            if (user == null)
            {
                throw new UserException("Please create the user first.");
            }

            if (string.IsNullOrWhiteSpace(user.PaymentCustomerId))
            {
                throw new UserException("PaymentCustomerId is missing. Please create the user first.");
            }

            var pricePlan = await _pricePlanRepository.GetPricePlanById(pricePlanId, cancellationToken);

            if (pricePlan == null)
            {
                throw new SubscriptionException($"Unable to find the price plan with '{user.CurrentPricePlanId}' identifier.");
            }

            var paymentIntent = await _paymentService.GetPaymentIntent(paymentIntentId, cancellationToken);

            if (pricePlan.Price != paymentIntent.Amount)
            {
                _logger.Error($"Payment amount is not matching with price plan '{pricePlan.Id}'. Expected '{pricePlan.Price}' and actual '{paymentIntent.Amount}'");
            }

            _logger.Information($"Collected payment of '{paymentIntent.Amount}' in minimum unit for user '{user.Id}'.");

            var nextInvoiceNumberDao = await _invoiceNumberRepository.GetNextInvoiceNumber(cancellationToken);

            var invoiceNumber = _invoiceHelper.CalculateInvoiceNumber(nextInvoiceNumberDao.Number);

            try
            {
                await _invoiceHelper.SendInvoiceAsEmailAttachment(user, paymentIntent, pricePlan.Title, _systemDateTime.GetUtcDateTime(), paymentIntent.Amount, invoiceNumber, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), cancellationToken);
            }
            catch (Exception ex)
            {
                _logger.Error($"Unable to send and invoice for an user {user.Id}. Exception: {ex.Message}");
            }

            var paymentDao = new ProcessedPaymentDao
            {
                UserId           = user.Id,
                PaymentServiceId = paymentIntent.Id,
                Amount           = paymentIntent.Amount,
                PricePlanId      = pricePlan.Id,
                NoOfStamps       = pricePlan.NoOfStamps,
                Created          = paymentIntent.Created,
                InvoiceNumber    = invoiceNumber
            };

            await _paymentRepository.CreatePaymentReceived(paymentDao, cancellationToken);

            _logger.Information($"Previous remaining stamp for the user '{user.Id}' is '{user.RemainingTimeStamps}'.");

            user.CurrentPricePlanId   = pricePlan.Id;
            user.PendingPricePlanId   = null;
            user.RemainingTimeStamps  = pricePlan.NoOfStamps;
            user.PaymentIntentId      = paymentIntentId;
            user.MembershipRenewDate  = _systemDateTime.GetUtcDateTime().AddMonths(1);
            user.MembershipRenewEpoch = user.MembershipRenewDate.Date.ToEpoch();

            await _userRepository.UpdateUser(user, cancellationToken);
        }