Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added helper to detect masked numbers, and changed CreditCardDetector to allow them #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Src/CreditCardValidator.Net35/CreditCardDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CreditCardDetector

public CreditCardDetector(string cardNumber)
{
if (!ValidationHelper.IsAValidNumber(cardNumber))
if (!ValidationHelper.IsAValidNumber(cardNumber) && !ValidationHelper.IsAMaskedNumber(cardNumber))
throw new ArgumentException("Invalid number. Just numbers and white spaces are accepted on the string.");

CardNumber = cardNumber.RemoveWhiteSpace();
Expand All @@ -23,7 +23,7 @@ public CreditCardDetector(string cardNumber)

internal CreditCardDetector(string cardNumber, bool ignoreLengthCheck)
{
if (!ValidationHelper.IsAValidNumber(cardNumber))
if (!ValidationHelper.IsAValidNumber(cardNumber) && !ValidationHelper.IsAMaskedNumber(cardNumber))
throw new ArgumentException("Invalid number. Just numbers and white spaces are accepted on the string.");

CardNumber = cardNumber.RemoveWhiteSpace();
Expand Down
7 changes: 7 additions & 0 deletions Src/CreditCardValidator.Net35/Helpers/ValidationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Text.RegularExpressions;

namespace CreditCardValidator.Helpers
{
Expand All @@ -13,5 +14,11 @@ public static bool IsAValidNumber(string number)
.All(char.IsNumber) &&
!string.IsNullOrEmpty(number));
}

public static bool IsAMaskedNumber(string number)
{
number = number.RemoveWhiteSpace();
return Regex.IsMatch(number, @"^\d{6}\*+\d{4}$");
}
}
}
99 changes: 85 additions & 14 deletions Src/CreditCardValidator.Tests/Data/ValidCards.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@
"4012 8888 8888 1881",
"4111 1111 1111 1111",
"4222 2222 2222 2",
"4917 6100 0000 0000"
"4917 6100 0000 0000",
"4012 88** **** 1881",
"4111 11** **** 1111",
"4222 22** *222 2",
"4917 61** **** 0000"
],
"MasterCard": [
"2230 0585 1022 2421",
"2336 8478 0025 6484",
"2720 3236 4111 0482",
"5274 5763 9425 9961",
"5555 5555 5555 4444",
"5105 1051 0510 5100"
"5105 1051 0510 5100",
"2230 05** **** 2421",
"2336 84** **** 6484",
"2720 32** **** 0482",
"5274 57** **** 9961",
"5555 55** **** 4444",
"5105 10** **** 5100"
],
"DinersClub": [
"3003 5268 8412 3642",
Expand All @@ -25,7 +35,19 @@
"3614 8900 6479 13",
"3670 0102 0000 00",
"3813 1367 4268 4783",
"30569309025904"
"30569309025904",
"3003 52** **** 3642",
"3011 71** **** 7500",
"3022 32** **** 4066",
"3033 23** **** 2778",
"3045 54** **** 4520",
"3059 13** **** 6367",
"3095 76** **** 5354",
"3600 00** **00 08",
"3614 89** **79 13",
"3670 01** **00 00",
"3813 13** **** 4783",
"305693****5904"
],
"AmericanExpress": [
"3714 4963 5398 431",
Expand All @@ -42,7 +64,22 @@
"3742 0000 0000 004",
"3764 6228 0921 451",
"3777 5274 9896 404",
"3782 8224 6310 005"
"3782 8224 6310 005",
"3714 49** ***8 431",
"3787 34** ***1 000",
"3400 00** ***0 009",
"3411 11** ***1 111",
"3434 34** ***4 343",
"3468 27** ***5 344",
"3700 00** ***0 002",
"3700 00** ***0 000",
"3704 07** ***9 809",
"3705 56** ***9 221",
"3714 49** ***8 431",
"3742 00** ***0 004",
"3764 62** ***1 451",
"3777 52** ***6 404",
"3782 82** ***0 005"
],
"Discover": [
"6011 1111 1111 1117",
Expand All @@ -52,50 +89,84 @@
"6011 1532 1637 1980",
"6011 6011 6011 6611",
"6011 6874 8256 4166",
"6011 8148 3690 5651"
"6011 8148 3690 5651",
"6011 11** **** 1117",
"6011 00** **** 9424",
"6011 00** **** 0004",
"6011 00** **** 0000",
"6011 15** **** 1980",
"6011 60** **** 6611",
"6011 68** **** 4166",
"6011 81** **** 5651"
],
"Maestro": [
"6759 6498 2643 8453",
"5641 8200 0000 0005",
"5033 9619 8909 17",
"5868 2416 0825 5333 38",
"6799 9901 0000 0000 019"
"6799 9901 0000 0000 019",
"6759 64** **** 8453",
"5641 82** **** 0005",
"5033 96** **09 17",
"5868 24** **** **33 38",
"6799 99** **** ***0 019"
],
"JCB": [
"3575 7591 5225 4876",
"3566 0020 2036 0505",
"1800 0163 8277 392",
"3569 9900 0000 0009",
"3530 1113 3330 0000",
"3572 6600 0000 0000 006"
"3572 6600 0000 0000 006",
"3575 75** **** 4876",
"3566 00** **** 0505",
"1800 01** ***7 392",
"3569 99** **** 0009",
"3530 11** **** 0000",
"3572 66** **** ***0 006"
],
"Solo": [
"6767 6222 2222 2222 222",
"6334 5805 0000 0000",
"6334 9000 0000 0005",
"6334 7306 0000 0000 00",
"6767 6767 6767 6767 671"
"6767 6767 6767 6767 671",
"6767 62** **** ***2 222",
"6334 58** **** 0000",
"6334 90** **** 0005",
"6334 73** **** **00 00",
"6767 67** **** ***7 671"
],
"ChinaUnionPay": [
"6264 1852 1292 2132 067",
"6288 9977 1545 2584",
"6269 9920 5813 4322"
"6269 9920 5813 4322",
"6264 18** **** ***2 067",
"6288 99** **** 2584",
"6269 99** **** 4322"
],
"Dankort": [
"5019 7170 1010 3742"
"5019 7170 1010 3742",
"5019 71** **** 3742"
],
"Switch": [
"6331 1019 9999 0016"
"6331 1019 9999 0016",
"6331 10** **** 0016"
],
"Laser": [
"6304 9506 0000 0000 00",
"6304 9000 1774 0292 441"
"6304 9000 1774 0292 441",
"6304 95** **** **00 00",
"6304 90** **** ***2 441"
],
"RuPay": [
"6076 6000 0619 9992",
"6070 5500 5000 0047"
"6070 5500 5000 0047",
"6076 60** **** 9992",
"6070 55** **** 0047"
],
"Hipercard": [
"3841 0058 9908 8180 330"
"3841 0058 9908 8180 330",
"3841 00** **** ***0 330"
]
}