Last active
October 13, 2015 03:57
-
-
Save paulund/4135141 to your computer and use it in GitHub Desktop.
Check if card number passes Luhn Check
This file contains hidden or 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
/** | |
* Determines whether the supplied PAN is valid. Will also check to see whether the PAN is numeric. | |
* @private | |
* @param pan | |
* The PAN to validate. | |
* @return If the PAN failed the Luhn Check of if the PAN is not numeric. | |
*/ | |
public function isValidLuhn($pan) { | |
if (!ctype_digit((string)$pan)) { | |
return false; | |
} | |
$nSum = 0; | |
for ($nPos = strlen($pan) - 1, $nMultiple = 0; $nPos >= 0; $nPos--, $nMultiple ^= 1) { | |
$nProduct = $pan{$nPos} * ($nMultiple + 1); | |
$nSum += (($nProduct > 9) ? $nProduct - 9 : $nProduct); | |
} | |
return (($nSum % 10) === 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment