コード例 #1
0
        public async Task <IActionResult> Create(CashTransferCreateBindingModel model)
        {
            var userId = this.GetCurrentUserId();

            if (!this.ModelState.IsValid)
            {
                model.OwnAccounts = await this.GetAllAccountsAsync(userId);

                return(this.View(model));
            }

            var account = await this.bankAccountService.GetByIdAsync <BankAccountDetailsServiceModel>(model.AccountId);

            if (account == null || account.UserId != userId)
            {
                return(this.Forbid());
            }


            if (account.Balance < model.Amount)
            {
                this.ShowErrorMessage(NotificationMessages.InsufficientFunds);
                model.OwnAccounts = await this.GetAllAccountsAsync(userId);

                return(this.View(model));
            }

            var referenceNumber    = ReferenceNumberGenerator.GenerateReferenceNumber();
            var sourceServiceModel = Mapper.Map <CashTransferCreateServiceModel>(model);

            sourceServiceModel.Amount         *= -1;
            sourceServiceModel.ReferenceNumber = referenceNumber;
            sourceServiceModel.Description     = model.Description;

            sourceServiceModel.SenderName    = account.Name;
            sourceServiceModel.RecipientName = account.UserFullName;
            sourceServiceModel.Destination   = sourceServiceModel.RecipientName;
            sourceServiceModel.Source        = sourceServiceModel.SenderName;

            if (!await this.moneyTransferService.CreateCashTransferAsync(sourceServiceModel))
            {
                this.ShowErrorMessage(NotificationMessages.TryAgainLaterError);
                model.OwnAccounts = await this.GetAllAccountsAsync(userId);

                return(this.View(model));
            }

            this.ShowSuccessMessage(NotificationMessages.SuccessfulMoneyTransfer);

            return(this.RedirectToPage("/Account/Login"));
        }
コード例 #2
0
        public async Task <GlobalTransactionResult> TransactAsync(GlobalTransactionDto model)
        {
            if (!ValidationUtil.IsObjectValid(model))
            {
                return(GlobalTransactionResult.GeneralFailure);
            }

            // TODO: Remove this check or the one in PaymentsController
            var account = await this.bankAccountService
                          .GetByUniqueIdAsync <BankAccountConciseServiceModel>(model.DestinationBankAccountUniqueId);

            if (account == null)
            {
                return(GlobalTransactionResult.GeneralFailure);
            }

            if (account.Balance < model.Amount)
            {
                return(GlobalTransactionResult.InsufficientFunds);
            }

            var serviceModel = new TransactionCreateServiceModel
            {
                Amount      = -model.Amount,
                Source      = account.UniqueId,
                Description = model.Description ?? String.Empty,
                AccountId   = account.Id,
                DestinationBankAccountUniqueId = model.DestinationBankAccountUniqueId,
                SenderName      = account.UserFullName,
                RecipientName   = model.RecipientName,
                ReferenceNumber = ReferenceNumberGenerator.GenerateReferenceNumber()
            };

            bool success = await this.transactionService.CreateTransactionAsync(serviceModel);

            return(!success ? GlobalTransactionResult.GeneralFailure : GlobalTransactionResult.Succeeded);
        }
コード例 #3
0
        public async Task <IActionResult> Create(InternalMoneyTransferCreateBindingModel model)
        {
            var userId = await this.userService.GetUserIdByUsernameAsync(this.User.Identity.Name);

            if (!this.ModelState.IsValid)
            {
                model.OwnAccounts = await this.GetAllAccountsAsync(userId);

                return(this.View(model));
            }

            var account = await this.bankAccountService.GetByIdAsync <BankAccountDetailsServiceModel>(model.AccountId);

            if (account == null || account.UserUserName != this.User.Identity.Name)
            {
                return(this.Forbid());
            }

            if (string.Equals(account.UniqueId, model.DestinationBankAccountUniqueId,
                              StringComparison.InvariantCulture))
            {
                this.ShowErrorMessage(NotificationMessages.SameAccountsError);
                model.OwnAccounts = await this.GetAllAccountsAsync(userId);

                return(this.View(model));
            }

            if (account.Balance < model.Amount)
            {
                this.ShowErrorMessage(NotificationMessages.InsufficientFunds);
                model.OwnAccounts = await this.GetAllAccountsAsync(userId);

                return(this.View(model));
            }

            var destinationAccount =
                await this.bankAccountService.GetByUniqueIdAsync <BankAccountConciseServiceModel>(
                    model.DestinationBankAccountUniqueId);

            if (destinationAccount == null)
            {
                this.ShowErrorMessage(NotificationMessages.DestinationBankAccountDoesNotExist);
                model.OwnAccounts = await this.GetAllAccountsAsync(userId);

                return(this.View(model));
            }

            var referenceNumber    = ReferenceNumberGenerator.GenerateReferenceNumber();
            var sourceServiceModel = Mapper.Map <MoneyTransferCreateServiceModel>(model);

            sourceServiceModel.Source          = account.UniqueId;
            sourceServiceModel.Amount         *= -1;
            sourceServiceModel.SenderName      = account.UserFullName;
            sourceServiceModel.RecipientName   = destinationAccount.UserFullName;
            sourceServiceModel.ReferenceNumber = referenceNumber;

            if (!await this.moneyTransferService.CreateMoneyTransferAsync(sourceServiceModel))
            {
                this.ShowErrorMessage(NotificationMessages.TryAgainLaterError);
                model.OwnAccounts = await this.GetAllAccountsAsync(userId);

                return(this.View(model));
            }

            var destinationServiceModel = Mapper.Map <MoneyTransferCreateServiceModel>(model);

            destinationServiceModel.Source          = account.UniqueId;
            destinationServiceModel.AccountId       = destinationAccount.Id;
            destinationServiceModel.SenderName      = account.UserFullName;
            destinationServiceModel.RecipientName   = destinationAccount.UserFullName;
            destinationServiceModel.ReferenceNumber = referenceNumber;

            if (!await this.moneyTransferService.CreateMoneyTransferAsync(destinationServiceModel))
            {
                this.ShowErrorMessage(NotificationMessages.TryAgainLaterError);
                model.OwnAccounts = await this.GetAllAccountsAsync(userId);

                return(this.View(model));
            }

            this.ShowSuccessMessage(NotificationMessages.SuccessfulMoneyTransfer);

            return(this.RedirectToHome());
        }