Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check if card number passes Luhn Check
/**
* 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