CardSchemeValidator.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  14. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  15. /**
  16. * Validates that a card number belongs to a specified scheme.
  17. *
  18. * @author Tim Nagel <t.nagel@infinite.net.au>
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. *
  21. * @see http://en.wikipedia.org/wiki/Bank_card_number
  22. * @see http://www.regular-expressions.info/creditcard.html
  23. * @see http://www.barclaycard.co.uk/business/files/Ranges_and_Rules_September_2014.pdf
  24. */
  25. class CardSchemeValidator extends ConstraintValidator
  26. {
  27. protected $schemes = array(
  28. // American Express card numbers start with 34 or 37 and have 15 digits.
  29. 'AMEX' => array(
  30. '/^3[47][0-9]{13}$/',
  31. ),
  32. // China UnionPay cards start with 62 and have between 16 and 19 digits.
  33. // Please note that these cards do not follow Luhn Algorithm as a checksum.
  34. 'CHINA_UNIONPAY' => array(
  35. '/^62[0-9]{14,17}$/',
  36. ),
  37. // Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits.
  38. // There are Diners Club cards that begin with 5 and have 16 digits.
  39. // These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard.
  40. 'DINERS' => array(
  41. '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',
  42. ),
  43. // Discover card numbers begin with 6011, 622126 through 622925, 644 through 649 or 65.
  44. // All have 16 digits.
  45. 'DISCOVER' => array(
  46. '/^6011[0-9]{12}$/',
  47. '/^64[4-9][0-9]{13}$/',
  48. '/^65[0-9]{14}$/',
  49. '/^622(12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|91[0-9]|92[0-5])[0-9]{10}$/',
  50. ),
  51. // InstaPayment cards begin with 637 through 639 and have 16 digits.
  52. 'INSTAPAYMENT' => array(
  53. '/^63[7-9][0-9]{13}$/',
  54. ),
  55. // JCB cards beginning with 2131 or 1800 have 15 digits.
  56. // JCB cards beginning with 35 have 16 digits.
  57. 'JCB' => array(
  58. '/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/',
  59. ),
  60. // Laser cards begin with either 6304, 6706, 6709 or 6771 and have between 16 and 19 digits.
  61. 'LASER' => array(
  62. '/^(6304|670[69]|6771)[0-9]{12,15}$/',
  63. ),
  64. // Maestro international cards begin with 675900..675999 and have between 12 and 19 digits.
  65. // Maestro UK cards begin with either 500000..509999 or 560000..699999 and have between 12 and 19 digits.
  66. 'MAESTRO' => array(
  67. '/^(6759[0-9]{2})[0-9]{6,13}$/',
  68. '/^(50[0-9]{4})[0-9]{6,13}$/',
  69. '/^5[6-9][0-9]{10,17}$/',
  70. '/^6[0-9]{11,18}$/',
  71. ),
  72. // All MasterCard numbers start with the numbers 51 through 55. All have 16 digits.
  73. // October 2016 MasterCard numbers can also start with 222100 through 272099.
  74. 'MASTERCARD' => array(
  75. '/^5[1-5][0-9]{14}$/',
  76. '/^2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12})$/',
  77. ),
  78. // All Visa card numbers start with a 4 and have a length of 13, 16, or 19 digits.
  79. 'VISA' => array(
  80. '/^4([0-9]{12}|[0-9]{15}|[0-9]{18})$/',
  81. ),
  82. );
  83. /**
  84. * Validates a creditcard belongs to a specified scheme.
  85. *
  86. * @param mixed $value
  87. * @param Constraint $constraint
  88. */
  89. public function validate($value, Constraint $constraint)
  90. {
  91. if (!$constraint instanceof CardScheme) {
  92. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\CardScheme');
  93. }
  94. if (null === $value || '' === $value) {
  95. return;
  96. }
  97. if (!is_numeric($value)) {
  98. if ($this->context instanceof ExecutionContextInterface) {
  99. $this->context->buildViolation($constraint->message)
  100. ->setParameter('{{ value }}', $this->formatValue($value))
  101. ->setCode(CardScheme::NOT_NUMERIC_ERROR)
  102. ->addViolation();
  103. } else {
  104. $this->buildViolation($constraint->message)
  105. ->setParameter('{{ value }}', $this->formatValue($value))
  106. ->setCode(CardScheme::NOT_NUMERIC_ERROR)
  107. ->addViolation();
  108. }
  109. return;
  110. }
  111. $schemes = array_flip((array) $constraint->schemes);
  112. $schemeRegexes = array_intersect_key($this->schemes, $schemes);
  113. foreach ($schemeRegexes as $regexes) {
  114. foreach ($regexes as $regex) {
  115. if (preg_match($regex, $value)) {
  116. return;
  117. }
  118. }
  119. }
  120. if ($this->context instanceof ExecutionContextInterface) {
  121. $this->context->buildViolation($constraint->message)
  122. ->setParameter('{{ value }}', $this->formatValue($value))
  123. ->setCode(CardScheme::INVALID_FORMAT_ERROR)
  124. ->addViolation();
  125. } else {
  126. $this->buildViolation($constraint->message)
  127. ->setParameter('{{ value }}', $this->formatValue($value))
  128. ->setCode(CardScheme::INVALID_FORMAT_ERROR)
  129. ->addViolation();
  130. }
  131. }
  132. }