tobias / credit_card_validator

A ruby gem for validating credit cards (a port of Thomas Fuchs' creditcard_js)
55 stars 14 forks source link

= credit_card_validator

The latest version is 1.3.2, available at http://rubygems.org/gems/credit_card_validator.

== DESCRIPTION:

A gem that provides credit card validation. It is basically a ruby port of the javascript credit card validator by Thomas Fuchs (madrobby) (http://github.com/madrobby/creditcard_js).

== SYNOPSIS:

Usage: CreditCardValidator::Validator.valid?('1111 2222 3333 4444')

allow test numbers to be valid (for development)

CreditCardValidator::Validator.options[:test_numbers_are_valid] = true CreditCardValidator::Validator.valid?('1111 2222 3333 4444')

limit the card types you allow

CreditCardValidator::Validator.options[:allowed_card_types] = [:visa, :master_card] CreditCardValidator::Validator.valid?('1111 2222 3333 4444')

Supported card types: :amex, :discover, :diners_club, :master_card, :visa, :maestro, :jcb

Whitespace is stripped from the number automatically.

The following things are tested:

  1. does the luhn validation code add up? (see http://en.wikipedia.org/wiki/Luhn_algorithm)
  2. does the number range and length seem right? (see http://en.wikipedia.org/wiki/Bank_card_number)
  3. is it one of several well-known test numbers?

Note: this only validates that the number is of a valid format, it does not check if it is an actual credit card number. You will need to talk to your payment gateway to learn that.

You can also use the validator to learn about the type of the card:

gives the type back as a string (visa, master_card, etc)

CreditCardValidator::Validator.card_type(number)

CreditCardValidator::Validator.is_visa?(number) CreditCardValidator::Validator.is_master_card?(number)

etc. - works for all of the supported card types

CreditCardValidator::Validator.is_allowed_card_type?(number)

== Command Line:

The gem comes with a basic command line application that allows you to do validation in a variety of ways. The utility produces tab delimited output for easy processing by other utilities.

$ credit_card_validator --help Usage: credit_card_validator [options] -v, --verboseverbose Be more verbose. -h, --help Request help. -a=s Comma Delimited List of allowed card types (default is 'amex,diners_club,discover,maestro,master_card,visa') --allowed-card-types -t --test-numbers-are-validtest-numbers-are-valid Allow test Credit Card Numbers to be considred valid.

You can validate a number directly from the command line:

$ credit_card_validator 5105105105105100 card_number valid? 5105105105105100 false

Verbose mode will emit more detailed information:

$ credit_card_validator -v 5105105105105100 credit_card_validator Parameters: verbose : true help : allowed-card-types : amex,diners_club,discover,maestro,master_card,visa test-numbers-are-valid :

card_number is_allowed_card_type? luhn_valid? card_type is_test_number? valid? 5105105105105100 true true master_card true false

The option summary is printed to $stderr and is easy to suppress:

$ credit_card_validator -v 5105105105105100 2>/dev/null card_number is_allowed_card_type? luhn_valid? card_type is_test_number? valid? 5105105105105100 true true master_card true false

You may also validate a file full of numbers (one per line):

$ cat nums.tab 5105105105105100 4012888888881882 $ credit_card_validator -v nums.tab 2>/dev/null card_number is_allowed_card_type? luhn_valid? card_type is_test_number? valid? 5105105105105100 true true master_card true false 4012888888881882 true false visa false false

Even from standard input:

$ cat nums.tab | credit_card_validator -v 2>/dev/null card_number is_allowed_card_type? luhn_valid? card_type is_test_number? valid? 5105105105105100 true true master_card true false 4012888888881882 true false visa false false

== INSTALL:

== LICENSE:

Copyright (c) 2009-2013 Tobias Crawley & Bruce Hauman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.