From 568bf9a7cc1b2b6df58c30e86e2674faf57f37ea Mon Sep 17 00:00:00 2001 From: Douglas Webb Date: Mon, 21 Oct 2019 14:39:29 -0400 Subject: [PATCH] Added validation helper to detect masked numbers, and changed CreditCardDetector to allow masked numbers Masked number is 6 digits, followed by asterisks, followed by 4 digits. Number of asterisks varies based on CC number length. --- .../CreditCardDetector.cs | 4 +- .../Helpers/ValidationHelper.cs | 7 ++ .../Data/ValidCards.json | 99 ++++++++++++++++--- 3 files changed, 94 insertions(+), 16 deletions(-) diff --git a/Src/CreditCardValidator.Net35/CreditCardDetector.cs b/Src/CreditCardValidator.Net35/CreditCardDetector.cs index d2bfe43..ce3e1d6 100644 --- a/Src/CreditCardValidator.Net35/CreditCardDetector.cs +++ b/Src/CreditCardValidator.Net35/CreditCardDetector.cs @@ -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(); @@ -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(); diff --git a/Src/CreditCardValidator.Net35/Helpers/ValidationHelper.cs b/Src/CreditCardValidator.Net35/Helpers/ValidationHelper.cs index f9cfa22..31c5aa0 100644 --- a/Src/CreditCardValidator.Net35/Helpers/ValidationHelper.cs +++ b/Src/CreditCardValidator.Net35/Helpers/ValidationHelper.cs @@ -1,4 +1,5 @@ using System.Linq; +using System.Text.RegularExpressions; namespace CreditCardValidator.Helpers { @@ -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}$"); + } } } \ No newline at end of file diff --git a/Src/CreditCardValidator.Tests/Data/ValidCards.json b/Src/CreditCardValidator.Tests/Data/ValidCards.json index 4a7a49b..b274247 100644 --- a/Src/CreditCardValidator.Tests/Data/ValidCards.json +++ b/Src/CreditCardValidator.Tests/Data/ValidCards.json @@ -3,7 +3,11 @@ "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", @@ -11,7 +15,13 @@ "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", @@ -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", @@ -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", @@ -52,14 +89,27 @@ "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", @@ -67,35 +117,56 @@ "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" ] }