/ Published in: PHP
Validate the credit card number and cvv code
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class cc_validation { public function validateCreditcard_number($cc_num) { $credit_card_number = $this->sanitize($cc_num); // Get the first digit // Make sure it is the correct amount of digits. Account for dashes being present. switch ($firstnumber) { case 3: $data['card_type'] ="American Express"; { //return 'This is not a valid American Express card number'; $data['status']='false'; return $data; } break; case 4: $data['card_type'] ="Visa"; { //return 'This is not a valid Visa card number'; $data['status']='false'; return $data; } break; case 5: $data['card_type'] ="MasterCard"; { //return 'This is not a valid MasterCard card number'; $data['status']='false'; return $data; } break; case 6: $data['card_type'] ="Discover"; { //return 'This is not a valid Discover card number'; $data['status']='false'; return $data; } break; default: //return 'This is not a valid credit card number'; $data['card_type'] ="Invalid"; $data['status']='false'; return $data; } // Here's where we use the Luhn Algorithm $sum = 0; for ($i = 0; $i <= $last; $i++) { $sum += $map[$credit_card_number[$last - $i] + ($i & 1) * 10]; } if ($sum % 10 != 0) { //return 'This is not a valid credit card number'; $data['status']='false'; return $data; } // If we made it this far the credit card number is in a valid format $data['status']='true'; return $data; } public function validateCreditCardExpirationDate($mon,$yr) { $month = $this->sanitize($mon); $year = $this->sanitize($yr); { return 'false'; // The month isn't a one or two digit number } { return 'false'; // The year isn't four digits long } { return 'false'; // The card is already expired } { return 'false'; // The card is already expired } return 'true'; } public function validateCVV($cc_num, $cc_cvv) { $cardNumber = $this->sanitize($cc_num); $cvv = $this->sanitize($cc_cvv); // Get the first number of the credit card so we know how many digits to look for if ($firstnumber === 3) { { // The credit card is an American Express card but does not have a four digit CVV code return 'false'; } } { // The credit card is a Visa, MasterCard, or Discover Card card but does not have a three digit CVV code return 'false'; } return 'true'; } function sanitize($value) { } } /* End of file My_payment.php */ /* Location: ./system/application/libraries/My_payment.php */
URL: http://michaeljaycatubay.wordpress.com/2011/06/21/credit-card-validator-codeigniter-library/