/// <summary> /// Action to Validate Credit Card Number. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnValidate_Click(object sender, EventArgs e) { ICardNumberGenerator cardNumberGenerator = CardNumberGenerator.Instance; if (txtCard.Text.Length > 0) { string cardNum = txtCard.Text; if (cardNumberGenerator.IsValidCreditCardNumber(cardNum)) { Constants.CardIssuer?cardType = cardNumberGenerator.GetCardTypeFromNumber(cardNum); string strCardType = (cardType == null) ? "Unknown" : cardType.ToString(); showMessage(String.Format("You have entered a valid card number. The card type is {0}.", strCardType), true); } else { showMessage("Card failed test. Please enter a valid card number.", false); } if (cardNumberGenerator.IsRewardsCardNumber(cardNum)) { Constants.CardIssuer?cardType = cardNumberGenerator.GetCardTypeFromNumber(cardNum); string strCardType = (cardType == null) ? "Unknown" : cardType.ToString(); showMessage(String.Format("You have entered a reward card number. The card type is {0}.", strCardType), true); } } else { showMessage("Please enter a card number first.", false); } }
public void GetCardTypeFromNumberTest() { ICardNumberGenerator cardNumberGenerator = CardNumberGenerator.Instance; string visaNum = "4111-1111-1111-1111"; string amexNum = "3782-822463-10005"; string mcNum = "5105-1051-0510-5100"; string discoverNum = "6011-1111-1111-1117"; string dinersNum = "3056-930902-5904"; string jcbNum = "3566-0020-2036-0505"; Assert.AreEqual(Constants.CardIssuer.Visa, cardNumberGenerator.GetCardTypeFromNumber(visaNum), "GetCardTypeFromNumber should return Visa"); Assert.AreEqual(Constants.CardIssuer.AmericanExpress, cardNumberGenerator.GetCardTypeFromNumber(amexNum), "GetCardTypeFromNumber should return American Express"); Assert.AreEqual(Constants.CardIssuer.MasterCard, cardNumberGenerator.GetCardTypeFromNumber(mcNum), "GetCardTypeFromNumber should return MasterCard"); Assert.AreEqual(Constants.CardIssuer.Discover, cardNumberGenerator.GetCardTypeFromNumber(discoverNum), "GetCardTypeFromNumber should return Discover"); Assert.AreEqual(Constants.CardIssuer.Diners, cardNumberGenerator.GetCardTypeFromNumber(dinersNum), "GetCardTypeFromNumber should return Diners"); Assert.AreEqual(Constants.CardIssuer.JCB, cardNumberGenerator.GetCardTypeFromNumber(jcbNum), "GetCardTypeFromNumber should return JCB"); }
public void IsValidCreditCardTypeTest() { ICardNumberGenerator cardNumberGenerator = CardNumberGenerator.Instance; //Src: https://developer.paypal.com/docs/classic/payflow/payflow-pro/payflow-pro-testing/ //Src: https://stevemorse.org/ssn/List_of_Bank_Identification_Numbers.html //American Express 378282246310005 string amexCardDash = "3782-822463-10005"; string amexCardSpace = "3782 822463 10005"; string amexCardNoSpace = "378282246310005"; string amexCardDot = "3782.822463.10005"; Assert.IsTrue(Constants.CardIssuer.AmericanExpress == cardNumberGenerator.GetCardTypeFromNumber(amexCardDash), "IsValidNumber should allow Amex numbers with dashes"); Assert.IsTrue(Constants.CardIssuer.AmericanExpress == cardNumberGenerator.GetCardTypeFromNumber(amexCardSpace), "IsValidNumber should allow Amex numbers with spaces"); Assert.IsTrue(Constants.CardIssuer.AmericanExpress == cardNumberGenerator.GetCardTypeFromNumber(amexCardNoSpace), "IsValidNumber should allow Amex numbers with no spaces"); Assert.IsFalse(Constants.CardIssuer.AmericanExpress == cardNumberGenerator.GetCardTypeFromNumber(amexCardDot), "IsValidNumber should not allow Amex numbers with decimals"); //Visa 4111111111111111 //Visa 4012888888881881 string visaCardDash = "4111-1111-1111-1111"; string visaCardSpace = "4111 1111 1111 1111"; string visaCardNoSpace = "4012888888881881"; string visaCardDot = "4012.8888.8888.1881"; Assert.IsTrue(Constants.CardIssuer.Visa == cardNumberGenerator.GetCardTypeFromNumber(visaCardDash), "IsValidNumber should allow Visa numbers with dashes"); Assert.IsTrue(Constants.CardIssuer.Visa == cardNumberGenerator.GetCardTypeFromNumber(visaCardSpace), "IsValidNumber should allow Visa numbers with spaces"); Assert.IsTrue(Constants.CardIssuer.Visa == cardNumberGenerator.GetCardTypeFromNumber(visaCardNoSpace), "IsValidNumber should allow Visa numbers with no spaces"); Assert.IsFalse(Constants.CardIssuer.Visa == cardNumberGenerator.GetCardTypeFromNumber(visaCardDot), "IsValidNumber should not allow Visa numbers with decimals"); string mcCardDash = "5500-0000-0000-0004"; string mcCardSpace = "5500 0000 0000 0004"; string mcCardNoSpace = "5500000000000004"; string mcCardDot = "5500.0000.0000.0004"; Assert.IsTrue(Constants.CardIssuer.MasterCard == cardNumberGenerator.GetCardTypeFromNumber(mcCardDash), "IsValidNumber should allow MC numbers with dashes"); Assert.IsTrue(Constants.CardIssuer.MasterCard == cardNumberGenerator.GetCardTypeFromNumber(mcCardSpace), "IsValidNumber should allow MC numbers with spaces"); Assert.IsTrue(Constants.CardIssuer.MasterCard == cardNumberGenerator.GetCardTypeFromNumber(mcCardNoSpace), "IsValidNumber should allow MC numbers with no spaces"); Assert.IsFalse(Constants.CardIssuer.MasterCard == cardNumberGenerator.GetCardTypeFromNumber(mcCardDot), "IsValidNumber should not allow MC numbers with decimals"); //Discover 6011111111111117 //Discover 6011000990139424 string discoverCardDash = "6011-1111-1111-1117"; string discoverCardSpace = "6011 1111 1111 1117"; string discoverCardNoSpace = "6011111111111117"; string discoverCardDot = "6011.1111.1111.1117"; Assert.IsTrue(Constants.CardIssuer.Discover == cardNumberGenerator.GetCardTypeFromNumber(discoverCardDash), "IsValidNumber should allow Discover numbers with dashes"); Assert.IsTrue(Constants.CardIssuer.Discover == cardNumberGenerator.GetCardTypeFromNumber(discoverCardSpace), "IsValidNumber should allow Discover numbers with spaces"); Assert.IsTrue(Constants.CardIssuer.Discover == cardNumberGenerator.GetCardTypeFromNumber(discoverCardNoSpace), "IsValidNumber should allow Discover numbers with no spaces"); Assert.IsFalse(Constants.CardIssuer.Discover == cardNumberGenerator.GetCardTypeFromNumber(discoverCardDot), "IsValidNumber should not allow Discover numbers with decimals"); //Diners Club 30569309025904 //Diners Club 38520000023237 string dinersCardDash = "3056-930902-5904"; string dinersCardSpace = "3056 930902 5904"; string dinersCardNoSpace = "30569309025904"; string dinersCardDot = "3056.930902.5904"; Assert.IsTrue(Constants.CardIssuer.Diners == cardNumberGenerator.GetCardTypeFromNumber(dinersCardDash), "IsValidNumber should allow Diners numbers with dashes"); Assert.IsTrue(Constants.CardIssuer.Diners == cardNumberGenerator.GetCardTypeFromNumber(dinersCardSpace), "IsValidNumber should allow Diners numbers with spaces"); Assert.IsTrue(Constants.CardIssuer.Diners == cardNumberGenerator.GetCardTypeFromNumber(dinersCardNoSpace), "IsValidNumber should allow Diners numbers with no spaces"); Assert.IsFalse(Constants.CardIssuer.Diners == cardNumberGenerator.GetCardTypeFromNumber(dinersCardDot), "IsValidNumber should not allow Diners numbers with decimals"); //JCB 3530111333300000 //JCB 3566002020360505 string jcbCardDash = "3566-0020-2036-0505"; string jcbCardSpace = "3566 0020 2036 0505"; string jcbCardNoSpace = "3566002020360505"; string jcbCardDot = "3566.0020.2036.0505"; Assert.IsTrue(Constants.CardIssuer.JCB == cardNumberGenerator.GetCardTypeFromNumber(jcbCardDash), "IsValidNumber should allow JCB numbers with dashes"); Assert.IsTrue(Constants.CardIssuer.JCB == cardNumberGenerator.GetCardTypeFromNumber(jcbCardSpace), "IsValidNumber should allow JCB numbers with spaces"); Assert.IsTrue(Constants.CardIssuer.JCB == cardNumberGenerator.GetCardTypeFromNumber(jcbCardNoSpace), "IsValidNumber should allow JCB numbers with no spaces"); Assert.IsFalse(Constants.CardIssuer.JCB == cardNumberGenerator.GetCardTypeFromNumber(jcbCardDot), "IsValidNumber should not allow JCB numbers with decimals"); }