From 9960f0e807d58bdfa846ae67e43a9917b992189d Mon Sep 17 00:00:00 2001 From: Alexander Pervushin Date: Mon, 7 Nov 2022 00:17:59 +0600 Subject: [PATCH] CreditCardValidator. Added MIR cards support --- .../commons/validator/routines/CreditCardValidator.java | 9 +++++++++ .../validator/routines/CreditCardValidatorTest.java | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java b/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java index 468e1c46d..bb441af34 100644 --- a/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java +++ b/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java @@ -199,6 +199,10 @@ public CreditCardRange(final String low, final String high, final int [] lengths @Deprecated public static final long MASTERCARD_PRE_OCT2016 = 1 << 6; // CHECKSTYLE IGNORE MagicNumber + /** + * Option specifying that MIR cards are allowed + */ + public static final long MIR = 1 << 7; /** * The CreditCardTypes that are allowed to pass validation. @@ -282,6 +286,7 @@ public CreditCardRange(final String low, final String high, final int [] lengths */ public static final CodeValidator VPAY_VALIDATOR = new CodeValidator("^(4)(\\d{12,18})$", LUHN_VALIDATOR); + public static final CodeValidator MIR_VALIDATOR = new CodeValidator("^(220\\d{13})$", LUHN_VALIDATOR); /** * Create a new CreditCardValidator with default options. * The default options are: @@ -325,6 +330,10 @@ public CreditCardValidator(final long options) { if (isOn(options, DINERS)) { this.cardTypes.add(DINERS_VALIDATOR); } + + if (isOn(options, MIR)) { + this.cardTypes.add(MIR_VALIDATOR); + } } /** diff --git a/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java index 35a5db3e7..8da567319 100644 --- a/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java +++ b/src/test/java/org/apache/commons/validator/routines/CreditCardValidatorTest.java @@ -43,6 +43,8 @@ public class CreditCardValidatorTest extends TestCase { private static final String VALID_VPAY = "4370000000000061"; // 16 private static final String VALID_VPAY2 = "4370000000000012"; private static final String ERROR_VPAY = "4370000000000069"; + private static final String VALID_MIR = "2203368778544108"; + private static final String ERROR_MIR = "2203368778544107"; private static final String [] VALID_CARDS = { VALID_VISA, @@ -55,6 +57,7 @@ public class CreditCardValidatorTest extends TestCase { VALID_VPAY, VALID_VPAY2, "60115564485789458", // VALIDATOR-403 + VALID_MIR }; private static final String [] ERROR_CARDS = { @@ -71,6 +74,7 @@ public class CreditCardValidatorTest extends TestCase { "12345678901", // too short (11) "12345678901234567890", // too long (20) "4417123456789112", // invalid check digit + ERROR_MIR }; /** @@ -588,6 +592,7 @@ public void testRangeGenerator() { CreditCardValidator.VISA_VALIDATOR, CreditCardValidator.MASTERCARD_VALIDATOR, CreditCardValidator.DISCOVER_VALIDATOR, + CreditCardValidator.MIR_VALIDATOR }, // Add missing validator new CreditCardRange[]{