public void Validate_RequestIsNull_ThrowsException()
 {
     ExceptionAssert.ThrowsArgumentNullException("request", () =>
     {
         PaymentRequestValidator.Validate(null);
     });
 }
Esempio n. 2
0
        public async Task ValidRequest_ReturnsValidRequest()
        {
            var request = new PaymentRequest
            {
                CardNumber      = "4111111111111111",
                ExpireDateYear  = 2021,
                ExpireDateMonth = 10,
                CVV             = "123",
                Amount          = (decimal)10.10,
                Currency        = "EUR"
            };

            var result = _paymentRequestValidator.Validate(request);

            Assert.IsTrue(result.IsValid);
        }
Esempio n. 3
0
            public void ValidRequest_ShouldReturnTrue()
            {
                //arrange

                //act
                var validationResult = _sut.Validate(_request);

                //assert
                validationResult.IsValid.Should().BeTrue();
            }
        public void Validate_TransactionReferenceIsThanMaxLength_NoException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "12345678901234567890123456789012"
            };

            PaymentRequestValidator.Validate(request);
        }
Esempio n. 5
0
        private IPaymentPostData CreatePostData(IPaymentRequest request)
        {
            PaymentRequestValidator.Validate(request);

            string data = DataString.Create(Configuration, request);
            string seal = Seal.Create(data, Configuration.SecretKey);

            return(new PaymentPostData()
            {
                Data = data,
                Seal = seal,
            });
        }
        public void Validate_CurrencyCodeIsNotSet_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "1234",
            };

            ExceptionAssert.Throws <InvalidOperationException>("The CurrencyCode should be set.", () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_ReturnUrlIsNull_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = null,
                TransactionReference = "1234"
            };

            ExceptionAssert.ThrowsNullValidationException(nameof(request.ReturnUrl), () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_TransactionReferenceIsWhitespace_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "    "
            };

            ExceptionAssert.ThrowsWhitespaceValidationException(nameof(request.TransactionReference), () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_ReturnUrlContainsSeparator_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://github.com/?a=|"),
                TransactionReference = "1234"
            };

            ExceptionAssert.ThrowsSeparatorValidationException(nameof(request.ReturnUrl), () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_ReturnUrlIsLongerThanMaxLength_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com/" + new string('x', 490)),
                TransactionReference = "1234",
            };

            ExceptionAssert.ThrowsLengthValidationException(nameof(request.ReturnUrl), 512, () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_AmountIsZero_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 0,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "1234"
            };

            ExceptionAssert.ThrowsHigherThanValidationException(nameof(request.Amount), 0, () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_TransactionReferenceIsLongerThanMaxLength_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "123456789012345678901234567890123"
            };

            ExceptionAssert.ThrowsLengthValidationException(nameof(request.TransactionReference), 32, () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_TransactionReferenceHasNonAlphanumericCharacter_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "&"
            };

            ExceptionAssert.ThrowsAlphanumericValidationException(nameof(request.TransactionReference), () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_AmountInYenIsLessThanIntMinValue_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = ((decimal)int.MaxValue) + 1,
                CurrencyCode         = CurrencyCode.JapaneseYen,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "1234"
            };

            ExceptionAssert.ThrowsOverflowException(() =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_AmountIsMoreThanMaxMaxValue_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = (int.MinValue / 100.0m) - 1,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "1234"
            };

            ExceptionAssert.ThrowsOverflowException(() =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_ExpirationDateIsNotUTC_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "1234",
                ExpirationDate       = DateTime.Now
            };

            ExceptionAssert.Throws <InvalidOperationException>("The value for ExpirationDate should use UTC.", () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_CaptureDayIsMoreThan99_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "1234",
                CaptureDay           = 100
            };

            ExceptionAssert.ThrowsLowerThanValidationException(nameof(request.CaptureDay), 100, () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_ExpirationDateIsInThePast_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "1234",
                ExpirationDate       = DateTime.UtcNow.AddSeconds(-1)
            };

            ExceptionAssert.Throws <InvalidOperationException>("The value for ExpirationDate should be in the future.", () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_PaymentBrandsContainsUnknown_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "1234",
                PaymentBrands        = new PaymentBrand[] { PaymentBrand.VISA, PaymentBrand.Unknown }
            };

            ExceptionAssert.Throws <InvalidOperationException>($"The value for {nameof(request.PaymentBrands)} should not contain an unknown payment brand.", () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_AutomaticResponseUrlIsLongerThanMaxLength_ThrowsException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount = 15.95m,
                AutomaticResponseUrl = new Uri("https://foor.bar/" + new string('x', 496)),
                CurrencyCode         = CurrencyCode.Euro,
                ReturnUrl            = new Uri("https://www.github.com"),
                TransactionReference = "1234",
                CaptureDay           = 0
            };

            ExceptionAssert.ThrowsLengthValidationException(nameof(request.AutomaticResponseUrl), 512, () =>
            {
                PaymentRequestValidator.Validate(request);
            });
        }
        public void Validate_IsValid_NoException()
        {
            PaymentRequest request = new PaymentRequest()
            {
                Amount               = 15.95m,
                CaptureDay           = 2,
                CaptureMode          = "MODE",
                CurrencyCode         = CurrencyCode.Euro,
                ExpirationDate       = DateTime.UtcNow.AddDays(1),
                PaymentBrands        = new PaymentBrand[] { PaymentBrand.IDEAL },
                ReturnUrl            = new Uri("https://www.github.com"),
                AutomaticResponseUrl = new Uri("https://www.github.com"),
                TransactionReference = "1234"
            };

            PaymentRequestValidator.Validate(request);
        }
        public void PaymentRequestValidator_Should_Pass()
        {
            var validPaymentRequest = new PaymentRequest()
            {
                Card = new Card()
                {
                    ExpiryDate = "11/2031",
                    Cvv        = "123",
                    Number     = "0000000000001234",
                    Name       = "Firstname Lastname"
                },
                Amount   = 500m,
                Currency = "GBP"
            };
            var result = _validator.Validate(validPaymentRequest);

            result.IsValid.ShouldBeTrue();
        }
Esempio n. 23
0
        public void Validate_ValidPaymentRequest_ReturnsNoValidationErrors()
        {
            // Arrange
            PaymentRequest request = new PaymentRequest
            {
                Amount            = 100,
                CardholderName    = "Testy McTester",
                CardNumber        = "1111 2222 3333 4444",
                CurrencyIsoAlpha3 = "GBP",
                Cvv = "123"
            };

            var sut = new PaymentRequestValidator();

            // Act
            var result = sut.Validate(request);

            // Assert
            Assert.IsEmpty(result);
        }
Esempio n. 24
0
        public void Validate_InvalidCardNumber_ReturnsValidationError(string testCardNumber)
        {
            // Arrange
            PaymentRequest request = new PaymentRequest
            {
                Amount            = 100,
                CardholderName    = "Valid Name",
                CardNumber        = testCardNumber,
                CurrencyIsoAlpha3 = "GBP",
                Cvv = "123"
            };

            var sut = new PaymentRequestValidator();

            // Act
            var result = sut.Validate(request);

            // Assert
            Assert.AreEqual(1, result.Count);
            Assert.Contains("Card number must be 16 digits long, and contain only numbers", result);
        }
Esempio n. 25
0
        public void Validate_InvalidCardholderName_ReturnsValidationError(string testCardholder)
        {
            // Arrange
            PaymentRequest request = new PaymentRequest
            {
                Amount            = 100,
                CardholderName    = testCardholder,
                CardNumber        = "1111 2222 3333 4444",
                CurrencyIsoAlpha3 = "GBP",
                Cvv = "123"
            };

            var sut = new PaymentRequestValidator();

            // Act
            var result = sut.Validate(request);

            // Assert
            Assert.AreEqual(1, result.Count);
            Assert.Contains("Cardholder name must contain only letters", result);
        }
Esempio n. 26
0
        public void Validate_InvalidCvv_ReturnsValidationError(string testCcv)
        {
            // Arrange
            PaymentRequest request = new PaymentRequest
            {
                Amount            = 100,
                CardholderName    = "Valid Name",
                CardNumber        = "1111 2222 3333 4444",
                CurrencyIsoAlpha3 = "GBP",
                Cvv = testCcv
            };

            var sut = new PaymentRequestValidator();

            // Act
            var result = sut.Validate(request);

            // Assert
            Assert.AreEqual(1, result.Count);
            Assert.Contains("CVV must be 3 digits long, and only contain numbers", result);
        }
Esempio n. 27
0
        public void Should_Not_Have_Validation_Error_For_Current_ExpiryMonth_And_ExpiryYear()
        {
            var validator = new PaymentRequestValidator();

            var paymentRequest = new PaymentRequest
            {
                CardNumber = "4242424242424242",
                Amount     = 32.56m,
                Currency   = "USD",
                Cvv        = "123",

                ExpiryMonth = DateTime.Now.Month,
                ExpiryYear  = DateTime.Now.Year
            };

            var validationResult = validator.Validate(paymentRequest);

            validationResult.IsValid.Should().BeTrue();
            validationResult.Errors.Should().NotContain(x => x.PropertyName == "ExpiryMonth");
            validationResult.Errors.Should().NotContain(x => x.PropertyName == "ExpiryYear");
        }
Esempio n. 28
0
        public void Validate_InvalidCurrency_ReturnsValidationError(string testCurrency)
        {
            // Arrange
            PaymentRequest request = new PaymentRequest
            {
                Amount            = 100,
                CardholderName    = "Valid Name",
                CardNumber        = "1111 2222 3333 4444",
                CurrencyIsoAlpha3 = testCurrency,
                Cvv = "123"
            };

            var sut = new PaymentRequestValidator();

            // Act
            var result = sut.Validate(request);

            // Assert
            Assert.AreEqual(1, result.Count);
            Assert.Contains("Currency must be in ISO 4217 Alpha 3 format", result);
        }
Esempio n. 29
0
        public void Validate_InvalidAmount_ReturnsValidationError(decimal testAmount)
        {
            // Arrange
            PaymentRequest request = new PaymentRequest
            {
                Amount            = testAmount,
                CardholderName    = "Valid Name",
                CardNumber        = "1111 2222 3333 4444",
                CurrencyIsoAlpha3 = "GBP",
                Cvv = "123"
            };

            var sut = new PaymentRequestValidator();

            // Act
            var result = sut.Validate(request);

            // Assert
            Assert.AreEqual(1, result.Count);
            Assert.Contains("Payment must be greater than 0", result);
        }
Esempio n. 30
0
        public async Task <IActionResult> Create([FromBody] OrderRequestDto payload)
        {
            TextInfo caseFormat = new CultureInfo("en-US", false).TextInfo;

            var accountId = UserHelper.GetAccountId(User);

            if (payload == null)
            {
                ModelState.AddModelError("order", "The order cannot be blank");
                return(BadRequest(Responses.ErrorResponse <PaymentRequest>(ModelState.ToErrors(), StatusMessage.ValidationErrors, ResponseCodes.VALIDATION_ERRORS)));
            }

            //call a function that assigns the customer, branch and employee from an order to the payment if empty on the payment
            payload = setCustomerDataOnPayment(payload);

            var payment = payload?.Payment;

            if (payment == null)
            {
                ModelState.AddModelError("payment", "There is no payment attached to the order");
                return(BadRequest(Responses.ErrorResponse <PaymentRequest>(ModelState.ToErrors(), StatusMessage.ValidationErrors, ResponseCodes.VALIDATION_ERRORS)));
            }

            var paymentValidator = new PaymentRequestValidator(_paymentTypeConfiguration);

            paymentValidator.Validate(payment).AddToModelState(ModelState, null);
            if (!ModelState.IsValid)
            {
                return(BadRequest(Responses.ErrorResponse <PaymentRequest>(ModelState.ToErrors(), StatusMessage.ValidationErrors, ResponseCodes.VALIDATION_ERRORS)));
            }

            var orderValidator = new OrderRequestValidator();

            orderValidator.Validate(payload).AddToModelState(ModelState, null);
            if (!ModelState.IsValid)
            {
                return(BadRequest(Responses.ErrorResponse <PaymentRequest>(ModelState.ToErrors(), StatusMessage.ValidationErrors, ResponseCodes.VALIDATION_ERRORS)));
            }

            var orderRequest = _mapper.Map <OrderRequest>(payload);

            var paymentRequest = _mapper.Map <PaymentRequest>(payment);

            paymentRequest.OrderRequestDoc = JsonConvert.SerializeObject(orderRequest);
            paymentRequest = await _paymentRequestRepository.AddAsync(paymentRequest).ConfigureAwait(false);

            var paymentTypeClassName = $"Hubtel.PaymentProxy.Services.{caseFormat.ToTitleCase(payment.PaymentType.ToLower())}PaymentService";
            var paymentService       = (PaymentService)ActivatorUtilities.CreateInstance(_provider, Type.GetType(paymentTypeClassName));
            //-->
            var processPaymentResult = await paymentService.ProcessPayment(paymentRequest).ConfigureAwait(false);

            if (processPaymentResult.Success)
            {
                processPaymentResult.Data.OrderRequestDoc = null;
                return(Ok(processPaymentResult));
            }
            await _paymentRequestRepository.DeleteByClientReferenceAsync(paymentRequest.ClientReference).ConfigureAwait(false);

            processPaymentResult.Data = null;
            return(BadRequest(processPaymentResult));
        }