Ejemplo n.º 1
0
        public void Validate_RequestIsValid()
        {
            // Arrange

            // Act
            var result = _validator.Validate(_payment, out var reasons);

            // Assert
            Assert.IsTrue(result, "Payment should be valid");
            Assert.IsTrue(reasons.Count == 0, "A reason should be provided");
        }
Ejemplo n.º 2
0
        public void CanValidate()
        {
            // Act
            _sut.Validate(new PaymentRequest
            {
                Amount                = 100,
                CardNumber            = RandomHelper.GetRandomString(16),
                CardVerificationValue = RandomHelper.GetRandomString(3),
                CurrencyCode          = "GBP",
                ExpiryDate            = DateTime.Now.AddYears(2)
            });

            // Assert
            Assert.True(true, "This test is deemed a pass if no exception is thrown.");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles a payment request
        /// </summary>
        /// <returns>Returns the id of the created request</returns>
        public async Task <string> HandleRequest(IPaymentRequest request)
        {
            _logger?.LogTrace($"Processing request '{request.Id}'");

            // Persist request to local repository
            var id = _repository.Create(request);

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            // Validate request
            if (!_validator.Validate(request, out var reasons))
            {
                throw new InvalidDataException(reasons);
            }

            // Post to bank
            await _bankService.Post(request);

            // Mark as successfully processed
            _repository.UpdateResult(request.Id, true);

            return(id);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Create([FromBody] CreatePaymentApiModel createPaymentApiModel)
        {
            var paymentRequest = _paymentMapper.Map(createPaymentApiModel);

            var validationResult = _paymentRequestValidator.Validate(paymentRequest);

            if (validationResult.Error != ValidationErrors.none)
            {
                return(BadRequest(new ErrorResponse {
                    ErrorCode = _errorToApiErrorCodeMapper.Map(validationResult.Error),
                    ErrorMessage = validationResult.ErrorMessage
                }));
            }

            var payment = await _paymentManager.AddPayment(paymentRequest);

            return(Ok(_paymentMapper.Map(payment)));
        }
Ejemplo n.º 5
0
        public PaymentResult MakeRequest(PaymentRequest request)
        {
            if (request == null)
            {
                throw new ArgumentException($"{nameof(PaymentRequest)} was null.");
            }

            Logger.Info($"{nameof(MakeRequest)} received for {request.CardNumber.Obfuscate(4)}.");

            _requestValidator.Validate(request);

            var result = _bankService.MakePayment(request.ToBankApi());

            _paymentRepository.Store(new Payment(
                                         result.Guid,
                                         request.CardNumber.Obfuscate(4),
                                         request.Amount,
                                         request.ExpiryDate,
                                         request.CurrencyCode,
                                         result.BankResult.ToPaymentStatusCode(),
                                         DateTime.Now));

            return(result.ToApiModel());
        }