Skip to content

Commit 9e266f5

Browse files
Merge pull request geekcomputers#340 from the-vishal/master
Added credit card validator script
2 parents c2395a5 + d873017 commit 9e266f5

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

Credit_Card_Validator.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#luhn algorithm
2+
3+
class CreditCard:
4+
def __init__(self,card_no):
5+
self.card_no = card_no
6+
7+
8+
@property
9+
def company(self):
10+
comp =None
11+
if str(self.card_no).startswith('4'):
12+
comp = 'Visa Card'
13+
elif str(self.card_no).startswith('5'):
14+
comp = 'Master Card'
15+
elif str(self.card_no).startswith('37'):
16+
comp = 'American Express Card'
17+
elif str(self.card_no).startswith('6'):
18+
comp = 'Discover Card'
19+
elif str(self.card_no).startswith('35'):
20+
comp = 'JCB Card'
21+
elif str(self.card_no).startswith('50' or '67'or '58'or'63'):
22+
comp = 'Maestro Card'
23+
elif str(self.card_no).startswith('7'):
24+
comp = 'Gasoline Card'
25+
26+
return 'Company : '+comp
27+
28+
29+
30+
def first_check(self):
31+
if 13<=len(self.card_no)<=19:
32+
message = "First check : Valid in terms of length."
33+
34+
else:
35+
message = "First chek : Check Card number once again it must be of 13 or 16 digit long."
36+
return message
37+
38+
39+
40+
41+
def validate(self):
42+
#double every second digit from right to left
43+
sum_=0
44+
crd_no = self.card_no[::-1]
45+
for i in range(len(crd_no)):
46+
if i%2==1:
47+
double_it = int(crd_no[i])*2
48+
49+
if len(str(double_it))==2:
50+
sum_ += sum([eval(i) for i in str(double_it)])
51+
52+
else:
53+
sum_+=double_it
54+
55+
else:
56+
sum_+=int(crd_no[i])
57+
58+
59+
60+
if sum_%10==0:
61+
response = "Valid Card"
62+
else:
63+
response = 'Invalid Card'
64+
65+
return response
66+
67+
68+
69+
@property
70+
def checksum(self):
71+
return '#CHECKSUM# : '+self.card_no[-1]
72+
73+
74+
@classmethod
75+
def set_card(cls,card_to_check):
76+
return cls(card_to_check)
77+
78+
79+
80+
card_number = input()
81+
card = CreditCard.set_card(card_number)
82+
print(card.company)
83+
print('Card : ',card.card_no)
84+
print(card.first_check())
85+
print(card.checksum)
86+
print(card.validate())
87+
88+
89+
90+
# 79927398713
91+
#4388576018402626
92+
#379354508162306
93+

0 commit comments

Comments
 (0)