/** * Set Expiration Date * @param exp card expiration date * @exception InvalidCardException */ public void setEXP (String exp) throws InvalidCardException { if (exp.length() != 4) throw new InvalidCardException ("exp length not 4"); this.exp = exp; }
/** * Sets Primary Account Number * @param pan Primary Account NUmber * @exception InvalidCardException */ public void setPAN (String pan) throws InvalidCardException { if (pan.length() < MINPANLEN) throw new InvalidCardException ("pan too short"); this.pan = pan; }
/** * Verify Card's PAN * @param p full card PAN * @return true if pan LUHN's matches */ public boolean verify (String p) throws InvalidCardException { if (p == null || p.length() < 5) throw new InvalidCardException ("Invalid PAN " + p); return p.charAt(p.length()-1) == calculate (p.substring(0, p.length()-1)); } }
/** * extract pan/exp/trailler from track2 * @param s a valid track2 * @exception InvalidCardException */ public void parseTrack2 (String s) throws InvalidCardException { if (s == null) throw new InvalidCardException ("null track2 data"); int separatorIndex = s.replace ('D','=').indexOf(TRACK2_SEPARATOR); if (separatorIndex > 0 && s.length() > separatorIndex+4) { pan = s.substring(0, separatorIndex); exp = s.substring(separatorIndex+1, separatorIndex+1+4); trailer = s.substring(separatorIndex+1+4); } else throw new InvalidCardException ("invalid track2"); }
public Builder track (String s) throws InvalidCardException { if (s == null) throw new InvalidCardException ("null track2 data"); track = s; Matcher matcher = pattern.matcher(s); int cnt = matcher.groupCount(); if (matcher.find() && cnt >= 2) { pan = matcher.group(1); exp = matcher.group(2); if (cnt > 2) serviceCode = matcher.group(3); if (cnt > 3) cvv = matcher.group(4); if (cnt > 4) discretionaryData = matcher.group(5); } else { throw new InvalidCardException ("invalid track2"); } return this; } public Track2 build() {
public Builder track (String s) throws InvalidCardException { if (s == null) throw new InvalidCardException ("null track1 data"); track = s; Matcher matcher = pattern.matcher(s); int cnt = matcher.groupCount(); if (matcher.find() && cnt >= 2) { pan = matcher.group(1); nameOnCard = matcher.group(2); if (cnt > 2) exp = matcher.group(3); if (cnt > 3) serviceCode = matcher.group(4); if (cnt > 4) cvv = matcher.group(5); if (cnt > 5) discretionaryData = matcher.group(6); } else { throw new InvalidCardException ("invalid track1"); } return this; }
@Test public void testConstructor() throws Throwable { new InvalidCardException(); assertTrue("Test completed without Exception", true); }
@Test public void testConstructor1() throws Throwable { InvalidCardException invalidCardException = new InvalidCardException("testInvalidCardExceptions"); assertEquals("invalidCardException.getMessage()", "testInvalidCardExceptions", invalidCardException.getMessage()); } }
if (pan != null) { if (card.getTrack1() != null && !pan.equals(card.getTrack1().getPan())) throw new InvalidCardException ("track1 PAN mismatch"); if (card.getTrack2() != null && !pan.equals(card.getTrack2().getPan())) throw new InvalidCardException ("track2 PAN mismatch"); throw new InvalidCardException ("track1 EXP mismatch"); if (card.getTrack2() != null && !exp.equals(card.getTrack2().getExp())) throw new InvalidCardException ("track2 EXP mismatch"); throw new InvalidCardException(String.format("service code mismatch (%d)", mismatch)); throw new InvalidCardException ("invalid LUHN");
/** * Construct a CardHolder based on content received on * field 35 (track2) or field 2 (PAN) + field 14 (EXP) * @param m an ISOMsg * @throws InvalidCardException */ public CardHolder (ISOMsg m) throws InvalidCardException { super(); if (m.hasField(35)) parseTrack2((String) m.getValue(35)); else if (m.hasField(2)) { setPAN((String) m.getValue(2)); if (m.hasField(14)) setEXP((String) m.getValue(14)); } else { throw new InvalidCardException("required fields not present"); } if (m.hasField(45)) { setTrack1((String) m.getValue(45)); } if (m.hasField(55)) { setSecurityCode(m.getString(55)); } }