Ejemplo n.º 1
0
        public async Task <IActionResult> PayDebt(DebtPayDTO model)
        {
            try
            {
                await _debtService.DebtPayAsync(model);

                return(Ok());
            }
            catch (DbUpdateConcurrencyException)
            {
                return(BadRequest(new
                {
                    Message =
                        "The record you attempted to edit was modified by another user after you got the original value"
                }));
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }
            catch (Exception e)
            {
                return(BadRequest(new { e.Message }));
            }
        }
Ejemplo n.º 2
0
        public async Task DebtPayAsync(DebtPayDTO model)
        {
            var idClaim = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            if (!long.TryParse(idClaim, out var ownerId))
            {
                throw new UnauthorizedAccessException();
            }

            var debtToPay =
                await(await _debtRepository.GetAllAsync(debt => debt.Id == model.DebtId))
                .Include(debt => debt.Owner)
                .Include(debt => debt.Currency)
                .Include(debt => debt.Friend)
                .ThenInclude(friend => friend.FriendUser)
                .FirstOrDefaultAsync();

            if (debtToPay == null)
            {
                throw new ArgumentException("Debt is not exist");
            }

            Currency payCurrency = null;
            decimal  valueToPay  = 0;
            Charge   charge      = null;

            if (debtToPay.IsMoney)
            {
                if (!debtToPay.Value.HasValue || !debtToPay.CurrentValue.HasValue)
                {
                    throw new NoNullAllowedException("Value and CurrentValue can't be null");
                }

                if (!model.CurrencyId.HasValue)
                {
                    throw new ArgumentNullException(nameof(model.CurrencyId));
                }

                if (!model.Value.HasValue)
                {
                    throw new ArgumentNullException(nameof(model.Value));
                }

                if (!model.IsNotification && string.IsNullOrWhiteSpace(model.Token))
                {
                    throw new ArgumentException("Token can't be empty");
                }


                payCurrency = await _currenciesRepository.GetByIdAsync(model.CurrencyId.Value);

                if (payCurrency == null)
                {
                    throw new SqlNullValueException("Currency with such id not found");
                }

                valueToPay = await _exchangeRateService.Convert(model.Value.Value, payCurrency, debtToPay.Currency);

                if (debtToPay.CurrentValue + debtToPay.PendingValue + valueToPay > debtToPay.Value)
                {
                    throw new ArgumentException("You can't pay more, than debt ");
                }

                debtToPay.PendingValue += valueToPay;

                if (!model.IsNotification)
                {
                    charge = await _stripeChargeService.Charge(new ChargeDTO
                    {
                        Value    = (long)(valueToPay * 100),
                        Currency = debtToPay.Currency.CurrencySymbol,
                        Token    = model.Token
                    });
                }
            }
            else
            {
                if (!model.IsNotification)
                {
                    throw new ArgumentException("You can pay thing debts only by notification");
                }
            }

            var userToPay = debtToPay.IsOwnerDebter ? debtToPay.Friend.FriendUser : debtToPay.Owner;
            var userPayer = debtToPay.IsOwnerDebter ? debtToPay.Owner : debtToPay.Friend.FriendUser;


            var newPayment = new Payment
            {
                CreatedBy       = ownerId,
                Payer           = userPayer,
                UserToPay       = userToPay,
                Debt            = debtToPay,
                Value           = valueToPay,
                Currency        = payCurrency,
                Message         = model.Message,
                PaymentType     = model.IsNotification ? PaymentType.Notification : PaymentType.Stripe,
                Status          = Status.Pending,
                TransactionId   = charge?.BalanceTransactionId,
                TransactionDate = charge?.Created
            };

            await _paymentRepository.InsertAsync(newPayment);

            await _debtRepository.UpdateAsync(debtToPay);

            if (newPayment.PaymentType == PaymentType.Notification && debtToPay.Synchronize &&
                (debtToPay.IsOwnerDebter && debtToPay.Owner.Id == ownerId ||
                 !debtToPay.IsOwnerDebter && debtToPay.Friend.FriendUser.Id == ownerId))
            {
                await _hubContext.Clients.User(userToPay.Id.ToString())
                .SendAsync("UpdatePayNotifications");
            }


            await _hubContext.Clients.User(ownerId.ToString())
            .SendAsync("UpdateDebtById", debtToPay.Id);
        }