creditcard 1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/creditcard.rb +88 -0
- metadata +37 -0
data/lib/creditcard.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# These functions tell you whether a credit card number is
|
2
|
+
# self-consistent using known algorithms for credit card numbers.
|
3
|
+
# All non-integer values are removed from the string before parsing
|
4
|
+
# so that you don't have to worry about the format of the string.
|
5
|
+
#
|
6
|
+
# == Usage
|
7
|
+
# require "creditcard"
|
8
|
+
#
|
9
|
+
# "5276 4400 6542 1319".creditcard?
|
10
|
+
# => true
|
11
|
+
#
|
12
|
+
# puts 5276440065421319.creditcard_type
|
13
|
+
# => "mastercard"
|
14
|
+
#
|
15
|
+
# "5276440065421319".creditcard? "visa"
|
16
|
+
# => false
|
17
|
+
#
|
18
|
+
# "5276-4400-6542-1319".creditcard? "mastercard"
|
19
|
+
# => true
|
20
|
+
#
|
21
|
+
# Ported from the Perl version by Jon Orwant and Ivan Kohler
|
22
|
+
# http://search.cpan.org/~ivan/Business-CreditCard-0.28/CreditCard.pm
|
23
|
+
#
|
24
|
+
# Author:: Lucas Carlson (mailto:lucas@rufy.com)
|
25
|
+
# Copyright:: Copyright (c) 2005 Lucas Carlson
|
26
|
+
# License:: Distributes under the same terms as Ruby
|
27
|
+
|
28
|
+
module CreditCard
|
29
|
+
# Returns a string containing the type of card from the list of known information below.
|
30
|
+
# == Known card types
|
31
|
+
# *Card Type* *Prefix* *Length*
|
32
|
+
# mastercard 51-55 16
|
33
|
+
# visa 4 13, 16
|
34
|
+
# american_express 34, 37 15
|
35
|
+
# diners_club 300-305, 36, 38 14
|
36
|
+
# enroute 2014, 2149 15
|
37
|
+
# discover 6011 16
|
38
|
+
# jcb 3 16
|
39
|
+
# jcb 2131, 1800 15
|
40
|
+
# bankcard 5610, 56022[1-5] 16
|
41
|
+
# switch various 16,18,19
|
42
|
+
# solo 63, 6767 16,18,19
|
43
|
+
def creditcard_type
|
44
|
+
number = self.to_s.gsub(/[^\d]/, "")
|
45
|
+
|
46
|
+
return 'visa' if number =~ /^4\d{12}(\d{3})?$/
|
47
|
+
return 'mastercard' if number =~ /^5[1-5]\d{14}$/
|
48
|
+
return 'discover' if number =~ /^6011\d{12}$/
|
49
|
+
return 'american_express' if number =~ /^3[47]\d{13}$/
|
50
|
+
return 'diners_club' if number =~ /^3(0[0-5]|[68]\d)\d{11}$/
|
51
|
+
return 'enroute' if number =~ /^2(014|149)\d{11}$/
|
52
|
+
return 'jcb' if number =~ /^(3\d{4}|2131|1800)\d{11}$/
|
53
|
+
return 'bankcard' if number =~ /^56(10\d\d|022[1-5])\d{10}$/
|
54
|
+
return 'switch' if number =~ /^49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})\d{10}(\d{2,3})?$/ || number =~ /^564182\d{10}(\d{2,3})?$/ || number =~ /^6(3(33[0-4][0-9])|759[0-9]{2})\d{10}(\d{2,3})?$/
|
55
|
+
return 'solo' if number =~ /^6(3(34[5-9][0-9])|767[0-9]{2})\d{10}(\d{2,3})?$/
|
56
|
+
return 'unknown'
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns true if it validates. Optionally, you can pass a card type as an argument and make sure it is of the correct type.
|
60
|
+
# == References
|
61
|
+
# - http://perl.about.com/compute/perl/library/nosearch/P073000.htm
|
62
|
+
# - http://www.beachnet.com/~hstiles/cardtype.html
|
63
|
+
def creditcard?(type = nil)
|
64
|
+
number = self.to_s.gsub(/[^\d]/, "")
|
65
|
+
return false unless number.length >= 13
|
66
|
+
|
67
|
+
if type
|
68
|
+
return false unless creditcard_type == type
|
69
|
+
end
|
70
|
+
|
71
|
+
sum = 0
|
72
|
+
for i in 0..number.length
|
73
|
+
weight = number[-1*(i+2), 1].to_i * (2 - (i%2))
|
74
|
+
sum += (weight < 10) ? weight : weight - 9
|
75
|
+
end
|
76
|
+
|
77
|
+
return true if number[-1,1].to_i == (10 - sum%10)%10
|
78
|
+
return false
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class String#:nodoc:
|
83
|
+
include CreditCard
|
84
|
+
end
|
85
|
+
|
86
|
+
class Integer#:nodoc:
|
87
|
+
include CreditCard
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.3
|
3
|
+
specification_version: 1
|
4
|
+
name: creditcard
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: "1.0"
|
7
|
+
date: 2005-01-15
|
8
|
+
summary: These functions tell you whether a credit card number is self-consistent using known algorithms for credit card numbers.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: lucas@rufy.com
|
12
|
+
homepage: http://tech.rufy.com/
|
13
|
+
rubyforge_project:
|
14
|
+
description: "These functions tell you whether a credit card number is self-consistent using known algorithms for credit card numbers. All non-integer values are removed from the string before parsing so that you don't have to worry about the format of the string."
|
15
|
+
autorequire: creditcard
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
authors:
|
28
|
+
- Lucas Carlson
|
29
|
+
files:
|
30
|
+
- lib/creditcard.rb
|
31
|
+
test_files: []
|
32
|
+
rdoc_options: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
requirements: []
|
37
|
+
dependencies: []
|