TrackUtils { public static EmvTrack1 extractTrack1Data(final byte[] pRawTrack1) { EmvTrack1 ret = null; if (pRawTrack1 != null) { EmvTrack1 track1 = new EmvTrack1(); track1.setRaw(pRawTrack1); Matcher m = TRACK1_PATTERN.matcher(new String(pRawTrack1)); if (m.find()) { track1.setFormatCode(m.group(1)); track1.setCardNumber(m.group(2)); String[] name = StringUtils.split(m.group(4).trim(), CARD_HOLDER_NAME_SEPARATOR); if (name != null && name.length == 2) { track1.setHolderLastname(StringUtils.trimToNull(name[0])); track1.setHolderFirstname(StringUtils.trimToNull(name[1])); } SimpleDateFormat sdf = new SimpleDateFormat("yyMM", Locale.getDefault()); try { track1.setExpireDate(DateUtils.truncate(sdf.parse(m.group(5)), Calendar.MONTH)); } catch (ParseException e) { LOGGER.error("Unparsable expire card date : {}", e.getMessage()); return ret; } track1.setService(new Service(m.group(6))); ret = track1; } } return ret; } private  TrackUtils(); static EmvTrack2 extractTrack2EquivalentData(final byte[] pRawTrack2); static EmvTrack1 extractTrack1Data(final byte[] pRawTrack1); static final String CARD_HOLDER_NAME_SEPARATOR; }
@Test public void track1Test() { EmvTrack1 track1 = TrackUtils .extractTrack1Data( BytesUtils .fromString("42343131313131313131313131313131313F305E202F5E31373032323031313030333F313030313030303030303030303030303F")); Assertions.assertThat(track1).isNotNull(); Assertions.assertThat(track1.getCardNumber()).isEqualTo("4111111111111111"); SimpleDateFormat sdf = new SimpleDateFormat("MM/yyyy"); Assertions.assertThat(sdf.format(track1.getExpireDate())).isEqualTo("02/2017"); Assertions.assertThat(track1).isNotNull(); Assertions.assertThat(track1.getService()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode1()).isEqualTo(ServiceCode1Enum.INTERNATIONNAL_ICC); Assertions.assertThat(track1.getService().getServiceCode1().getInterchange()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode1().getTechnology()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode2()).isEqualTo(ServiceCode2Enum.NORMAL); Assertions.assertThat(track1.getService().getServiceCode2().getAuthorizationProcessing()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode3()).isEqualTo(ServiceCode3Enum.NO_RESTRICTION); Assertions.assertThat(track1.getService().getServiceCode3().getAllowedServices()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode3().getPinRequirements()).isNotNull(); }
@Test public void track1NameTest() { EmvTrack1 track1 = TrackUtils .extractTrack1Data( BytesUtils .fromString("42343131313131313131313131313131313F305E446F652F4A6F686E5E31373032323031313030333F313030313030303030303030303030303F")); Assertions.assertThat(track1).isNotNull(); Assertions.assertThat(track1.getCardNumber()).isEqualTo("4111111111111111"); SimpleDateFormat sdf = new SimpleDateFormat("MM/yyyy"); Assertions.assertThat(sdf.format(track1.getExpireDate())).isEqualTo("02/2017"); Assertions.assertThat(track1).isNotNull(); Assertions.assertThat(track1.getHolderFirstname()).isEqualTo("John"); Assertions.assertThat(track1.getHolderLastname()).isEqualTo("Doe"); Assertions.assertThat(track1.getService()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode1()).isEqualTo(ServiceCode1Enum.INTERNATIONNAL_ICC); Assertions.assertThat(track1.getService().getServiceCode1().getInterchange()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode1().getTechnology()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode2()).isEqualTo(ServiceCode2Enum.NORMAL); Assertions.assertThat(track1.getService().getServiceCode2().getAuthorizationProcessing()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode3()).isEqualTo(ServiceCode3Enum.NO_RESTRICTION); Assertions.assertThat(track1.getService().getServiceCode3().getAllowedServices()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode3().getPinRequirements()).isNotNull(); }
@Test public void track1FormatTest() { EmvTrack1 track1 = TrackUtils .extractTrack1Data( BytesUtils .fromString("42353231313131313131313131313131315E202F2020202020202020202020202020202020202020202020205E31363038323032303030303030303030303030312020303030202020202030")); Assertions.assertThat(track1).isNotNull(); Assertions.assertThat(track1.getCardNumber()).isEqualTo("5211111111111111"); SimpleDateFormat sdf = new SimpleDateFormat("MM/yyyy"); Assertions.assertThat(sdf.format(track1.getExpireDate())).isEqualTo("08/2016"); Assertions.assertThat(track1.getHolderFirstname()).isNull(); Assertions.assertThat(track1.getHolderLastname()).isNull(); Assertions.assertThat(track1.getService()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode1()).isEqualTo(ServiceCode1Enum.INTERNATIONNAL_ICC); Assertions.assertThat(track1.getService().getServiceCode1().getInterchange()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode1().getTechnology()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode2()).isEqualTo(ServiceCode2Enum.NORMAL); Assertions.assertThat(track1.getService().getServiceCode2().getAuthorizationProcessing()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode3()).isEqualTo(ServiceCode3Enum.GOODS_SERVICES); Assertions.assertThat(track1.getService().getServiceCode3().getAllowedServices()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode3().getPinRequirements()).isNotNull(); }
@Test public void track1FormatNullUser() { EmvTrack1 track1 = TrackUtils .extractTrack1Data( BytesUtils .fromString("42353231313131313131313131313131315E22202020202020202020202020202020202020202020202020205E31363038323032303030303030303030303030312020303030202020202030")); Assertions.assertThat(track1.getCardNumber()).isEqualTo("5211111111111111"); SimpleDateFormat sdf = new SimpleDateFormat("MM/yyyy"); Assertions.assertThat(sdf.format(track1.getExpireDate())).isEqualTo("08/2016"); Assertions.assertThat(track1.getHolderFirstname()).isNull(); Assertions.assertThat(track1.getHolderLastname()).isNull(); Assertions.assertThat(track1.getService()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode1()).isEqualTo(ServiceCode1Enum.INTERNATIONNAL_ICC); Assertions.assertThat(track1.getService().getServiceCode1().getInterchange()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode1().getTechnology()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode2()).isEqualTo(ServiceCode2Enum.NORMAL); Assertions.assertThat(track1.getService().getServiceCode2().getAuthorizationProcessing()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode3()).isEqualTo(ServiceCode3Enum.GOODS_SERVICES); Assertions.assertThat(track1.getService().getServiceCode3().getAllowedServices()).isNotNull(); Assertions.assertThat(track1.getService().getServiceCode3().getPinRequirements()).isNotNull(); }