code_junkie

What is the best way to validate a credit card in PHP?

Nov 11th, 2011
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. <?
  2. /* Luhn algorithm number checker - (c) 2005-2008 shaman - www.planzero.org *
  3. * This code has been released into the public domain, however please *
  4. * give credit to the original author where possible. */
  5.  
  6. function luhn_check($number) {
  7.  
  8. // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
  9. $number=preg_replace('/D/', '', $number);
  10.  
  11. // Set the string length and parity
  12. $number_length=strlen($number);
  13. $parity=$number_length % 2;
  14.  
  15. // Loop through each digit and do the maths
  16. $total=0;
  17. for ($i=0; $i<$number_length; $i++) {
  18. $digit=$number[$i];
  19. // Multiply alternate digits by two
  20. if ($i % 2 == $parity) {
  21. $digit*=2;
  22. // If the sum is two digits, add them together (in effect)
  23. if ($digit > 9) {
  24. $digit-=9;
  25. }
  26. }
  27. // Total up the digits
  28. $total+=$digit;
  29. }
  30.  
  31. // If the total mod 10 equals 0, the number is valid
  32. return ($total % 10 == 0) ? TRUE : FALSE;
  33.  
  34. }
  35. ?>
  36.  
  37. function check_cc($cc, $extra_check = false){
  38. $cards = array(
  39. "visa" => "(4d{12}(?:d{3})?)",
  40. "amex" => "(3[47]d{13})",
  41. "jcb" => "(35[2-8][89]ddd{10})",
  42. "maestro" => "((?:5020|5038|6304|6579|6761)d{12}(?:dd)?)",
  43. "solo" => "((?:6334|6767)d{12}(?:dd)?d?)",
  44. "mastercard" => "(5[1-5]d{14})",
  45. "switch" => "(?:(?:(?:4903|4905|4911|4936|6333|6759)d{12})|(?:(?:564182|633110)d{10})(dd)?d?)",
  46. );
  47. $names = array("Visa", "American Express", "JCB", "Maestro", "Solo", "Mastercard", "Switch");
  48. $matches = array();
  49. $pattern = "#^(?:".implode("|", $cards).")$#";
  50. $result = preg_match($pattern, str_replace(" ", "", $cc), $matches);
  51. if($extra_check && $result > 0){
  52. $result = (validatecard($cc))?1:0;
  53. }
  54. return ($result>0)?$names[sizeof($matches)-2]:false;
  55. }
  56.  
  57. $cards = array(
  58. "4111 1111 1111 1111",
  59. );
  60.  
  61. foreach($cards as $c){
  62. $check = check_cc($c, true);
  63. if($check!==false)
  64. echo $c." - ".$check;
  65. else
  66. echo "$c - Not a match";
  67. echo "<br/>";
  68. }
  69.  
  70. 4111 1111 1111 1111 - Visa
  71.  
  72. function validateCC($cc_num, $type) {
  73.  
  74. if($type == "American") {
  75. $denum = "American Express";
  76. } elseif($type == "Dinners") {
  77. $denum = "Diner's Club";
  78. } elseif($type == "Discover") {
  79. $denum = "Discover";
  80. } elseif($type == "Master") {
  81. $denum = "Master Card";
  82. } elseif($type == "Visa") {
  83. $denum = "Visa";
  84. }
  85.  
  86. if($type == "American") {
  87. $pattern = "/^([34|37]{2})([0-9]{13})$/";//American Express
  88. if (preg_match($pattern,$cc_num)) {
  89. $verified = true;
  90. } else {
  91. $verified = false;
  92. }
  93.  
  94.  
  95. } elseif($type == "Dinners") {
  96. $pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club
  97. if (preg_match($pattern,$cc_num)) {
  98. $verified = true;
  99. } else {
  100. $verified = false;
  101. }
  102.  
  103.  
  104. } elseif($type == "Discover") {
  105. $pattern = "/^([6011]{4})([0-9]{12})$/";//Discover Card
  106. if (preg_match($pattern,$cc_num)) {
  107. $verified = true;
  108. } else {
  109. $verified = false;
  110. }
  111.  
  112.  
  113. } elseif($type == "Master") {
  114. $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/";//Mastercard
  115. if (preg_match($pattern,$cc_num)) {
  116. $verified = true;
  117. } else {
  118. $verified = false;
  119. }
  120.  
  121.  
  122. } elseif($type == "Visa") {
  123. $pattern = "/^([4]{1})([0-9]{12,15})$/";//Visa
  124. if (preg_match($pattern,$cc_num)) {
  125. $verified = true;
  126. } else {
  127. $verified = false;
  128. }
  129.  
  130. }
  131.  
  132. if($verified == false) {
  133. //Do something here in case the validation fails
  134. echo "Credit card invalid. Please make sure that you entered a valid <em>" . $denum . "</em> credit card ";
  135.  
  136. } else { //if it will pass...do something
  137. echo "Your <em>" . $denum . "</em> credit card is valid";
  138. }
  139.  
  140.  
  141. }
  142.  
  143. echo validateCC("1738292928284637", "Dinners");
Add Comment
Please, Sign In to add comment