private void SingleErrorTest(ProcessPaymentDto payment, string expectedMessage)
        {
            //Arrange
            var context = new ValidationContext(payment);
            var results = new List <ValidationResult>();

            //Act
            Validator.TryValidateObject(payment, context, results, true);

            //Assert
            Assert.Single(results);
            Assert.Equal(expectedMessage, results.First().ErrorMessage);
        }
Esempio n. 2
0
        public async Task <ActionResult <PaymentDetailsDto> > ProcessPayment([FromBody] ProcessPaymentDto processPaymentDto)
        {
            if (processPaymentDto == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!IsExpirationDateActual(processPaymentDto.CardExpirationMonth, processPaymentDto.CardExpirationYear))
            {
                return(BadRequest("Card is already expired"));
            }

            var bankRequest  = _mapper.Map <BankPaymentRequestDto>(processPaymentDto);
            var bankResponse = await _bankService.ProcessPaymentRequest(bankRequest);

            if (bankResponse == null)
            {
                return(StatusCode(500, "Bank processing returned empty response"));
            }

            var payment = _mapper.Map <Payment>(processPaymentDto);

            UpdatePaymentByBankResponse(payment, bankResponse);

            var success = await _paymentService.CreatePayment(payment);

            if (success)
            {
                return(Created(
                           Url.Link("GetPaymentDetails", new { id = payment.Id }),
                           _mapper.Map <PaymentDetailsDto>(payment)));
            }

            return(StatusCode(500, "Payment creation failed"));
        }