Determine credit card type by number?

后端 未结 15 1948
旧巷少年郎
旧巷少年郎 2020-11-30 19:44

Can credit card type be determined solely from the credit card number?

Is this recommended or should we always ask client for the type of credit card they\'re using?

相关标签:
15条回答
  • 2020-11-30 20:07

    I have heard one good reason to make them pick (even though you can figure it out). So that they know the list of credit cards you accept.

    0 讨论(0)
  • 2020-11-30 20:12

    This implementation in Python should work for AmEx, Discover, Master Card, Visa:

    def cardType(number):
        number = str(number)
        cardtype = "Invalid"
        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"
        return cardtype
    
    0 讨论(0)
  • 2020-11-30 20:12

    If all the credit cards that you accept have the same properties then just let the user enter the card number and other properties (expiry date, CVV, etc). However, some card types require different fields to be entered (e.g. start date or issue number for UK Maestro cards). In those cases, you either have to have all fields, thereby confusing the user, or some Javascript to hide/show the relevant fields, again making the user experience a bit weird (fields disappearing/appearing, as they enter the credit card number). In those cases, I recommend asking for the card type first.

    0 讨论(0)
提交回复
热议问题