merged branch ricardclau/improve-creditcard-regexp (PR #6583)
This PR was merged into the master branch. Commits -------5be0042
better regexp, more test cases, added comments about each credit cardcc278af
[Validator] Fix `CardSchemeValidator` double violation when value is non-numeric. Making scheme option accept strings in addition to arrays. Discussion ---------- [Validator] Improve regexp for Credit Cards and some more tests Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: Todo: Ensure these regexps are proper (credit card validation is always a pain) License of the code: MIT Documentation PR: Regarding Cases excluded from new Regular Expressions: - Credit card lengths should be respected, these regexp cover lengths in http://en.wikipedia.org/wiki/Bank_card_number - Visa length can only be 16 and 13 (older ones) - Diners Cards starting by 5 come from a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard (according to http://www.regular-expressions.info/creditcard.html). - There seems to be JCB cards starting by 2131 and 1800, I could find them is some places, also found these numbers being tested in Credit Card generators, but some people don't cover them. I don't know their story either Any comments will be much appreciated! --------------------------------------------------------------------------- by fabpot at 2013-01-06T19:33:27Z Thanks for working on this. It would be very valuable if you can add information about these regexes as comments (with links to relevant sources -- like what you've done in the PR description). Thanks. --------------------------------------------------------------------------- by ricardclau at 2013-01-06T21:01:52Z Always glad to be able to contribute a little bit @fabpot you mean @link / @see PHPDoc inside CardSchemeValidator.php? Or further comments in this discussion before adding them? --------------------------------------------------------------------------- by fabpot at 2013-01-06T21:16:48Z The more information we can add in the class, the better it is. --------------------------------------------------------------------------- by ricardclau at 2013-01-07T20:56:05Z I've added comments and included code from #6603 as I've said there. If you need something else, please let me know, once this is merged, #6603 can also be closed --------------------------------------------------------------------------- by fabpot at 2013-01-07T21:41:40Z Can you keep the commit from #6603 to keep ownership? --------------------------------------------------------------------------- by ricardclau at 2013-01-07T21:44:16Z I actually have thought about that... let me try my git skills :) --------------------------------------------------------------------------- by ricardclau at 2013-01-07T21:59:16Z There you go!
This commit is contained in:
commit
87290264e3
@ -18,45 +18,80 @@ use Symfony\Component\Validator\ConstraintValidator;
|
||||
* Validates that a card number belongs to a specified scheme.
|
||||
*
|
||||
* @see http://en.wikipedia.org/wiki/Bank_card_number
|
||||
* @see http://www.regular-expressions.info/creditcard.html
|
||||
* @author Tim Nagel <t.nagel@infinite.net.au>
|
||||
*/
|
||||
class CardSchemeValidator extends ConstraintValidator
|
||||
{
|
||||
protected $schemes = array(
|
||||
/**
|
||||
* American Express card numbers start with 34 or 37 and have 15 digits.
|
||||
*/
|
||||
'AMEX' => array(
|
||||
'/^(3[47])([0-9]{13})/'
|
||||
'/^3[47][0-9]{13}$/'
|
||||
),
|
||||
/**
|
||||
* China UnionPay cards start with 62 and have between 16 and 19 digits.
|
||||
* Please note that these cards do not follow Luhn Algorithm as a checksum.
|
||||
*/
|
||||
'CHINA_UNIONPAY' => array(
|
||||
'/^(62)([0-9]{16,19}/'
|
||||
'/^62[0-9]{14,17}$/'
|
||||
),
|
||||
/**
|
||||
* Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits.
|
||||
* There are Diners Club cards that begin with 5 and have 16 digits.
|
||||
* These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard.
|
||||
*/
|
||||
'DINERS' => array(
|
||||
'/^(36)([0-9]{12})/',
|
||||
'/^(30[0-5])([0-9]{11})/',
|
||||
'/^(5[45])([0-9]{14})/'
|
||||
'/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',
|
||||
),
|
||||
/**
|
||||
* Discover card numbers begin with 6011, 622126 through 622925, 644 through 649 or 65.
|
||||
* All have 16 digits
|
||||
*/
|
||||
'DISCOVER' => array(
|
||||
'/^(6011)([0-9]{12})/',
|
||||
'/^(64[4-9])([0-9]{13})/',
|
||||
'/^(65)([0-9]{14})/',
|
||||
'/^(622)(12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|91[0-9]|92[0-5])([0-9]{10})/'
|
||||
'/^6011[0-9]{12}$/',
|
||||
'/^64[4-9][0-9]{13}$/',
|
||||
'/^65[0-9]{14}$/',
|
||||
'/^622(12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|91[0-9]|92[0-5])[0-9]{10}$/'
|
||||
),
|
||||
/**
|
||||
* InstaPayment cards begin with 637 through 639 and have 16 digits
|
||||
*/
|
||||
'INSTAPAYMENT' => array(
|
||||
'/^(63[7-9])([0-9]{13})/'
|
||||
'/^63[7-9][0-9]{13}$/'
|
||||
),
|
||||
/**
|
||||
* JCB cards beginning with 2131 or 1800 have 15 digits.
|
||||
* JCB cards beginning with 35 have 16 digits.
|
||||
*/
|
||||
'JCB' => array(
|
||||
'/^(352[8-9]|35[3-8][0-9])([0-9]{12})/'
|
||||
'/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/'
|
||||
),
|
||||
/**
|
||||
* Laser cards begin with either 6304, 6706, 6709 or 6771 and have between 16 and 19 digits
|
||||
*/
|
||||
'LASER' => array(
|
||||
'/^(6304|670[69]|6771)([0-9]{12, 15})/'
|
||||
'/^(6304|670[69]|6771)[0-9]{12,15}$/'
|
||||
),
|
||||
/**
|
||||
* Maestro cards begin with either 5018, 5020, 5038, 5893, 6304, 6759, 6761, 6762, 6763 or 0604
|
||||
* They have between 12 and 19 digits
|
||||
*/
|
||||
'MAESTRO' => array(
|
||||
'/^(5018|5020|5038|6304|6759|6761|676[23]|0604)([0-9]{8, 15})/'
|
||||
'/^(5018|5020|5038|6304|6759|6761|676[23]|0604)[0-9]{8,15}$/'
|
||||
),
|
||||
/**
|
||||
* All MasterCard numbers start with the numbers 51 through 55. All have 16 digits.
|
||||
*/
|
||||
'MASTERCARD' => array(
|
||||
'/^(5[1-5])([0-9]{14})/'
|
||||
'/^5[1-5][0-9]{14}$/'
|
||||
),
|
||||
/**
|
||||
* All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13.
|
||||
*/
|
||||
'VISA' => array(
|
||||
'/^(4)([0-9]{12})/'
|
||||
'/^4([0-9]{12}|[0-9]{15})$/'
|
||||
),
|
||||
);
|
||||
|
||||
@ -74,9 +109,11 @@ class CardSchemeValidator extends ConstraintValidator
|
||||
|
||||
if (!is_numeric($value)) {
|
||||
$this->context->addViolation($constraint->message);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$schemes = array_flip($constraint->schemes);
|
||||
$schemes = array_flip((array) $constraint->schemes);
|
||||
$schemeRegexes = array_intersect_key($this->schemes, $schemes);
|
||||
|
||||
foreach ($schemeRegexes as $regexes) {
|
||||
|
@ -56,26 +56,79 @@ class CardSchemeValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->context->expects($this->never())
|
||||
->method('addViolation');
|
||||
|
||||
$this->validator->validate($number, new CardScheme(array('schemes' => array($scheme))));
|
||||
$this->validator->validate($number, new CardScheme(array('schemes' => $scheme)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidNumbers
|
||||
*/
|
||||
public function testInvalidNumbers($scheme, $number)
|
||||
{
|
||||
$this->context->expects($this->once())
|
||||
->method('addViolation');
|
||||
|
||||
$this->validator->validate($number, new CardScheme(array('schemes' => $scheme)));
|
||||
}
|
||||
|
||||
public function getValidNumbers()
|
||||
{
|
||||
return array(
|
||||
array('VISA', '42424242424242424242'),
|
||||
array('AMEX', '378282246310005'),
|
||||
array('AMEX', '371449635398431'),
|
||||
array('AMEX', '378734493671000'),
|
||||
array('AMEX', '347298508610146'),
|
||||
array('CHINA_UNIONPAY', '6228888888888888'),
|
||||
array('CHINA_UNIONPAY', '62288888888888888'),
|
||||
array('CHINA_UNIONPAY', '622888888888888888'),
|
||||
array('CHINA_UNIONPAY', '6228888888888888888'),
|
||||
array('DINERS', '30569309025904'),
|
||||
array('DINERS', '36088894118515'),
|
||||
array('DINERS', '38520000023237'),
|
||||
array('DISCOVER', '6011111111111117'),
|
||||
array('DISCOVER', '6011000990139424'),
|
||||
array('INSTAPAYMENT', '6372476031350068'),
|
||||
array('INSTAPAYMENT', '6385537775789749'),
|
||||
array('INSTAPAYMENT', '6393440808445746'),
|
||||
array('JCB', '3530111333300000'),
|
||||
array('JCB', '3566002020360505'),
|
||||
array('JCB', '213112345678901'),
|
||||
array('JCB', '180012345678901'),
|
||||
array('LASER', '6304678107004080'),
|
||||
array('LASER', '6706440607428128629'),
|
||||
array('LASER', '6771656738314582216'),
|
||||
array('MAESTRO', '6759744069209'),
|
||||
array('MAESTRO', '5020507657408074712'),
|
||||
array('MAESTRO', '6759744069209'),
|
||||
array('MAESTRO', '6759744069209'),
|
||||
array('MASTERCARD', '5555555555554444'),
|
||||
array('MASTERCARD', '5105105105105100'),
|
||||
array('VISA', '4111111111111111'),
|
||||
array('VISA', '4012888888881881'),
|
||||
array('VISA', '4222222222222'),
|
||||
array(array('AMEX', 'VISA'), '4111111111111111'),
|
||||
array(array('AMEX', 'VISA'), '378282246310005'),
|
||||
array(array('JCB', 'MASTERCARD'), '5105105105105100'),
|
||||
array(array('VISA', 'MASTERCARD'), '5105105105105100'),
|
||||
);
|
||||
}
|
||||
|
||||
public function getInvalidNumbers()
|
||||
{
|
||||
return array(
|
||||
array('VISA', '42424242424242424242'),
|
||||
array('AMEX', '357298508610146'),
|
||||
array('DINERS', '31569309025904'),
|
||||
array('DINERS', '37088894118515'),
|
||||
array('INSTAPAYMENT', '6313440808445746'),
|
||||
array('CHINA_UNIONPAY', '622888888888888'),
|
||||
array('CHINA_UNIONPAY', '62288888888888888888'),
|
||||
array('AMEX', '30569309025904'), // DINERS number
|
||||
array('AMEX', 'invalid'), // A string
|
||||
array('AMEX', 0), // a lone number
|
||||
array('AMEX', '0'), // a lone number
|
||||
array('AMEX', '000000000000'), // a lone number
|
||||
array('DINERS', '3056930'), // only first part of the number
|
||||
array('DISCOVER', '1117'), // only last 4 digits
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user