Created
July 3, 2013 17:15
-
-
Save danilo04/5920654 to your computer and use it in GitHub Desktop.
Validacion credit card
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Luhn algorithm number checker - | |
* (c) 2005-2008 shaman - www.planzero.org * This code has been released | |
* into the public domain, however please * give credit to the original author | |
* where possible. | |
*/ | |
function _luhn_check($number) { | |
// Strip any non-digits (useful for credit card numbers with spaces | |
// and hyphens) | |
$number = preg_replace ( '/\D/', '', $number ); | |
// Set the string length and parity | |
$number_length = strlen ( $number ); | |
$parity = $number_length % 2; | |
// Loop through each digit and do the maths | |
$total = 0; | |
for($i = 0; $i < $number_length; $i ++) { | |
$digit = $number [$i]; | |
// Multiply alternate digits by two | |
if ($i % 2 == $parity) { | |
$digit *= 2; | |
// If the sum is two digits, add them together (in effect) | |
if ($digit > 9) { | |
$digit -= 9; | |
} | |
} | |
// Total up the digits | |
$total += $digit; | |
} | |
// If the total mod 10 equals 0, the number is valid | |
return ($total % 10 == 0) ? TRUE : FALSE; | |
} | |
function _check_cc($cc, $extra_check = false) { | |
$cards = array ( | |
"visa" => "(4\d{12}(?:\d{3})?)", | |
// "amex" => "(3[47]\d{13})", | |
// "jcb" => "(35[2-8][89]\d\d\d{10})", | |
// "maestro" => "((?:5020|5038|6304|6579|6761)\d{12}(?:\d\d)?)", | |
// "solo" => "((?:6334|6767)\d{12}(?:\d\d)?\d?)", | |
"mastercard" => "(5[1-5]\d{14})" | |
// "switch" => "(?:(?:(?:4903|4905|4911|4936|6333|6759) | |
// \d{12})|(?:(?:564182|633110)\d{10})(\d\d)?\d?)", | |
); | |
$names = array ( | |
"Visa", | |
/*"American Express", | |
"JCB", | |
"Maestro", | |
"Solo", */ | |
"Mastercard" | |
/*, "Switch"*/ | |
); | |
$matches = array (); | |
$pattern = "#^(?:" . implode ( "|", $cards ) . ")$#"; | |
$result = preg_match ( $pattern, str_replace ( " ", "", $cc ), $matches ); | |
if ($extra_check && $result > 0) { | |
$result = (validatecard ( $cc )) ? 1 : 0; | |
} | |
return ($result > 0) ? $names [sizeof ( $matches ) - 2] : false; | |
} | |
function _validate_cvv($card_number, $cvv) { | |
// Get the first number of the credit card so we know how many digits to | |
// look for | |
$firstnumber = ( int ) substr ( $card_number, 0, 1 ); | |
if ($firstnumber === 3) { | |
if (! preg_match ( "/^\d{4}$/", $cvv )) { | |
// The credit card is an American Express card but does not have a | |
// four digit CVV code | |
return false; | |
} | |
} else if (! preg_match ( "/^\d{3}$/", $cvv )) { | |
// 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 _validate_expiration_date($card_expiration) { | |
return preg_match ( '/^\d{2}\/\d{4}$/', $card_expiration ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment