Python code to check either the card number provided is valid or not
can be used in validating the Credit-card while dealing with cards
# coding=utf-8
class Check_Credit_Card(object):
"""
Class to check either the Credit card number is valid or not
"""
cc_patterns = dict(amex={15: ('34', '37')},
carteblanche={14: ('300', '301', '302', '303', '304', '305', '36', '38')},
dinersclub={14: ('300', '301', '302', '303', '304', '305', '36', '38')}, discover={16: ('6011')},
enroute={(15): ('2014', '2149')}, jcb={15: ('2131', '1800'), (16): ('3')},
mastercard={(16): ('51', '52', '53', '54', '55')}, visa={(13, 16): ('4')})
def check(self,cc_num,cc_type):
"""
Main function to check the card
"""
cc_type = cc_type.strip().lower().replace(' ', '')
cc_num = str(cc_num)
if cc_type in self.cc_patterns.keys():
# Perform length check
valid_lengths = self.cc_patterns[cc_type].keys()
if len(cc_num) not in valid_lengths:
return (False, 'Invalid length (%s) for %s' % \
(str(valid_lengths).strip('[]'), cc_type))
# Perform prefix check
valid_prefixes = self.cc_patterns[cc_type][len(cc_num)]
prefix_match = False
for prefix in valid_prefixes:
prefix_length = len(prefix)
if cc_num[:prefix_length] == prefix:
prefix_match = True
if not prefix_match:
return (False, 'Invalid prefix (not in %s)' % \
','.join(valid_prefixes))
# Perform mod 10 check
if cc_type == 'enroute':
pass # Only cc_type that doesn't do a mod 10 check
else:
check_digits = []
is_even = False
for digit in cc_num:
if not is_even:
check_digits.append(int(digit))
else:
mult_digits = str(int(digit) * 2)
check_digits.append(mult_digits[0])
if len(mult_digits == 2):
check_digits.append(int(mult_digits[1]))
check_sum = sum(check_digits)
if check_sum % 10 != 0:
return False, 'Invalid check sum (%d)' % check_sum
return True, ''
else:
return False, 'Invalid card type (%s)' % cc_type
if __name__ == "__main__": #For testing you can Use the file directly in python console..
x = Check_Credit_Card()
x = x.check('5235235879456682','MASTERCARD')
print x[0]
#VIEWS.PY
def Check_Validity(request):
if request.method == 'POST':
Data = request.POST.copy()
cc_obj = Check_Credit_Card()
result = cc_obj.check(Data.get('card_no',''),Data.get('card_type',''))
return HttpResponse(simplejson.dumps({'result':result[0],'reason':result[1]}), mimetype="application/json")
return HttpResponse(simplejson.dumps({'result':'Not a valid method'}), mimetype="application/json")
#URLS.PY
url(r'^check_validity$',Check_Validity,name="Check_Validity_View")
28.459594
77.051530
Like this:
Like Loading...
I like this weblog it’s a master piece! Glad I detected this on google.
I appreciate, lead to I found exactly what I was having a look for.
You’ve ended my four day lengthy hunt! God Bless you man. Have a great day. Bye
Thanks Man.My pleasure anytime …God Bless You Too..
I’m not sure where you are getting your information, but great topic. I needs to spend some time learning more or understanding more. Thanks for wonderful information I was looking for this info for my mission.
I pay a quick visit each day some websites and websites to read articles, however
this weblog offers feature based articles.
If you would like to increase your experience just keep visiting this site and
be updated with the newest news posted here.