# Copyright (c) 2000-2011 bivio Software, Inc. All rights reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: ECCreditCardType.pm,v 2.4 2011/06/06 20:44:49 moeller Exp $ package Bivio::Type::ECCreditCardType; use strict; use Bivio::Base 'Type.Enum'; our($VERSION) = sprintf('%d.%02d', q$Revision: 2.4 $ =~ /\d+/g); __PACKAGE__->compile([ UNKNOWN => [0], VISA => [1], MASTERCARD => [2, 'MasterCard'], AMEX => [3, 'Amex', 'American Express'], DISCOVER => [4], ]); b_use('IO.Config')->register({ supported_card_list => 'VISA MASTERCARD AMEX DISCOVER', }); my($_SUPPORTED_CARDS); sub get_by_number { # (proto, string) : Type.ECCreditCard # Given a card I, return its type Handles C as unknown. my($proto, $number) = @_; return $proto->UNKNOWN unless defined($number); $number =~ s/\s+//g; return $proto->UNKNOWN if $number =~ /\D/; my($len) = length($number); return $proto->VISA if ($len == 13 || $len == 16) && $number =~ /^4/; return $proto->MASTERCARD if $len == 16 && $number =~ /^5[1-5]/; return $proto->AMEX if $len == 15 && $number =~ /^3[47]/; return $proto->DISCOVER if $len == 16 && $number =~ /^6011/; return $proto->UNKNOWN; } sub handle_config { # (proto, hash) : undef # supported_card_list : string # # List of supported card enum names. # Defaults to 'VISA MASTERCARD AMEX DISCOVER'. my($proto, $cfg) = @_; # map of enum name values, ensures they are valid before adding $_SUPPORTED_CARDS = { map {$proto->from_name($_)->get_name => 1} split(' ', $cfg->{supported_card_list}), }; return; } sub is_supported_by_number { # (proto, string) : boolean # Returns true if CC is supported. my($self, $number) = @_; return $_SUPPORTED_CARDS->{$self->get_by_number($number)->get_name} ? 1 : 0; } 1;