예제 #1
0
        public void SuccessfullyMapsPaymentProcessingRequestModelToBankingApiRequestDto()
        {
            //Arrange
            var model = new ProcessPaymentRequestDto()
            {
                CardNumber      = "5500000000000004",
                CardHolder      = "Test Account",
                CardType        = CardType.MasterCard,
                ExpirationMonth = DateTime.Now.ToString("MM"),
                ExpirationYear  = DateTime.Now.AddYears(1).ToString("yy"),
                PaymentAmount   = 100.00M,
                Currency        = SupportedCurrencies.GBP,
                Cvv             = "123"
            };

            //Act
            var result = this._mapper.MapProcessPaymentRequestModelToBankDto(model);

            //Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOf <BankProcessPaymentRequestDto>(result);
            Assert.AreEqual(result.CardNumber, model.CardNumber);
            Assert.AreEqual(result.CardHolder, model.CardHolder);
            Assert.AreEqual(result.CardType, model.CardType);
            Assert.AreEqual(result.ExpirationMonth, model.ExpirationMonth);
            Assert.AreEqual(result.ExpirationYear, model.ExpirationYear);
            Assert.AreEqual(result.PaymentAmount, model.PaymentAmount);
            Assert.AreEqual(result.Currency, model.Currency);
            Assert.AreEqual(result.Cvv, model.Cvv);
        }
예제 #2
0
        public BankProcessPaymentRequestDto MapProcessPaymentRequestModelToBankDto(ProcessPaymentRequestDto model)
        {
            if (model == null)
            {
                this._logger.LogError(Resources.Logging_DtoMapperNullInput, (typeof(ProcessPaymentRequestDto).Name));

                throw new HttpException(HttpStatusCode.InternalServerError,
                                        Resources.ErrorCode_MappingError_PaymentApiToBankApi,
                                        Resources.ErrorMessage_MappingError_PaymentApiToBankApi);
            }

            var bankDto = new BankProcessPaymentRequestDto()
            {
                CardNumber      = model.CardNumber,
                CardHolder      = model.CardHolder,
                CardType        = model.CardType,
                ExpirationMonth = model.ExpirationMonth,
                ExpirationYear  = model.ExpirationYear,
                PaymentAmount   = model.PaymentAmount,
                Currency        = model.Currency,
                Cvv             = model.Cvv
            };

            return(bankDto);
        }
예제 #3
0
        public async Task <ProcessPaymentResponse> ProcessPayment(ProcessPaymentRequestDto model)
        {
            var bankRequestDto  = this._dtoMapper.MapProcessPaymentRequestModelToBankDto(model);
            var bankResponseDto = await this._bankingService.ProcessPayment(bankRequestDto);

            var processPaymentResponse = this._dtoMapper.MapBankApiPostResponseToDomainResponse(bankResponseDto);

            return(processPaymentResponse);
        }
예제 #4
0
        public void Validate_WhenExpiryMonthIsNotValid_ShouldError(int expiryMonth)
        {
            var request = new ProcessPaymentRequestDto {
                CardExpiryMonth = expiryMonth
            };

            var result = _validator.TestValidate(request);

            result.ShouldHaveValidationErrorFor(p => p.Amount);
        }
예제 #5
0
        public void Validate_WhenAmountIsLessThanOrEqualsToZero_ShouldError(decimal amount)
        {
            var request = new ProcessPaymentRequestDto {
                Amount = amount
            };

            var result = _validator.TestValidate(request);

            result.ShouldHaveValidationErrorFor(p => p.Amount);
        }
예제 #6
0
        public void Validate_WhenCvvIsInvalid_ShouldError(int cvv)
        {
            var request = new ProcessPaymentRequestDto {
                Cvv = cvv
            };

            var result = _validator.TestValidate(request);

            result.ShouldHaveValidationErrorFor(p => p.Cvv);
        }
예제 #7
0
        public void ModelWithoutRequiredPropertiesShouldFailValidation(string propertyName)
        {
            //Arrange
            var model = new ProcessPaymentRequestDto();

            //Act
            var results = ModelValidator.ValidateModel(model);

            //Assert
            Assert.IsTrue(results.Any(result => result.MemberNames.Contains(propertyName) && result.ErrorMessage.Contains("required")));
        }
예제 #8
0
        public async Task ProcessPaymentReturnsProcessPaymentResponse()
        {
            //Arrange
            var model = new ProcessPaymentRequestDto()
            {
                CardNumber      = "5500000000000004",
                CardHolder      = "Test Account",
                CardType        = CardType.MasterCard,
                ExpirationMonth = DateTime.Now.ToString("MM"),
                ExpirationYear  = DateTime.Now.AddYears(1).ToString("yy"),
                PaymentAmount   = 100.00M,
                Currency        = SupportedCurrencies.GBP,
                Cvv             = "123"
            };

            var bankingRequestDto = new BankProcessPaymentRequestDto()
            {
                CardNumber      = "5500000000000004",
                CardHolder      = "Test Account",
                CardType        = CardType.MasterCard,
                ExpirationMonth = DateTime.Now.ToString("MM"),
                ExpirationYear  = DateTime.Now.AddYears(1).ToString("yy"),
                PaymentAmount   = 100.00M,
                Currency        = SupportedCurrencies.GBP,
                Cvv             = "123"
            };

            var transactionId      = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
            var bankingResponseDto = new BankProcessPaymentResponseDto()
            {
                TransactionId = transactionId,
                PaymentStatus = PaymentStatus.Success
            };

            var guid = new Guid(transactionId);
            var processPaymentResponse = new ProcessPaymentResponse()
            {
                TransactionId = guid,
                PaymentStatus = PaymentStatus.Success
            };

            this._dtoMapper.Setup(x => x.MapProcessPaymentRequestModelToBankDto(It.IsAny <ProcessPaymentRequestDto>())).Returns(bankingRequestDto);
            this._bankingService.Setup(x => x.ProcessPayment(It.IsAny <BankProcessPaymentRequestDto>())).ReturnsAsync(bankingResponseDto);
            this._dtoMapper.Setup(x => x.MapBankApiPostResponseToDomainResponse(It.IsAny <BankProcessPaymentResponseDto>())).Returns(processPaymentResponse);

            //Act
            var result = await this._paymentProcessingService.ProcessPayment(model);

            //Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOf <ProcessPaymentResponse>(result);
            Assert.AreEqual(guid, result.TransactionId);
            Assert.AreEqual(PaymentStatus.Success, result.PaymentStatus);
        }
예제 #9
0
        public void Validate_WhenValuesAreNull_ShouldError()
        {
            var request = new ProcessPaymentRequestDto();

            var result = _validator.TestValidate(request);

            result.ShouldHaveValidationErrorFor(p => p.Amount);
            result.ShouldHaveValidationErrorFor(p => p.Currency);
            result.ShouldHaveValidationErrorFor(p => p.Cvv);
            result.ShouldHaveValidationErrorFor(p => p.CardNumber);
            result.ShouldHaveValidationErrorFor(p => p.CardExpiryMonth);
            result.ShouldHaveValidationErrorFor(p => p.CardExpiryYear);
        }
예제 #10
0
        public void ValidateCurrency(int currency, bool shouldFailValidation)
        {
            //Arrange
            var model = new ProcessPaymentRequestDto()
            {
                Currency = (SupportedCurrencies)currency
            };

            //Act
            var results = ModelValidator.ValidateModel(model);

            //Assert
            Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("Currency")));
        }
예제 #11
0
        //The tests for the custom validation logic which require the current date are in the CustomAttributes folder.
        public void ValidatePaymentAmount(decimal paymentAmount, bool shouldFailValidation)
        {
            //Arrange
            var model = new ProcessPaymentRequestDto()
            {
                PaymentAmount = paymentAmount
            };

            //Act
            var results = ModelValidator.ValidateModel(model);

            //Assert
            Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("PaymentAmount")));
        }
예제 #12
0
        //The tests for the custom validation logic which require the current date are in the CustomAttributes folder.
        public void ValidateExpirationYearFormat(string expirationYear, bool shouldFailValidation)
        {
            //Arrange
            var model = new ProcessPaymentRequestDto()
            {
                ExpirationYear = expirationYear
            };

            //Act
            var results = ModelValidator.ValidateModel(model);

            //Assert
            Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("ExpirationYear")));
        }
예제 #13
0
        public void ValidateCvv(string cvv, bool shouldFailValidation)
        {
            //Arrange
            var model = new ProcessPaymentRequestDto()
            {
                Cvv = cvv
            };

            //Act
            var results = ModelValidator.ValidateModel(model);

            //Assert
            Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("Cvv")));
        }
예제 #14
0
        public void ValidateCardType(int cardType, bool shouldFailValidation)
        {
            //Arrange
            var model = new ProcessPaymentRequestDto()
            {
                CardType = (CardType)cardType
            };

            //Act
            var results = ModelValidator.ValidateModel(model);

            //Assert
            Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("CardType")));
        }
예제 #15
0
        public void ValidateExpirationMonth(string expirationMonth, bool shouldFailValidation)
        {
            //Arrange
            var model = new ProcessPaymentRequestDto()
            {
                ExpirationMonth = expirationMonth,
                ExpirationYear  = DateTime.Now.AddYears(1).ToString("yy") //The tests for the custom validation logic are in the CustomAttributes folder.
            };

            //Act
            var results = ModelValidator.ValidateModel(model);

            //Assert
            Assert.AreEqual(shouldFailValidation, results.Any(result => result.MemberNames.Contains("ExpirationMonth")));
        }
        public void AttributeShouldOnlyTreatValuesOneToTwelveAsValidForFutureYears(string inputMonth, bool expectedOutput)
        {
            //Arrange
            var expirationYear = DateTime.Now.AddYears(1).ToString("yy");
            var model          = new ProcessPaymentRequestDto()
            {
                ExpirationYear = expirationYear
            };
            var validationContext2 = new ValidationContext(model);

            //Act
            var result = IsValid(inputMonth, validationContext2);

            //Assert
            Assert.AreEqual(expectedOutput, string.IsNullOrEmpty(result?.ErrorMessage));
        }
예제 #17
0
        public void Validate_WhenValuesAreValid_ShouldNotError()
        {
            var request = new ProcessPaymentRequestDto
            {
                Currency        = Currency.USD,
                CardNumber      = "1111222233334444",
                CardExpiryMonth = 12,
                CardExpiryYear  = 2020,
                Cvv             = 123,
                Amount          = 12
            };

            var result = _validator.TestValidate(request);

            result.ShouldNotHaveValidationErrorFor(p => p.Amount);
            result.ShouldNotHaveValidationErrorFor(p => p.Currency);
            result.ShouldNotHaveValidationErrorFor(p => p.Cvv);
            result.ShouldNotHaveValidationErrorFor(p => p.CardNumber);
            result.ShouldNotHaveValidationErrorFor(p => p.CardExpiryMonth);
            result.ShouldNotHaveValidationErrorFor(p => p.CardExpiryYear);
        }
        public void AttributeShouldOnlyTreatCurrentOrFurtureMonthsAsValidForFutureYears(string inputMonth)
        {
            //Arrange
            var expirationYear = DateTime.Now.ToString("yy");
            var model          = new ProcessPaymentRequestDto()
            {
                ExpirationYear = expirationYear
            };
            var validationContext2 = new ValidationContext(model);

            int inputMonthAsInt;

            int.TryParse(inputMonth, out inputMonthAsInt);
            var expectedOutput = inputMonthAsInt >= DateTime.Now.Month && inputMonthAsInt <= 12;

            //Act
            var result = IsValid(inputMonth, validationContext2);

            //Assert
            Assert.AreEqual(expectedOutput, string.IsNullOrEmpty(result?.ErrorMessage));
        }
예제 #19
0
        public async Task <ActionResult <ProcessPaymentResponseDto> > ProcessPayment(
            [FromBody] ProcessPaymentRequestDto paymentRequestDto)
        {
            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            var paymentResult = await _paymentProcessor.ProcessAsync(new PaymentRequest
            {
                Amount          = paymentRequestDto.Amount.Value,
                Currency        = paymentRequestDto.Currency.Value,
                CardCvv         = paymentRequestDto.Cvv.Value,
                CardNumber      = paymentRequestDto.CardNumber,
                CardExpiryMonth = paymentRequestDto.CardExpiryMonth.Value,
                CardExpiryYear  = paymentRequestDto.CardExpiryYear.Value,
                User            = user
            });

            return(Ok(new ProcessPaymentResponseDto
            {
                PaymentId = paymentResult.Payment.Id,
                Success = paymentResult.Payment.Success
            }));
        }
        public async Task ProcessPaymentShouldReturn200()
        {
            //Arrange
            var model           = new ProcessPaymentRequestDto();
            var guid            = new Guid();
            var serviceResponse = new ProcessPaymentResponse()
            {
                TransactionId = guid,
                PaymentStatus = PaymentStatus.Success
            };

            this._paymentProcessingService.Setup(x => x.ProcessPayment(It.IsAny <ProcessPaymentRequestDto>()))
            .ReturnsAsync(serviceResponse);

            //Act
            var result = await this._controller.ProcessPayment(model);

            //Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOf <OkObjectResult>(result);

            var resultAsOkObject = result as OkObjectResult;

            Assert.IsTrue(resultAsOkObject.StatusCode == 200);
            Assert.IsNotNull(resultAsOkObject.Value);
            Assert.IsInstanceOf <ResponseBaseDto>(resultAsOkObject.Value);

            var resultValue = resultAsOkObject.Value as ResponseBaseDto;

            Assert.IsTrue(resultValue.StatusCode == HttpStatusCode.OK);
            Assert.IsNotNull(resultValue.Data);
            Assert.IsInstanceOf <ProcessPaymentResponse>(resultValue.Data);

            var resultData = resultValue.Data as ProcessPaymentResponse;

            Assert.AreEqual(guid, resultData.TransactionId);
            Assert.AreEqual(PaymentStatus.Success, resultData.PaymentStatus);
        }