
The CardChecksum function returns 1 for valid cards and 0 for invalid cards, after performing a check using the Luhn algorithm which validates all major credit cards.
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
def CardChecksum(CardNum): | |
CardSum = 0 | |
NumDigits = len(CardNum) | |
OddOrEven = NumDigits & 1 | |
for DigCount in range(0, NumDigits): | |
Digit = int(CardNum[DigCount]) | |
if not ((DigCount & 1) ^ OddOrEven): | |
Digit = Digit * 2 | |
if Digit > 9: | |
Digit = Digit - 9 | |
CardSum = CardSum + Digit | |
return ((CardSum % 10) == 0) |
No comments:
Post a Comment