Skip to content

Commit 8f6e14d

Browse files
committed
Adding support for MasterCard series 2 BINs
1 parent e5ba7aa commit 8f6e14d

File tree

3 files changed

+174
-14
lines changed

3 files changed

+174
-14
lines changed

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<prerequisites>
7-
<maven>3.3.1</maven>
7+
<maven>3.3.9</maven>
88
</prerequisites>
99
<groupId>us.fatehi</groupId>
1010
<artifactId>credit_card_number</artifactId>
11-
<version>1.8</version>
11+
<version>1.9</version>
1212
<packaging>jar</packaging>
1313
<name>Credit Card Number</name>
1414
<description>Credit Card Number is a library that can provide details of a bank issued credit card number. All classes are immutable and thread-safe. The standard `toString()` function formats data in a readable form.</description>
@@ -66,7 +66,7 @@
6666
<dependency>
6767
<groupId>org.threeten</groupId>
6868
<artifactId>threetenbp</artifactId>
69-
<version>1.3</version>
69+
<version>1.3.1</version>
7070
</dependency>
7171
</dependencies>
7272
<properties>
@@ -109,7 +109,7 @@
109109
<plugin>
110110
<groupId>org.apache.maven.plugins</groupId>
111111
<artifactId>maven-source-plugin</artifactId>
112-
<version>2.2.1</version>
112+
<version>2.4</version>
113113
<executions>
114114
<execution>
115115
<id>attach-sources</id>
@@ -136,7 +136,7 @@
136136
<inherited>true</inherited>
137137
<groupId>org.apache.maven.plugins</groupId>
138138
<artifactId>maven-javadoc-plugin</artifactId>
139-
<version>2.9.1</version>
139+
<version>2.10.3</version>
140140
<executions>
141141
<execution>
142142
<id>attach-javadocs</id>
@@ -161,7 +161,7 @@
161161
<plugin>
162162
<groupId>org.apache.maven.plugins</groupId>
163163
<artifactId>maven-gpg-plugin</artifactId>
164-
<version>1.4</version>
164+
<version>1.6</version>
165165
<configuration>
166166
<useAgent>true</useAgent>
167167
<skip>${skip.signing.artifacts}</skip>

src/main/java/us/fatehi/creditcardnumber/CardBrand.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,10 @@ public enum CardBrand
4040

4141
Unknown(Pattern.compile("^unknown$")),
4242
Visa(Pattern.compile("^4[0-9]{6,}$")),
43-
// MasterCard numbers start with the numbers 51 through 55, but this
44-
// will only detect MasterCard credit cards; there are other cards
45-
// issued using the MasterCard system that do not fall into this IIN
46-
// range.
47-
MasterCard(Pattern.compile("^5[1-5][0-9]{5,}$")),
43+
// MasterCard numbers start with the numbers 51 through 55, and 2221 through 2720.
44+
MasterCard(Pattern.compile("^5[1-5][0-9]{5,}$|^(222[1-9]|2[3-6][0-9][0-9]|27[0-1][0-9]|2720)[0-9]{12}$")),
4845
AmericanExpress(Pattern.compile("^3[47][0-9]{5,}$")),
4946
// Diners Club card numbers begin with 300 through 305, 36 or 38.
50-
// There are Diners Club cards that begin with 5 and have 16 digits.
51-
// These are a joint venture between Diners Club and MasterCard, and
52-
// should be processed like a MasterCard.
5347
DinersClub(Pattern.compile("^3(?:0[0-5]|[68][0-9])[0-9]{4,}$")),
5448
Discover(Pattern.compile("^6(?:011|5[0-9]{2})[0-9]{3,}$")),
5549
JCB(Pattern.compile("^(?:2131|1800|35[0-9]{3})[0-9]{3,}$")), ;
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
*
3+
* Credit Card Number
4+
* https://github.com/sualeh/credit_card_number
5+
* Copyright (c) 2014-2015, Sualeh Fatehi.
6+
*
7+
* This library is free software; you can redistribute it and/or modify it under the terms
8+
* of the GNU Lesser General Public License as published by the Free Software Foundation;
9+
* either version 2.1 of the License, or (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13+
* See the GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License along with this
16+
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17+
* Boston, MA 02111-1307, USA.
18+
*
19+
*/
20+
package us.fatehi.test.creditcardnumber;
21+
22+
23+
import static org.junit.Assert.assertTrue;
24+
25+
import org.junit.Test;
26+
27+
import us.fatehi.creditcardnumber.CardBrand;
28+
29+
public class CardBrandTest
30+
{
31+
32+
// https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
33+
@Test
34+
public void americanExpress()
35+
{
36+
final long[] longCardNumbers = {
37+
378282246310005L,
38+
371449635398431L,
39+
378734493671000L
40+
};
41+
for (final long longCardNumber: longCardNumbers)
42+
{
43+
test(longCardNumber, CardBrand.AmericanExpress);
44+
}
45+
}
46+
47+
// https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
48+
@Test
49+
public void dinersClub()
50+
{
51+
final long[] longCardNumbers = {
52+
30569309025904L,
53+
38520000023237L
54+
};
55+
for (final long longCardNumber: longCardNumbers)
56+
{
57+
test(longCardNumber, CardBrand.DinersClub);
58+
}
59+
}
60+
61+
// https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
62+
@Test
63+
public void discover()
64+
{
65+
final long[] longCardNumbers = {
66+
6011000990139424L,
67+
6011111111111117L
68+
};
69+
for (final long longCardNumber: longCardNumbers)
70+
{
71+
test(longCardNumber, CardBrand.Discover);
72+
}
73+
}
74+
75+
// https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
76+
@Test
77+
public void jcb()
78+
{
79+
final long[] longCardNumbers = {
80+
3530111333300000L,
81+
3566002020360505L
82+
};
83+
for (final long longCardNumber: longCardNumbers)
84+
{
85+
test(longCardNumber, CardBrand.JCB);
86+
}
87+
}
88+
89+
// https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
90+
@Test
91+
public void masterCard()
92+
{
93+
final long[] longCardNumbers = {
94+
5555555555554444L,
95+
5105105105105100L
96+
};
97+
for (final long longCardNumber: longCardNumbers)
98+
{
99+
test(longCardNumber, CardBrand.MasterCard);
100+
}
101+
}
102+
103+
@Test
104+
public void masterCard2Series_1negative()
105+
{
106+
final long longCardNumber = 2221000000000000L;
107+
for (long i = 1; i <= 10; i++)
108+
{
109+
test(longCardNumber - i, CardBrand.Unknown);
110+
}
111+
}
112+
113+
@Test
114+
public void masterCard2Series_1positive()
115+
{
116+
final long longCardNumber = 2221000000000000L;
117+
for (long i = 0; i <= 10; i++)
118+
{
119+
test(longCardNumber + i, CardBrand.MasterCard);
120+
}
121+
}
122+
123+
@Test
124+
public void masterCard2Series_2negative()
125+
{
126+
final long longCardNumber = 2720999999999999L;
127+
for (long i = 1; i <= 10; i++)
128+
{
129+
test(longCardNumber + i, CardBrand.Unknown);
130+
}
131+
}
132+
133+
@Test
134+
public void masterCard2Series_2positive()
135+
{
136+
final long longCardNumber = 2720999999999999L;
137+
for (long i = 0; i <= 10; i++)
138+
{
139+
test(longCardNumber - i, CardBrand.MasterCard);
140+
}
141+
}
142+
143+
// https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
144+
@Test
145+
public void visa()
146+
{
147+
final long[] longCardNumbers = {
148+
4111111111111111L,
149+
4012888888881881L,
150+
4222222222222L
151+
};
152+
for (final long longCardNumber: longCardNumbers)
153+
{
154+
test(longCardNumber, CardBrand.Visa);
155+
}
156+
}
157+
158+
private void test(final long longCardNumber,
159+
final CardBrand expectedCardBrand)
160+
{
161+
final String cardNumber = String.valueOf(longCardNumber);
162+
final CardBrand cardBrand = CardBrand.from(cardNumber);
163+
assertTrue(cardNumber, expectedCardBrand == cardBrand);
164+
}
165+
166+
}

0 commit comments

Comments
 (0)