public void Validate_ShouldReturnsExpectedErrorMessage_WhenPaymentRequestIsNotValid( PaymentRequest paymentRequest, List <string> expectedErrorMessages) { // Arrange // Act var validationResult = _subject.Validate(paymentRequest); // Assert Assert.False(validationResult.IsValid); Assert.Equal(expectedErrorMessages, validationResult.Errors.Select(x => x.ErrorMessage).ToList()); }
public async Task <ObjectResult> Post([FromBody] Payment payment) { PaymentValidator paymaentValidator = new PaymentValidator(); ValidationResult valitaionResult = paymaentValidator.Validate(payment); if (!valitaionResult.IsValid) { List <string> errors = new List <string>(); foreach (ValidationFailure failure in valitaionResult.Errors) { errors.Add(failure.ErrorMessage); } return(BadRequest(String.Join("\n", errors))); } var result = await _paymentManager.PostPayment(new PaymentViewPost(payment)); if (payment != null) { //if you want to return the created object with status code 201 use Created("/" + result.Id, result); return(Ok(result)); } else { return(BadRequest("Insufficient funds")); } }
public Result <Payment> CreatePayment(PaymentDTO paymentDTO, Order order) { Payment payment = new Payment(paymentDTO.OrderId); payment.Order = order; //string failureMessage = payment.IsValid(); PaymentValidator paymentValidator = new PaymentValidator(); var validationResult = paymentValidator.Validate(payment); //if (failureMessage == string.Empty) if (validationResult.IsValid) { try { PaymentsDbSet.Add(payment); _context.SaveChanges(); return(Result <Payment> .Ok(payment)); } catch (Exception ex) { Console.WriteLine(ex); return(Result.Fail <Payment>(ex.Message + " " + ex.InnerException)); } } else { return(Result.Fail <Payment>(string.Join(" ", validationResult.Errors))); } //return Result.Fail<Payment>(failureMessage); }
public void IsValidAmount(decimal amount, bool status) { payment.BSB = "062-223"; payment.AccountNumber = "123456"; payment.AccountName = "xcdh dmncs"; payment.Amount = amount; PaymentValidator validator = new PaymentValidator(); ValidationResult results = validator.Validate(payment); Assert.AreEqual(results.IsValid, status); }
public void IsValidAccountName(string accountName, bool status) { payment.BSB = "062-223"; payment.AccountNumber = "123456"; payment.AccountName = accountName; payment.Amount = 636; PaymentValidator validator = new PaymentValidator(); ValidationResult results = validator.Validate(payment); Assert.AreEqual(results.IsValid, status); }
public void IsValidBSB(string bsb, bool status) { payment.BSB = bsb; payment.AccountNumber = "123456"; payment.AccountName = "Mr shdsmj"; payment.Amount = 636; PaymentValidator validator = new PaymentValidator(); ValidationResult results = validator.Validate(payment); Assert.AreEqual(results.IsValid, status); }
public void TestValidate_InvalidCardNumber() { PaymentDTO paymentDTO = GetPaymentDTO(); paymentDTO.CreditCardNumber = null; var result = paymentValidator.Validate(paymentDTO); result.Should().BeFalse(); BaseParameters.ErrorBag.OperationOutcomeCode.Should().Equals("Error"); }
public IReadOnlyDictionary <string, string> Validate() { var validator = new PaymentValidator(); var results = validator.Validate(this); var domainErrors = new Dictionary <string, string>(); foreach (var result in results.Errors) { domainErrors.Add(result.PropertyName, result.ErrorMessage); } return(domainErrors); }
public void Amount_Is_Greater_Than_Zero_Is_Valid() { // Arrange var validator = new PaymentValidator(); var payment = new Payment { Amount = 0.01M }; // Act var result = validator.Validate(payment); // Assert Assert.False(result.Errors.Any(e => e.ErrorMessage == "Amount must be greater than 0.00")); }
public void AccountId_Is_Greater_Than_Zero_Is_Valid() { // Arrange var validator = new PaymentValidator(); var payment = new Payment { AccountId = 1 }; // Act var result = validator.Validate(payment); // Assert Assert.False(result.Errors.Any(e => e.ErrorMessage == "AccountId must be a interger greater than 0")); }
public void Amount_Is_Zero_Generates_Error() { // Arrange var validator = new PaymentValidator(); var payment = new Payment { Amount = 0.00M }; // Act var result = validator.Validate(payment); // Assert Assert.True(result.Errors.Any(e => e.ErrorMessage == "Amount must be greater than 0.00")); }
public void Cvv_Is_4_Digits_Is_Valid() { // Arrange var validator = new PaymentValidator(); var payment = new Payment { Cvv = "1234" }; // Act var result = validator.Validate(payment); // Assert Assert.False(result.Errors.Any(e => e.ErrorMessage == "CVV is invalid")); }
public void AccountId_Is_Zero_Generates_Error() { // Arrange var validator = new PaymentValidator(); var payment = new Payment { AccountId = 0 }; // Act var result = validator.Validate(payment); // Assert Assert.True(result.Errors.Any(e => e.ErrorMessage == "AccountId must be a interger greater than 0")); }
public void OrderId_Exceeds_50_Characters_Generates_Error() { // Arrange var validator = new PaymentValidator(); var payment = new Payment { OrderId = "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk" }; // Act var result = validator.Validate(payment); // Assert Assert.True(result.Errors.Any(e => e.ErrorMessage == "OrderId cannot exceed 50 characters")); }
public void Cvv_Has_5_Digits_Generates_Error() { // Arrange var validator = new PaymentValidator(); var payment = new Payment { Cvv = "12345" }; // Act var result = validator.Validate(payment); // Assert Assert.True(result.Errors.Any(e => e.ErrorMessage == "CVV is invalid")); }
public ActionResult Index(PaymentViewModel payment) { var validator = new PaymentValidator(); var result = validator.Validate(payment); if (result.IsValid) { } else { foreach (ValidationFailure failer in result.Errors) { ModelState.AddModelError(failer.PropertyName, failer.ErrorMessage); } } return(View(payment)); }
public void PaymentValidation(string label, Payment payment, bool expected) { var validationResult = _paymentValidator.Validate(payment); validationResult.IsValid.Should().Be(expected); _output.WriteLine($@" {label} {JsonConvert.SerializeObject(payment, Formatting.Indented)} Errors: "); foreach (var error in validationResult.Errors) { _output.WriteLine($" {error.ErrorMessage}"); } if (!validationResult.Errors.Any()) { _output.WriteLine(" None"); } }
public void Description_Exceeds_2048_Characters_Generates_Error() { // Arrange var description = new StringBuilder(); for (int i = 0; i < 2049; i++) { description.Append("k"); } var validator = new PaymentValidator(); var payment = new Payment { Description = description.ToString() }; // Act var result = validator.Validate(payment); // Assert Assert.True(result.Errors.Any(e => e.ErrorMessage == "Description cannot exceed 2048 characters")); }
public async Task <IActionResult> Post([FromBody] PaymentModel payment) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var validationStatus = PaymentValidator.Validate(payment); if (!validationStatus.IsValid) { return(BadRequest(validationStatus.Message)); } var createPaymentResponse = await PaymentService.CreatePaymentAsync(new CreatePaymentDto() { CreditCardNumber = payment.CreditCardNumber, CardHolder = payment.CardHolder, ExpirationDate = payment.ExpirationDate, SecurityCode = payment.SecurityCode, Amount = payment.Amount }); if (createPaymentResponse?.Id > 0) { return(Ok(createPaymentResponse)); } else { Logger.LogError("Transaction could not be completed"); return(BadRequest("Transaction could not be completed")); } } catch (Exception ex) { Logger.LogError(ex, ex.ToString()); return(BadRequest(ex)); } }
public IActionResult DoPayment(PaymentRequest request) { try { var validator = new PaymentValidator(); var validationResult = validator.Validate(request); if (validationResult.IsValid) { _logger.LogInformation("Do payment process started."); var paymentResponse = _paymentService.DoPayment(request); if (!paymentResponse.Status) { return(ResponseHelper.Instance.BadRequestResponseCreator(new List <string>() { paymentResponse.Message })); } return(ResponseHelper.Instance.OkResponseCreator(paymentResponse)); } else { return(ResponseHelper.Instance.BadRequestResponseCreator(validationResult.Errors.Select(p => p.ErrorMessage).ToList())); } } catch (BankServiceException e) { _logger.LogError(e.ToString()); return(ResponseHelper.Instance.InternalServerErrorResponseCreator(e.Message)); } catch (Exception e) { _logger.LogError(e.ToString()); return(ResponseHelper.Instance.BadRequestResponseCreator(new List <string>() { e.Message })); } }
public ActionResult Index(PaymentViewModel transferdetails) { Payment payment = new Payment(); payment.BSB = transferdetails.BSB; payment.AccountNumber = transferdetails.AccountNumber; payment.AccountName = transferdetails.AccountName; payment.Reference = transferdetails.Reference; payment.Amount = transferdetails.Amount; PaymentValidator validator = new PaymentValidator(); ValidationResult results = validator.Validate(payment); if (results.Errors.Count == 0) { ViewBag.SuccessMessage = "Payment Successful!"; } if (results.Errors.Count > 0) { foreach (var error in results.Errors) { ModelState.AddModelError(String.Empty, error.ErrorMessage); } } log.Info("payment details Start:"); log.Info("BSB :" + payment.BSB); log.Info("Account Number :" + payment.AccountNumber); log.Info("Account Name :" + payment.AccountName); log.Info("Account Reference :" + payment.Reference); log.Info("Payment Amount :" + payment.Amount); log.Info("payment details End:"); return(View()); }
public async Task <Payment> Process(Payment payment) { var dbPayment = await _paymentRepository.GetByIdempotencyId(payment.IdempotencyId); if (dbPayment != null) { _logger.LogWarning($"IDEMPOTENT :: An identical request was already made for this Payment (Id - {dbPayment.Id}"); return(dbPayment); } var dbMerchant = await _merchantRepository.Get(payment.MerchantId); if (dbMerchant == null) { throw new BusinessException( BusinessExceptionCodes.MerchantHasNoContract, "This Merchant has no contract with us, therefore we cannot accept payment requests."); } var paymentValidator = new PaymentValidator(); var validationResult = paymentValidator.Validate(payment); HandleErrors.HandleValidatorResult(validationResult); var transactionResults = await _bankApiClient.PostTransaction(payment); payment.Id = Guid.NewGuid(); payment.Status = transactionResults.Status; payment.BankTransactionId = transactionResults.BankTransactionId; payment.HasError = payment.Status != PaymentStatus.Approved; payment.ErrorCode = transactionResults.ErrorCode; payment.ErrorMessage = transactionResults.Message; payment.CreatedOn = DateTime.UtcNow; payment.UpdatedOn = DateTime.UtcNow; return(await _paymentRepository.Add(payment)); }