forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request geekcomputers#340 from the-vishal/master
Added credit card validator script
- Loading branch information
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#luhn algorithm | ||
|
||
class CreditCard: | ||
def __init__(self,card_no): | ||
self.card_no = card_no | ||
|
||
|
||
@property | ||
def company(self): | ||
comp =None | ||
if str(self.card_no).startswith('4'): | ||
comp = 'Visa Card' | ||
elif str(self.card_no).startswith('5'): | ||
comp = 'Master Card' | ||
elif str(self.card_no).startswith('37'): | ||
comp = 'American Express Card' | ||
elif str(self.card_no).startswith('6'): | ||
comp = 'Discover Card' | ||
elif str(self.card_no).startswith('35'): | ||
comp = 'JCB Card' | ||
elif str(self.card_no).startswith('50' or '67'or '58'or'63'): | ||
comp = 'Maestro Card' | ||
elif str(self.card_no).startswith('7'): | ||
comp = 'Gasoline Card' | ||
|
||
return 'Company : '+comp | ||
|
||
|
||
|
||
def first_check(self): | ||
if 13<=len(self.card_no)<=19: | ||
message = "First check : Valid in terms of length." | ||
|
||
else: | ||
message = "First chek : Check Card number once again it must be of 13 or 16 digit long." | ||
return message | ||
|
||
|
||
|
||
|
||
def validate(self): | ||
#double every second digit from right to left | ||
sum_=0 | ||
crd_no = self.card_no[::-1] | ||
for i in range(len(crd_no)): | ||
if i%2==1: | ||
double_it = int(crd_no[i])*2 | ||
|
||
if len(str(double_it))==2: | ||
sum_ += sum([eval(i) for i in str(double_it)]) | ||
|
||
else: | ||
sum_+=double_it | ||
|
||
else: | ||
sum_+=int(crd_no[i]) | ||
|
||
|
||
|
||
if sum_%10==0: | ||
response = "Valid Card" | ||
else: | ||
response = 'Invalid Card' | ||
|
||
return response | ||
|
||
|
||
|
||
@property | ||
def checksum(self): | ||
return '#CHECKSUM# : '+self.card_no[-1] | ||
|
||
|
||
@classmethod | ||
def set_card(cls,card_to_check): | ||
return cls(card_to_check) | ||
|
||
|
||
|
||
card_number = input() | ||
card = CreditCard.set_card(card_number) | ||
print(card.company) | ||
print('Card : ',card.card_no) | ||
print(card.first_check()) | ||
print(card.checksum) | ||
print(card.validate()) | ||
|
||
|
||
|
||
# 79927398713 | ||
#4388576018402626 | ||
#379354508162306 | ||
|