Credit card validation using Codeigniter


/ Published in: PHP
Save to your folder(s)

Validate the credit card number and cvv code


Copy this code and paste it in your HTML
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class cc_validation {
  4.  
  5. public function validateCreditcard_number($cc_num)
  6. {
  7. $credit_card_number = $this->sanitize($cc_num);
  8. // Get the first digit
  9. $data = array();
  10. $firstnumber = substr($credit_card_number, 0, 1);
  11. // Make sure it is the correct amount of digits. Account for dashes being present.
  12. switch ($firstnumber)
  13. {
  14. case 3:
  15. $data['card_type'] ="American Express";
  16. if (!preg_match('/^3\d{3}[ \-]?\d{6}[ \-]?\d{5}$/', $credit_card_number))
  17. {
  18. //return 'This is not a valid American Express card number';
  19. $data['status']='false';
  20. return $data;
  21. }
  22. break;
  23. case 4:
  24. $data['card_type'] ="Visa";
  25. if (!preg_match('/^4\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $credit_card_number))
  26. {
  27. //return 'This is not a valid Visa card number';
  28. $data['status']='false';
  29. return $data;
  30. }
  31. break;
  32. case 5:
  33. $data['card_type'] ="MasterCard";
  34. if (!preg_match('/^5\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $credit_card_number))
  35. {
  36. //return 'This is not a valid MasterCard card number';
  37. $data['status']='false';
  38. return $data;
  39. }
  40. break;
  41. case 6:
  42. $data['card_type'] ="Discover";
  43. if (!preg_match('/^6011[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $credit_card_number))
  44. {
  45. //return 'This is not a valid Discover card number';
  46. $data['status']='false';
  47. return $data;
  48. }
  49. break;
  50. default:
  51. //return 'This is not a valid credit card number';
  52. $data['card_type'] ="Invalid";
  53. $data['status']='false';
  54. return $data;
  55. }
  56. // Here's where we use the Luhn Algorithm
  57. $credit_card_number = str_replace('-', '', $credit_card_number);
  58. $map = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,0, 2, 4, 6, 8, 1, 3, 5, 7, 9);
  59. $sum = 0;
  60. $last = strlen($credit_card_number) - 1;
  61. for ($i = 0; $i <= $last; $i++)
  62. {
  63. $sum += $map[$credit_card_number[$last - $i] + ($i & 1) * 10];
  64. }
  65. if ($sum % 10 != 0)
  66. {
  67. //return 'This is not a valid credit card number';
  68. $data['status']='false';
  69. return $data;
  70. }
  71. // If we made it this far the credit card number is in a valid format
  72. $data['status']='true';
  73. return $data;
  74. }
  75. public function validateCreditCardExpirationDate($mon,$yr)
  76. {
  77. $month = $this->sanitize($mon);
  78. $year = $this->sanitize($yr);
  79. if (!preg_match('/^\d{1,2}$/', $month))
  80. {
  81. return 'false'; // The month isn't a one or two digit number
  82. }
  83. else if (!preg_match('/^\d{4}$/', $year))
  84. {
  85. return 'false'; // The year isn't four digits long
  86. }
  87. else if ($year < date("Y"))
  88. {
  89. return 'false'; // The card is already expired
  90. }
  91. else if ($month < date("m") && $year == date("Y"))
  92. {
  93. return 'false'; // The card is already expired
  94. }
  95. return 'true';
  96. }
  97. public function validateCVV($cc_num, $cc_cvv)
  98. {
  99. $cardNumber = $this->sanitize($cc_num);
  100. $cvv = $this->sanitize($cc_cvv);
  101. // Get the first number of the credit card so we know how many digits to look for
  102. $firstnumber = (int) substr($cardNumber, 0, 1);
  103. if ($firstnumber === 3)
  104. {
  105. if (!preg_match("/^\d{4}$/", $cvv))
  106. {
  107. // The credit card is an American Express card but does not have a four digit CVV code
  108. return 'false';
  109. }
  110. }
  111. else if (!preg_match("/^\d{3}$/", $cvv))
  112. {
  113. // The credit card is a Visa, MasterCard, or Discover Card card but does not have a three digit CVV code
  114. return 'false';
  115. }
  116. return 'true';
  117. }
  118. function sanitize($value)
  119. {
  120. return trim(strip_tags($value));
  121. }
  122.  
  123. }
  124. /* End of file My_payment.php */
  125. /* Location: ./system/application/libraries/My_payment.php */

URL: http://michaeljaycatubay.wordpress.com/2011/06/21/credit-card-validator-codeigniter-library/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.