コード例 #1
0
        public async Task Receive()
        {
            // Arrange
            var obj = new SbpEventDto();

            // Act
            var controller = new SistemaBrasileiroDePagamentosController(_logger.Object, _bankService.Object);
            var result     = await controller.Receive(obj);

            // Assert
            Assert.IsType <OkObjectResult>(result);
        }
コード例 #2
0
        private async Task ProcessTransfer(BankAccountEntity account, SbpEventDto sbpEvent)
        {
            account.BalanceInBrl += sbpEvent.Amount;

            await _bankAccountRepository.Update(account);

            // var bankClientAccountExchange = _mapper.Map<SbpEventDto, BankAccountExchangeEntity>(sbpEvent);
            // bankClientAccountExchange.BankAccountId = account.Id;
            //
            // await _bankAccountExchangeRepository.Insert(bankClientAccountExchange);

            // _bankAccountExchangeRepository.Save();
            _bankAccountRepository.Save();
        }
コード例 #3
0
        public async Task <ActionResult> Receive([Required] SbpEventDto sbpEvent)
        {
            try
            {
                await _bankService.ReceiveEvent(sbpEvent);

                return(Ok("Event received with success"));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "The server was unable to process SBP event. If problem persists, contact an administrator");
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  "An error occurred and the server could not receive the SBP event. If the problem persists, contact an administrator"));
            }
        }
コード例 #4
0
        public async Task ReceiveTransfer()
        {
            // Arrange
            var transfer = new SbpEventDto()
            {
                Target = new SbpEventBankDto()
                {
                    Bank    = _appSettings.ToroBankCode,
                    Branch  = _appSettings.ToroBranch,
                    Account = ""
                },
                Origin = new SbpEventBankDto()
                {
                    Cpf = ""
                }
            };
            var bankClientList = new List <BankClientEntity>()
            {
                new BankClientEntity()
            };
            var bankClientAccountList = new List <BankClientAccountEntity>()
            {
                new BankClientAccountEntity()
            };
            var bankAccountList = new List <BankAccountEntity>()
            {
                new BankAccountEntity()
            };

            _bankClientRepository.Setup(x => x.Select(It.IsAny <Expression <Func <BankClientEntity, bool> > >()))
            .ReturnsAsync(bankClientList);
            _bankClientAccountRepository.Setup(x => x.Select(It.IsAny <Expression <Func <BankClientAccountEntity, bool> > >()))
            .ReturnsAsync(bankClientAccountList);
            _bankAccountRepository.Setup(x => x.Select(It.IsAny <Expression <Func <BankAccountEntity, bool> > >()))
            .ReturnsAsync(bankAccountList);
            _mapper.Setup(x => x.Map <SbpEventDto, BankAccountExchangeEntity>(transfer))
            .Returns(new BankAccountExchangeEntity());

            // Act
            var service = new BankService(_logger.Object, _appSettings, _mapper.Object, _bankClientRepository.Object,
                                          _bankClientAccountRepository.Object, _bankAccountRepository.Object, _bankAccountExchangeRepository.Object,
                                          _bankAccountStockRepository.Object, _userBankClientRepository.Object);
            var result = await Record.ExceptionAsync(() => service.ReceiveEvent(transfer));

            // Assert
            Assert.NotNull(result);
        }
コード例 #5
0
        public async Task ReceiveEvent(SbpEventDto sbpEventDto)
        {
            try
            {
                if (sbpEventDto.Target.Bank != _appSettings.ToroBankCode)
                {
                    _logger.LogInformation("SBP event not targeted at Toro {@SbpEventDto}", sbpEventDto);
                    return;
                }

                if (sbpEventDto.Target.Branch != _appSettings.ToroBranch)
                {
                    _logger.LogWarning("SBP event not targeted at Toro's branch: {@SbpEventDto}", sbpEventDto);
                    return;
                }

                var client = (await _bankClientRepository.Select(a => a.Document == sbpEventDto.Origin.Cpf)).FirstOrDefault();

                if (client == null)
                {
                    _logger.LogWarning("Client does not exist in database: {@Transfer}", sbpEventDto);
                    return;
                }

                var clientAccount = (await _bankClientAccountRepository.Select(a => a.BankClientId == client.Id)).FirstOrDefault();

                if (clientAccount == null)
                {
                    _logger.LogWarning("Client account does not exist in database: {@Transfer}", sbpEventDto);
                    return;
                }

                var bankAccount = (await _bankAccountRepository.Select(a =>
                                                                       a.Id == clientAccount.BankAccountId && a.AccountNumber == sbpEventDto.Target.Account &&
                                                                       a.Branch == sbpEventDto.Target.Branch)).FirstOrDefault();

                if (bankAccount == null)
                {
                    _logger.LogWarning("SBP event does not match any client account number and/or branch: {@Transfer}", sbpEventDto);
                    return;
                }

                if (!Enum.TryParse(sbpEventDto.Event, true, out FlagSbpEvent flagSbpEvent))
                {
                    throw new NotImplementedException($"Sbp event not implemented: {sbpEventDto.Event}");
                }

                switch (flagSbpEvent)
                {
                case FlagSbpEvent.Transfer:
                    await ProcessTransfer(bankAccount, sbpEventDto); break;

                default:
                    throw new ArgumentOutOfRangeException($"Invalid SBP event: {flagSbpEvent}");
                }

                _logger.LogInformation("Bank transfer successful: {@Transfer}", sbpEventDto);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failure processing bank transfer: {@Transfer}", sbpEventDto);
                throw;
            }
        }