Приглашаем посетить
CSS (css.find-info.ru)

Checks Visa, Mastercard, and American Express credit card

############################################################################
#                                                                          #
# cc_validate()                     Version 2.0                            #
# Written by Craig Patchett         craig@patchett.com                     #
# Created 9/24/96                   Last Modified 3/21/97                  #
#                                                                          #
# Copyright 1997 Craig Patchett & Matthew Wright.  All Rights Reserved.    #
# This subroutine is part of The CGI/Perl Cookbook from John Wiley & Sons. #
# License to use this program or install it on a server (in original or    #
# modified form) is granted only to those who have purchased a copy of The #
# CGI/Perl Cookbook. (This notice must remain as part of the source code.) #
#                                                                          #
# Function:      Checks Visa, Mastercard, and American Express credit card #
#                numbers to make sure they follow bank guidelines for      #
#                credit card numbers.                                      #
#                                                                          #
# Usage:         &cc_validate($card_type, $card_num, $exp_date);           #
#                                                                          #
# Variables:     $card_type -- Card type                                   #
#                              Valid values: 'VS', 'MC', 'AX'              #
#                $card_num --  Card number. Non-numeric will be revoed     #
#                              Example: '1234-5678-9012-3456'              #
#                $exp_date --  Expiration date                             #
#                              Examples: '05/99', '5/99'                   #
#                                                                          #
# Returns:       0 if successful                                           #
#                1 if invalid card type                                    #
#                2 if invalid number of digits in card number              #
#                3 if if card has expired or expiration date is invalid    #
#                4 if card number is invalid                               #
#                                                                          #
# Uses Globals:  Sets $Error_Message with descriptive error message        #
#                                                                          #
# Files Created: None                                                      #
#                                                                          #
############################################################################


sub cc_validate {

    local($card_type, $card_num, $exp_date) = @_;
    local(%card_length) = ('VS', '13,15,16', 'MC', '16', 'AX', '15');
    local($old_card_num, $card_length, $total, $d);
    
    # Check to make sure the card type is valid
    
    $card_type =~ tr/a-z/A-Z/;
    if (!$card_length{$card_type}) {
        $Error_Message = "$card_type is not a valid card type.";
        return(1);
    }
    
    # Get rid of non-digits in card number and check for right number 
    # of digits
    
    $old_card_num = $card_num;
    $card_num =~ s/\D//g;
    $card_length = length($card_num);
    if (!($card_length{$card_type} =~ /(^|,)$card_length(,|$)/)) {
        $Error_Message = "Card number $old_card_num does not have the correct number of digits.";
        return(2);
    }
    
    # Check to see if the card has expired
    
    local($month_now, $year_now) = (localtime)[4,5];
    ++$month_now;
    if ($year_now < 50) { $year_now += 100 }
    $exp_date =~ m|(\d+)/(\d+)|;
    $exp_month = $1;
    $exp_year = ($2 < 50) ? $2 + 100 : $2;
    if ((($year_now == $exp_year) && ($month_now > $exp_month))
      || ($exp_year - $year_now < 0) || ($exp_year - $year_now > 10)) {

        $Error_Message = "$exp_date is an invalid expiration date.";
        return(3);
    }
    
    # Check to see if card number is valid (checks only to see if the card
    # number sequence is a valid sequence.
    
    while (length($card_num)) {
        $total += chop($card_num);
        $total += (($d = chop($card_num)) < 9) ? ($d * 2) % 9 : 9;
    }
    if ($total % 10) {
        $Error_Message = "$old_card_num is not a valid credit card number.";
        return(4);
    }
    
    # It passed all the tests!
    
    return(0);
}

1;