Пример #1
0
        public async Task <IActionResult> PostTransaction(NSTransactionDto newTransaction)
        {
            var userId       = HttpContext.GetUserId();
            var isSuccessful = await _repo.AddTransaction(newTransaction, userId);

            if (isSuccessful)
            {
                return(RedirectToAction("Success", "Home"));
            }

            return(RedirectToAction("Error", "Home"));
        }
Пример #2
0
        public async Task <ApiResponseDto <bool> > AddFirstTimeUserTransaction(FirstTimeUserTransactionRegisterationDto registerationDto)
        {
            var merchantToRegister     = registerationDto.Merchant;
            var merchantBankingDetails = registerationDto.MerchantBankingDetails;
            var buyerToRegiser         = registerationDto.Buyer;
            var buyerBankingDetails    = registerationDto.BuyerBankingDetails;

            await _authServices.Register(merchantToRegister);

            await _authServices.Register(buyerToRegiser);


            var merchant = await _userRepo.FindUserByEmail(merchantToRegister.Email);

            var buyer = await _userRepo.FindUserByEmail(buyerToRegiser.Email);

            if (merchant == null || buyer == null)
            {
                return(new ApiResponseDto <bool>(500, "An error occured while creating transaction, please try again",
                                                 "Error creating transaction", false));
            }


            await _bankingService.SaveBankingDetails((long)merchant.Id, merchantBankingDetails);

            await _bankingService.SaveBankingDetails((long)buyer.Id, buyerBankingDetails);

            var transaction = new Transaction()
            {
                Code        = await GenerateTransactionCode(),
                ProductName = registerationDto.ProductName,
                Category    = registerationDto.Category,
                Charges     = registerationDto.Charges,
                Description = registerationDto.Description,
                Merchant    = merchant,
                Buyer       = buyer
            };

            _transRepo.AddTransaction(transaction);
            var isUpdated = await _transRepo.SaveChanges();

            if (!isUpdated)
            {
                return(new ApiResponseDto <bool>(500, "An error occured while creating transaction, login in to your account and try again",
                                                 "Error creating transaction", false));
            }
            await _mailingServices.SendTransactionMail(merchant, buyer);

            await _mailingServices.SendTransactionMail(buyer, merchant);

            return(new ApiResponseDto <bool>(200, "Transaction successfully created, check your email for transaction details", null, true));
        }
Пример #3
0
        public async Task <TransactionDetailsModel> CreateTransactionAsync(CreateTransactionModel transaction)
        {
            if (await IsEnoughBalanceAsync(transaction) == false)
            {
                throw new CreateTransactionException(CreateTransactionErrors.NotEnoughBalance);
            }

            var transactionDto = Mapper.Map <TransactionDto>(transaction);

            transactionDto.Occured = DateTime.UtcNow;

            var entity = _transactionsRepository.AddTransaction(transactionDto);

            var mappedEntity = Mapper.Map <TransactionDetailsModel>(entity);

            return(mappedEntity);
        }
        public Transaction PerformNewDeal(TransactionToAdd transaction)
        {
            if (transaction == null || transaction.SellerId == 0 || transaction.BuyerId == 0 || transaction.ShareId == 0 || transaction.Quantity == 0 || transaction.SellerId == transaction.BuyerId)
            {
                return(null);
            }

            var sellerToChange            = tradersRepository.GetTrader(transaction.SellerId);
            var buyerToChange             = tradersRepository.GetTrader(transaction.BuyerId);
            var sellerShareRecordToChange = portfoliosRepository.GetPortfolio(transaction.SellerId, transaction.ShareId);
            var sharePrice = sharesRepository.GetPrice(transaction.ShareId);

            if (sellerToChange == null || buyerToChange == null || sellerShareRecordToChange == null)
            {
                return(null);
            }

            tradersRepository.AddToBalance(sellerToChange.TraderId, sharePrice * transaction.Quantity);
            tradersRepository.SubtractFromBalance(buyerToChange.TraderId, sharePrice * transaction.Quantity);

            portfoliosRepository.RemoveShares(sellerShareRecordToChange, transaction.Quantity);

            if (sellerShareRecordToChange.Quantity == 0)
            {
                portfoliosRepository.RemovePortfolio(sellerShareRecordToChange);
            }

            if (portfoliosRepository.GetPortfoliosCount(transaction.BuyerId, transaction.ShareId) > 0)
            {
                var buyerShareRecordToChange = portfoliosRepository.GetPortfolio(transaction.BuyerId, transaction.ShareId);

                if (buyerShareRecordToChange != null)
                {
                    portfoliosRepository.AddShares(buyerShareRecordToChange, transaction.Quantity);
                }
            }
            else
            {
                portfoliosRepository.AddPortfolio(transaction.BuyerId, transaction.ShareId, transaction.Quantity);
            }

            var newTransaction = transactionsRepository.AddTransaction(transaction.BuyerId, transaction.SellerId, transaction.ShareId, sharePrice, transaction.Quantity);

            return(newTransaction);
        }
Пример #5
0
        public async Task ProcessTransaction(Transaction transaction)
        {
            ValidateTransactionAmount(transaction);
            ValidateTransactionAccounts(transaction);

            Console.WriteLine("TransactionsService: Started processing transaction.");

            var fromAccount = await _accountsService.GetAccount(transaction.FromAccountId);

            ValidateAccountHasEnoughMoney(fromAccount.Balance, transaction.Amount);

            var toAccount = await _accountsService.GetAccount(transaction.ToAccountId);

            await _transactionSemaphore.WaitAsync();

            try
            {
                fromAccount = await _accountsService.GetAccount(transaction.FromAccountId);

                ValidateAccountHasEnoughMoney(fromAccount.Balance, transaction.Amount);

                toAccount = await _accountsService.GetAccount(transaction.ToAccountId);

                fromAccount.Balance -= transaction.Amount;
                await _accountsService.UpdateAccount(fromAccount);

                toAccount.Balance += transaction.Amount;
                await _accountsService.UpdateAccount(toAccount);

                var transactionEntity = _mapper.Map <TransactionEntity>(transaction);
                await _transactionsRepository.AddTransaction(transactionEntity);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Transaction failed ({ex.Message})");
                throw;
            }
            finally
            {
                _transactionSemaphore.Release();
            }
        }
Пример #6
0
        private async Task ProcessBasicTransaction()
        {
            var    transaction = new WalletTransaction();
            double amount;

            if (double.TryParse(AmountLabelText, out amount))
            {
                if (TransactionType == TransactionType.EXPENSES)
                {
                    transaction.Amount = -amount;
                }
                else
                {
                    transaction.Amount = amount;
                }

                transaction.Date = new DateTimeOffset(DateTime.Now);
                await _transactionsRepository.AddTransaction(transaction, _rightButtonText, _leftButtonText);
            }
        }
Пример #7
0
 public ActionResult AddTransaction(Transactions transaction)
 {
     _transactionsRepository.AddTransaction(transaction);
     return(RedirectToAction("Index", "Home"));
 }