Sunday, 8 December 2013

How to detect Credit card type based on number in Python?

If you have any credit card number then you can easily find that credit card number is which type of card like as (Visa card, master card, dinner card, JCB, AMX card etc...)

I am showing both ways to finding credit card type 

1) Manual calculation you can find it.
2) Using python code you can find it.

1) Manual calculation for credit card type

see below images, it will show how to find credit card type


 1) Using Python code for credit card type

from python code you can find as below.
Actually all cards having some fix prefix and length . according to that we can find which card is which type. see this link List of Issuer Identification Numbers.

for algorithm open this link  Card type Luhn algorithm

This is a  fix prefix and length of card number.

  VISA = [
        {length: [16], prefixes: ['4']}
    ]
    MASTERCARD = [
        {length: [16], prefixes: ['51', '52', '53', '54', '55']}
    ]
    ######## other brands ########
    AMEX = [
        {length: [15], prefixes: ['34', '37']}
    ]

    DINERS = [
        {length: [14], prefixes: ['300', '301', '302', '303', '304', '305', '36']},
    ]

    #There are Diners Club (North America) cards that begin with 5. These are a joint venture between Diners Club and MasterCard, and are processed like a MasterCard
    DINERS_US = [
        {length: [16], prefixes: ['54', '55']}
    ]

    DISCOVER = [
        {length: [16], prefixes: ['6011', '622126', '622127', '622128', '622129', '62213',
                                '62214', '62215', '62216', '62217', '62218', '62219',
                                '6222', '6223', '6224', '6225', '6226', '6227', '6228',
                                '62290', '62291', '622920', '622921', '622922', '622923',
                                '622924', '622925', '644', '645', '646', '647', '648',
                                '649', '65']}
    ]

    JCB = [
        {length: [16], prefixes: ['3528', '3529', '353', '354', '355', '356', '357', '358']}
    ]


    LASER = [
        {length: [16, 17, 18, 19], prefixes: ['6304', '6706', '6771', '6709']}
    ]

    MAESTRO = [
        {length: [12, 13, 14, 15, 16, 17, 18, 19], prefixes: ['5018', '5020', '5038', '6304', '6759', '6761', '6763']}
    ]

    SOLO = [
        {length: [16, 18, 19], prefixes: ['6334', '6767']}
    ]

    UNIONPAY = [
        {length: [16, 17, 18, 19], prefixes: ['620', '621', '623', '625', '626']}
    ]

and code is

def cardType(number):
    number = str(number)
    cardtype = ""
    if len(number) == 15:
        if number[:2] == "34" or number[:2] == "37":
            cardtype = "American Express"
    if len(number) == 13:
        if number[:1] == "4":
            cardtype = "Visa"
    if len(number) == 16:
        if number[:4] == "6011":
            cardtype = "Discover"
        if int(number[:2]) >= 51 and int(number[:2]) <= 55:
            cardtype = "Master Card"
        if number[:1] == "4":
            cardtype = "Visa"
        if number[:4] == "3528" or number[:4] == "3529":
            cardtype = "JCB"
        if int(number[:3]) >= 353 and int(number[:3]) <= 359:
            cardtype = "JCB"
    if len(number) == 14:
        if number[:2] == "36":
            cardtype = "DINERS"
        if int(number[:3]) >= 300 and int(number[:3]) <= 305:
            cardtype = "DINERS"
    return cardtype

print cardType("38507091291997")
 
Thanks guys 

No comments:

Post a Comment