Пример #1
0
        public async Task <InternalTransferPaymentTransactionsDTO> MakeInternalTransferPaymentTransaction(string fromUniqueMasterCitizenNumber,
                                                                                                          string password,
                                                                                                          string toUniqueMasterCitizenNumber,
                                                                                                          decimal amount)
        {
            Wallet fromWallet = await CheckForWallet(fromUniqueMasterCitizenNumber, password);

            Wallet toWallet = await _unitOfWork.WalletRepository
                              .GetFirstWithIncludes(Wallet =>
                                                    Wallet.UniqueMasterCitizenNumber.Value == toUniqueMasterCitizenNumber,
                                                    Wallet => Wallet.PaymentTransactions
                                                    );

            if (toWallet == null)
            {
                throw new ArgumentException($"Wallet with unique master citizen number {toUniqueMasterCitizenNumber} doesn't exist");
            }
            IBankAPI bankAPI       = _bankAPIDeterminator.DeterminateBankAPI(fromWallet.SupportedBank);
            IBankAPI secondBankAPI = _bankAPIDeterminator.DeterminateBankAPI(toWallet.SupportedBank);

            InternalTransferPaymentTransactions paymentTransactions = fromWallet.MakeInternalTransfer(toWallet, amount);

            bool successWithdrawal = await bankAPI.Withdraw(fromUniqueMasterCitizenNumber, fromWallet.PostalIndexNumber, amount);

            if (!successWithdrawal)
            {
                CleanupWallets(fromWallet, toWallet, paymentTransactions);
                throw new BankAPIException("Bank api - failed to withdrawal");
            }
            bool successDeposit = await secondBankAPI.Withdraw(toUniqueMasterCitizenNumber, toWallet.PostalIndexNumber, amount);

            if (!successDeposit)
            {
                CleanupWallets(fromWallet, toWallet, paymentTransactions);
                throw new BankAPIException("Bank api - failed to deposit");
            }
            await _unitOfWork.PaymentTransactionRepository.Insert(paymentTransactions.Deposit);

            await _unitOfWork.PaymentTransactionRepository.Insert(paymentTransactions.Withdrawal);

            if (paymentTransactions.HasFee())
            {
                await _unitOfWork.PaymentTransactionRepository.Insert(paymentTransactions.Fee);
            }
            await _unitOfWork.SaveChangesAsync();

            return(new InternalTransferPaymentTransactionsDTO(paymentTransactions));
        }
Пример #2
0
        public InternalTransferPaymentTransactions MakeInternalTransfer(Wallet toWallet, decimal amount)
        {
            CheckIfWalletIsBlocked();
            toWallet.CheckIfWalletIsBlocked();
            decimal feeAmount = FeeCalculator.CalculateFee(this, amount);
            bool    isWithdrawalLimitExceeded = LimitPaymentTransactionCalculator.IsWithdrawalLimitExceed(this, amount);

            if (isWithdrawalLimitExceeded)
            {
                throw new LimitExceededException();
            }
            bool isDepositLimitExceeded = LimitPaymentTransactionCalculator.IsDepositLimitExceed(toWallet, amount);

            if (isDepositLimitExceeded)
            {
                throw new LimitExceededException();
            }
            if (CurrentAmount < amount + feeAmount)
            {
                throw new NotEnoughAmountException();
            }
            string internalTransferId = Guid.NewGuid().ToString();
            var    deposit            = new DepositInternalTransferPaymentTransaction(toWallet, this, amount, internalTransferId);
            var    withdrawal         = new WithdrawalInternalTransferPaymentTransaction(this, toWallet, amount, internalTransferId);
            FeeInternalTransferPaymentTransaction feePaymentTransaction = new FeeInternalTransferPaymentTransaction(this, toWallet, feeAmount, internalTransferId);

            this.CurrentAmount     -= amount;
            toWallet.CurrentAmount += amount;
            PaymentTransactions.Add(withdrawal);
            toWallet.PaymentTransactions.Add(deposit);
            var internalTransferPaymentTransactions = new InternalTransferPaymentTransactions(deposit, withdrawal, feePaymentTransaction);

            if (internalTransferPaymentTransactions.HasFee())
            {
                this.CurrentAmount -= feeAmount;
                PaymentTransactions.Add(feePaymentTransaction);
            }
            return(internalTransferPaymentTransactions);
        }
Пример #3
0
 private void CleanupWallets(Wallet fromWallet, Wallet toWallet, InternalTransferPaymentTransactions paymentTransactions)
 {
     fromWallet.DeletePaymentTransaction(paymentTransactions.Withdrawal);
     fromWallet.DeletePaymentTransaction(paymentTransactions.Fee);
     toWallet.DeletePaymentTransaction(paymentTransactions.Deposit);
 }
 public InternalTransferPaymentTransactionsDTO(InternalTransferPaymentTransactions internalTransferPaymentTransactions)
 {
     Deposit    = (DepositInternalTransferPaymentTransactionDTO)internalTransferPaymentTransactions.Deposit.ToPaymentTransactionDTO();
     Withdrawal = (WithdrawalInternalTransferPaymentTransactionDTO)internalTransferPaymentTransactions.Withdrawal.ToPaymentTransactionDTO();
     Fee        = (FeeInternalTransferPaymentTransactionDTO)internalTransferPaymentTransactions.Fee.ToPaymentTransactionDTO();
 }