Skip to content

Commit

Permalink
Merge pull request #476 from barspi/8-digit-bin
Browse files Browse the repository at this point in the history
Adding getBin(len) to Card and CardHolder (support 8-digit-bin)
  • Loading branch information
ar authored Mar 16, 2022
2 parents c573536 + dd7364c commit b8d9d75
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 18 deletions.
15 changes: 14 additions & 1 deletion jpos/src/main/java/org/jpos/core/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,21 @@ public boolean hasBothTracks() {
return hasTrack1() && hasTrack2();
}

/**
* Returns the traditional 6-digit BIN from the PAN
* @return the first 6 digits of the PAN
*/
public String getBin () {
return pan.substring(0, BINLEN);
return getBin(BINLEN);
}

/**
* Returns the first <code>len</code> digits from the PAN.
* Can be used for the newer 8-digit BINs, or some arbitrary length.
* @return the first <code>len</code> digits of the PAN
*/
public String getBin (int len) {
return pan.substring(0, len);
}

@Override
Expand Down
42 changes: 26 additions & 16 deletions jpos/src/main/java/org/jpos/core/CardHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public CardHolder() {
* @param track2 cards track2
* @exception InvalidCardException
*/
public CardHolder (String track2)
public CardHolder (String track2)
throws InvalidCardException
{
super();
Expand All @@ -90,7 +90,7 @@ public CardHolder (String track2)
* creates a new CardHolder based on pan and exp
* @exception InvalidCardException
*/
public CardHolder (String pan, String exp)
public CardHolder (String pan, String exp)
throws InvalidCardException
{
super();
Expand Down Expand Up @@ -130,7 +130,7 @@ else if (m.hasField(2)) {
* @param s a valid track2
* @exception InvalidCardException
*/
public void parseTrack2 (String s)
public void parseTrack2 (String s)
throws InvalidCardException
{
if (s == null)
Expand All @@ -140,7 +140,7 @@ public void parseTrack2 (String s)
pan = s.substring(0, separatorIndex);
exp = s.substring(separatorIndex+1, separatorIndex+1+4);
trailer = s.substring(separatorIndex+1+4);
} else
} else
throw new InvalidCardException ("Invalid track2 format");
}

Expand Down Expand Up @@ -171,7 +171,7 @@ public boolean hasTrack1() {
public String getNameOnCard() {
String name = null;
if (track1!=null) {
StringTokenizer st =
StringTokenizer st =
new StringTokenizer(track1, TRACK1_SEPARATOR);
if (st.countTokens()<2)
return null;
Expand Down Expand Up @@ -247,9 +247,9 @@ public void setTrailer(String trailer) {
* @param pan Primary Account NUmber
* @exception InvalidCardException
*/
public void setPAN (String pan)
public void setPAN (String pan)
throws InvalidCardException
{
{
if (pan.length() < MINPANLEN)
throw new InvalidCardException ("PAN length smaller than min required");
this.pan = pan;
Expand All @@ -258,26 +258,36 @@ public void setPAN (String pan)
/**
* @return Primary Account Number
*/
public String getPAN () {
public String getPAN () {
return pan;
}


/**
* Get the first <code>len</code> digits from the PAN.
* Can be used for the newer 8-digit BINs, or some arbitrary length.
* @return <code>len</code>-digit bank issuer number
*/
public String getBIN (int len) {
return pan.substring(0, len);
}

/**
* Get Bank Issuer Number
* @return bank issuer number
* Get the traditional 6-digit BIN (Bank Issuer Number) from the PAN
* @return 6-digit bank issuer number
*/
public String getBIN () {
return pan.substring(0, BINLEN);
public String getBIN () {
return getBIN(BINLEN);
}

/**
* Set Expiration Date
* @param exp card expiration date
* @exception InvalidCardException
*/
public void setEXP (String exp)
public void setEXP (String exp)
throws InvalidCardException
{
{
if (exp.length() != 4)
throw new InvalidCardException ("Invalid Exp length, must be 4");
this.exp = exp;
Expand All @@ -287,7 +297,7 @@ public void setEXP (String exp)
* Get Expiration Date
* @return card expiration date
*/
public String getEXP () {
public String getEXP () {
return exp;
}

Expand Down Expand Up @@ -328,7 +338,7 @@ public static boolean isValidCRC (String p) {
int i, crc;

int odd = p.length() % 2;

for (i=crc=0; i<p.length(); i++) {
char c = p.charAt(i);
if (!Character.isDigit (c))
Expand Down
8 changes: 8 additions & 0 deletions jpos/src/test/java/org/jpos/core/CardHolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ public void testGetBIN() throws Throwable {
assertEquals("testCa", result, "result");
}

@Test
public void testGet8DigitBIN() throws Throwable {
CardHolder cardHolder = new CardHolder();
cardHolder.setPAN("testCardHolderPan");
String result = cardHolder.getBIN(8);
assertEquals("testCard", result, "result");
}

@Test
public void testGetBINThrowsNullPointerException() throws Throwable {
try {
Expand Down
28 changes: 27 additions & 1 deletion jpos/src/test/java/org/jpos/core/CardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testShortTrack2() throws Throwable {

assertEquals("4111111111111111", t2.getPan(), "pan");
}

@Test
public void testTrack1() throws Throwable {
Track1 t1 = Track1.builder()
Expand Down Expand Up @@ -157,4 +157,30 @@ public void testShortPan() throws Throwable {
assertEquals("4561", t2.getCvv(), "cvv");
assertEquals("23456789012", t2.getDiscretionaryData(), "discretionaryData");
}

@Test
public void test6DigitBIN() throws Throwable {
Card c = Card.builder()
.pan("4111111111111111")
.exp("4011")
.cvv("123")
.cvv2("4567")
.serviceCode("101")
.build();

assertEquals("411111", c.getBin(), "pan");
}

@Test
public void test8DigitBIN() throws Throwable {
Card c = Card.builder()
.pan("4111111111111111")
.exp("4011")
.cvv("123")
.cvv2("4567")
.serviceCode("101")
.build();

assertEquals("41111111", c.getBin(8), "pan");
}
}

0 comments on commit b8d9d75

Please sign in to comment.