/// <summary> /// Validate card number /// </summary> /// <param name="cardNumber">Card number</param> /// <returns>Validation result <see cref="ValidationResult" /> </returns> public object Validate([FromBody] string cardNumber) { try { if (cardNumber == null) { return(BadRequest($"Card number not specified")); } decimal?cardNumberDec = CardNumberHelper.TryParse(cardNumber); if (cardNumberDec == null) { return(BadRequest($"Invalid card number")); } var res = Repository.ValidateCard(cardNumberDec.Value); return(new ValidationResult { CardType = res.CardType, ValidationStatus = res.ValidationStatus, }); } catch (Exception e) { Logger.Error(e, $"Validate cardNumber={cardNumber}"); return(InternalServerError(e)); } }
static void TestParseCardNumber(string cardNumberString, decimal?expectedCardNumber) { decimal?actualCardNumber = CardNumberHelper.TryParse(cardNumberString); Assert.AreEqual(expectedCardNumber, actualCardNumber); }
public void TestParseCardNumber15HyphenDelimValid() => Assert.AreEqual(123456789012345M, CardNumberHelper.TryParse("1234-567890-12345"));
public void TestParseCardNumber15MixDelimInvalid() => Assert.AreEqual(null, CardNumberHelper.TryParse("1234 567890-12345"));
public void TestParseCardNumber15WhitespaceDelimValid() => Assert.AreEqual(123456789012345M, CardNumberHelper.TryParse("1234 567890 12345"));
public void TestParseCardNumber16HyphenDelimValid() => Assert.AreEqual(1234567812345678M, CardNumberHelper.TryParse("1234-5678-1234-5678"));
public void TestParseCardNumberGreater16Invalid() => Assert.AreEqual(null, CardNumberHelper.TryParse("12345678123456781"));
public void TestParseCardNumber15Valid() => Assert.AreEqual(123456781234567M, CardNumberHelper.TryParse("123456781234567"));
public void TestParseCardNumberLess15Invalid() => Assert.AreEqual(null, CardNumberHelper.TryParse("12345678123456"));
public void TestParseCardNumberNonDigitCharsInvalid() => Assert.AreEqual(null, CardNumberHelper.TryParse("1234567812345A"));
public void TestParseCardNumberEmptyInvalid() => Assert.AreEqual(null, CardNumberHelper.TryParse(string.Empty));