示例#1
0
        public async Task <GlobalTransferResult> TransferMoneyAsync(GlobalTransferDto model)
        {
            if (!ValidationUtil.IsObjectValid(model))
            {
                return(GlobalTransferResult.GeneralFailure);
            }

            var account = await this.bankAccountService
                          .GetByIdAsync <BankAccountConciseServiceModel>(model.SourceAccountId);

            // check if account exists and recipient name is accurate
            if (account == null)
            {
                return(GlobalTransferResult.GeneralFailure);
            }

            // verify there is enough money in the account
            if (account.Balance < model.Amount)
            {
                return(GlobalTransferResult.InsufficientFunds);
            }

            // contact the CentralApi to execute the transfer
            var submitDto = this.mapper.Map <CentralApiSubmitTransferDto>(model);

            submitDto.SenderName            = account.UserFullName;
            submitDto.SenderAccountUniqueId = account.UniqueId;

            bool remoteSuccess = await this.ContactCentralApiAsync(submitDto);

            if (!remoteSuccess)
            {
                return(GlobalTransferResult.GeneralFailure);
            }

            // remove money from source account
            var serviceModel = new MoneyTransferCreateServiceModel
            {
                Amount      = -model.Amount,
                Source      = account.UniqueId,
                Description = model.Description,
                AccountId   = account.Id,
                DestinationBankAccountUniqueId = model.DestinationBankAccountUniqueId,
                SenderName      = account.UserFullName,
                RecipientName   = model.RecipientName,
                ReferenceNumber = submitDto.ReferenceNumber
            };

            bool success = await this.moneyTransferService.CreateMoneyTransferAsync(serviceModel);

            return(!success ? GlobalTransferResult.GeneralFailure : GlobalTransferResult.Succeeded);
        }
        public async Task <IActionResult> Post([FromBody] string data)
        {
            var model = JsonConvert.DeserializeObject <ReceiveMoneyTransferModel>(data);

            if (!this.TryValidateModel(model))
            {
                return(this.BadRequest());
            }

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

            if (account == null || !string.Equals(account.UserFullName, model.RecipientName,
                                                  StringComparison.InvariantCulture))
            {
                return(this.BadRequest());
            }

            var serviceModel = new MoneyTransferCreateServiceModel
            {
                AccountId   = account.Id,
                Amount      = model.Amount,
                Description = model.Description,
                DestinationBankAccountUniqueId = model.DestinationBankAccountUniqueId,
                Source          = model.SenderAccountUniqueId,
                SenderName      = model.SenderName,
                RecipientName   = model.RecipientName,
                ReferenceNumber = model.ReferenceNumber
            };

            var isSuccessful = await this.moneyTransferService.CreateMoneyTransferAsync(serviceModel);

            if (!isSuccessful)
            {
                return(this.NoContent());
            }

            return(this.Ok());
        }
        private static MoneyTransferCreateServiceModel PrepareCreateModel(
            string description             = SampleDescription,
            decimal amount                 = SampleAmount,
            string accountId               = SampleBankAccountId,
            string destinationBankUniqueId = SampleDestination,
            string source          = SampleBankAccountId,
            string senderName      = SampleSenderName,
            string recipientName   = SampleRecipientName,
            string referenceNumber = SampleReferenceNumber)
        {
            var model = new MoneyTransferCreateServiceModel
            {
                Description = description,
                Amount      = amount,
                AccountId   = accountId,
                DestinationBankAccountUniqueId = destinationBankUniqueId,
                Source          = source,
                SenderName      = senderName,
                RecipientName   = recipientName,
                ReferenceNumber = SampleReferenceNumber,
            };

            return(model);
        }