Skip to content

Instantly share code, notes, and snippets.

@paulund
Last active October 13, 2015 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulund/4135141 to your computer and use it in GitHub Desktop.
Save paulund/4135141 to your computer and use it in GitHub Desktop.
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