CardSchemeValidator.php 5.0 KB

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