From dd7364ca177485a8349a52492f63727852113b68 Mon Sep 17 00:00:00 2001 From: Barzilai Spinak Date: Mon, 14 Mar 2022 23:58:12 -0300 Subject: [PATCH] Adding getBin(len) to Card and CardHolder (support 8-digit-bin) --- jpos/src/main/java/org/jpos/core/Card.java | 15 ++++++- .../main/java/org/jpos/core/CardHolder.java | 42 ++++++++++++------- .../java/org/jpos/core/CardHolderTest.java | 8 ++++ .../src/test/java/org/jpos/core/CardTest.java | 28 ++++++++++++- 4 files changed, 75 insertions(+), 18 deletions(-) diff --git a/jpos/src/main/java/org/jpos/core/Card.java b/jpos/src/main/java/org/jpos/core/Card.java index 5c1da1a4a1..fa920d4ad7 100644 --- a/jpos/src/main/java/org/jpos/core/Card.java +++ b/jpos/src/main/java/org/jpos/core/Card.java @@ -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 len digits from the PAN. + * Can be used for the newer 8-digit BINs, or some arbitrary length. + * @return the first len digits of the PAN + */ + public String getBin (int len) { + return pan.substring(0, len); } @Override diff --git a/jpos/src/main/java/org/jpos/core/CardHolder.java b/jpos/src/main/java/org/jpos/core/CardHolder.java index b821ebadb7..fd82b6b43f 100644 --- a/jpos/src/main/java/org/jpos/core/CardHolder.java +++ b/jpos/src/main/java/org/jpos/core/CardHolder.java @@ -79,7 +79,7 @@ public CardHolder() { * @param track2 cards track2 * @exception InvalidCardException */ - public CardHolder (String track2) + public CardHolder (String track2) throws InvalidCardException { super(); @@ -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(); @@ -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) @@ -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"); } @@ -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; @@ -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; @@ -258,16 +258,26 @@ public void setPAN (String pan) /** * @return Primary Account Number */ - public String getPAN () { + public String getPAN () { return pan; } + + /** + * Get the first len digits from the PAN. + * Can be used for the newer 8-digit BINs, or some arbitrary length. + * @return len-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); } /** @@ -275,9 +285,9 @@ public String getBIN () { * @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; @@ -287,7 +297,7 @@ public void setEXP (String exp) * Get Expiration Date * @return card expiration date */ - public String getEXP () { + public String getEXP () { return exp; } @@ -328,7 +338,7 @@ public static boolean isValidCRC (String p) { int i, crc; int odd = p.length() % 2; - + for (i=crc=0; i