# Copyright (c) 2000-2010 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: ECCreditCardNumber.pm,v 2.4 2010/08/02 17:27:51 nagler Exp $ package Bivio::Type::ECCreditCardNumber; use strict; use Bivio::Base 'Type.Secret'; our($VERSION) = sprintf('%d.%02d', q$Revision: 2.4 $ =~ /\d+/g); my($_TE) = b_use('Bivio.TypeError'); sub TEST_NUMBER { return '4222 2222 2222 2'; } sub from_literal { my($proto, $value) = @_; my($err); ($value, $err) = $proto->SUPER::from_literal($value); return ($value, $err) if $err; return undef unless defined($value); $value =~ s/[-\s]+//g; return $proto->luhn_mod10($value) ? $value : (undef, $_TE->CREDITCARD_INVALID_NUMBER); } sub get_width { return 19; } sub luhn_mod10 { my(undef, $number) = @_; return 0 unless defined($number); my($len) = length($number); return 0 if $len < 12 || $len > 19 || $number =~ /\D/; my($sum) = 0; my($mul) = 1; my(@digits) = split('', $number); for (my $i = $len-1; $i >= 0; $i--) { $a = $digits[$i] * $mul; $sum += $a % 10 + ($a > 9 ? 1 : 0); $mul = $mul == 1 ? 2 : 1; } return ($sum % 10) == 0 ? 1 : 0; } 1;