/** * Checks whether or not the {@link #cvc} field is valid. * * @return {@code true} if valid, {@code false} otherwise */ public boolean validateCVC() { if (StripeTextUtils.isBlank(cvc)) { return false; } String cvcValue = cvc.trim(); String updatedType = getBrand(); boolean validLength = (updatedType == null && cvcValue.length() >= 3 && cvcValue.length() <= 4) || (AMERICAN_EXPRESS.equals(updatedType) && cvcValue.length() == 4) || cvcValue.length() == 3; return ModelUtils.isWholePositiveNumber(cvcValue) && validLength; }
/** * Determines whether the input year-month pair has passed. * * @param year the input year, as a two or four-digit integer * @param month the input month * @param now the current time * @return {@code true} if the input time has passed the specified current time, * {@code false} otherwise. */ @SuppressWarnings("WrongConstant") static boolean hasMonthPassed(int year, int month, Calendar now) { if (hasYearPassed(year, now)) { return true; } // Expires at end of specified month, Calendar month starts at 0 return normalizeYear(year, now) == now.get(Calendar.YEAR) && month < (now.get(Calendar.MONTH) + 1); }
boolean validateExpiryDate(Calendar now) { if (!validateExpMonth()) { return false; } if (!validateExpYear(now)) { return false; } return !ModelUtils.hasMonthPassed(expYear, expMonth, now); }
/** * Determines whether or not the input year has already passed. * * @param year the input year, as a two or four-digit integer * @param now, the current time * @return {@code true} if the input year has passed the year of the specified current time * {@code false} otherwise. */ static boolean hasYearPassed(int year, Calendar now) { int normalized = normalizeYear(year, now); return normalized < now.get(Calendar.YEAR); }
/** * Checks whether or not the {@link #expYear} field is valid. * * @return {@code true} if valid, {@code false} otherwise. */ boolean validateExpYear(Calendar now) { return expYear != null && !ModelUtils.hasYearPassed(expYear, now); }
@Test public void normalizeSameCenturyShouldPass() { Calendar now = Calendar.getInstance(); int year = 1997; now.set(Calendar.YEAR, year); assertEquals(ModelUtils.normalizeYear(97, now), year); }
@Test public void wholePositiveNumberShouldFailIfNegative() { assertFalse(ModelUtils.isWholePositiveNumber("-1")); }
@Test public void normalizeDifferentCenturyShouldFail() { Calendar now = Calendar.getInstance(); int year = 1997; now.set(Calendar.YEAR, year); assertNotEquals(ModelUtils.normalizeYear(97, now), 2097); } }
@Test public void wholePositiveNumberShouldPassWithLeadingZero() { assertTrue(ModelUtils.isWholePositiveNumber("000")); }
@Test public void wholePositiveNumberShouldFailNull() { assertFalse(ModelUtils.isWholePositiveNumber(null)); }
@Test public void wholePositiveNumberShouldPassIfEmpty() { assertTrue(ModelUtils.isWholePositiveNumber("")); }
@Test public void wholePositiveNumberShouldPass() { assertTrue(ModelUtils.isWholePositiveNumber("123")); }
@Test public void wholePositiveNumberShouldFailIfLetters() { assertFalse(ModelUtils.isWholePositiveNumber("1a")); }