最近在RapidAPI上了一个信用卡文字提取API,需要输出卡类型。
def get_card_type(card_number):
# 根据BIN范围判断卡类型
bin_ranges = {
'Discover Card': [str(num) for num in range(622126, 622927)] + ['6011', '644', '649', '65'],
'MasterCard': [str(num) for num in range(2221, 2721)] + ['51', '52', '53', '54', '55'],
'JCB': [str(num) for num in range(3528, 3590)],
'Switch': ['4903', '4905', '4911', '4936', '5641', '82', '6331', '6333', '6759'],
'InstaPayment': ['636', '637', '638', '639'],
'Laser': ['6304', '6706', '6771', '6709'],
'Solo': ['6334', '6767'],
'Diners Club Carte Blanche': ['300', '301', '302', '303', '304', '305'],
'Diners Club enRoute': ['2014', '2149'],
'American Express': ['34', '37'],
'Diners Club International': ['36'],
'China UnionPay': ['62'],
'Maestro': ['50', '56', '57', '58'],
'Visa': ['4'],
'UATP': ['1']
}
# 将卡号的前六位转换为字符串
card_bin = card_number[:6]
# 检查卡号的前六位是否在任何已知的BIN范围内
for card_type, ranges in bin_ranges.items():
if any(card_bin.startswith(range) for range in ranges):
return card_type
# 如果卡号的前六位不在任何已知的BIN范围内,返回未知
return 'Unknown'
# 示例使用
card_number_example = "4111111111111111" # 示例Visa卡号
print(get_card_type(card_number_example)) # 输出: Visa