/** * Gets the {@link #brand} of this card, changed from the "type" field. Use {@link #getBrand()} * instead. * * @return the {@link #brand} of this card */ @Deprecated @CardBrand public String getType() { return getBrand(); }
/** * 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; }
/** * Set the card data displayed using a {@link Card} object. Note that * the full number will not be accessed here. * * @param card the {@link Card} to be partially displayed */ void setCard(@NonNull Card card) { mCardBrand = card.getBrand(); mLast4 = card.getLast4(); updateBrandIcon(); updateCardInformation(); }
@Test public void testTypeReturnsCorrectlyForVisaCard() { Card card = new Card("4112123412341234", null, null, null); assertEquals(Card.VISA, card.getBrand()); }
@Test public void testTypeReturnsCorrectlyForMasterCard() { Card card = new Card("5112123412341234", null, null, null); assertEquals(Card.MASTERCARD, card.getBrand()); }
@Test public void testTypeReturnsCorrectlyForAmexCard() { Card card = new Card("3412123412341234", null, null, null); assertEquals(Card.AMERICAN_EXPRESS, card.getBrand()); }
@Test public void testTypeReturnsCorrectlyForDinersClubCard() { Card card = new Card("3612123412341234", null, null, null); assertEquals(Card.DINERS_CLUB, card.getBrand()); }
@Test public void testTypeReturnsCorrectlyForDiscoverCard() { Card card = new Card("6452123412341234", null, null, null); assertEquals(Card.DISCOVER, card.getBrand()); }
@Test public void testTypeReturnsCorrectlyForJCBCard() { Card card = new Card("3512123412341234", null, null, null); assertEquals(Card.JCB, card.getBrand()); }
@Test public void testTypeReturnsCorrectlyForUnionPay() { Card card = new Card("6200000000000005", null, null, null); assertEquals(Card.UNIONPAY, card.getBrand()); }
@Test public void shouldPassValidateNumberIfLuhnNumberAmex() { Card card = new Card("378282246310005", null, null, null); assertEquals(Card.AMERICAN_EXPRESS, card.getBrand()); assertTrue(card.validateNumber()); }
@Test public void shouldFailValidateNumberIfTooLong() { Card card = new Card("4242 4242 4242 4242 6", null, null, null); assertEquals(Card.VISA, card.getBrand()); assertFalse(card.validateNumber()); }
@Test public void getBrand_whenNumberIsNullButBrandIsSet_returnsCorrectValue() { Card card = new Card( null, 2, 2020, "123", "John Q Public", null, null, null, null, null, null, Card.AMERICAN_EXPRESS, null, null, null, null, null, null, null); assertEquals(Card.AMERICAN_EXPRESS, card.getBrand()); //noinspection deprecation assertEquals(Card.AMERICAN_EXPRESS, card.getType()); }
@Test public void setAllCardFields_whenValidValues_allowsGetCardWithExpectedValues() { if (Calendar.getInstance().get(Calendar.YEAR) > 2079) { fail("Update the code with a date that is still valid. Also, hello from the past."); } mCardInputWidget.setCardNumber(VALID_AMEX_NO_SPACES); mCardInputWidget.setExpiryDate(12, 2079); mCardInputWidget.setCvcCode("1234"); Card card = mCardInputWidget.getCard(); assertNotNull(card); assertEquals(VALID_AMEX_NO_SPACES, card.getNumber()); assertNotNull(card.getExpMonth()); assertNotNull(card.getExpYear()); assertEquals(12, (int) card.getExpMonth()); assertEquals(2079, (int) card.getExpYear()); assertEquals("1234", card.getCVC()); assertEquals(Card.AMERICAN_EXPRESS, card.getBrand()); }
private Card(Builder builder) { this.number = StripeTextUtils.nullIfBlank(normalizeCardNumber(builder.number)); this.expMonth = builder.expMonth; this.expYear = builder.expYear; this.cvc = StripeTextUtils.nullIfBlank(builder.cvc); this.name = StripeTextUtils.nullIfBlank(builder.name); this.addressLine1 = StripeTextUtils.nullIfBlank(builder.addressLine1); this.addressLine1Check = StripeTextUtils.nullIfBlank(builder.addressLine1Check); this.addressLine2 = StripeTextUtils.nullIfBlank(builder.addressLine2); this.addressCity = StripeTextUtils.nullIfBlank(builder.addressCity); this.addressState = StripeTextUtils.nullIfBlank(builder.addressState); this.addressZip = StripeTextUtils.nullIfBlank(builder.addressZip); this.addressZipCheck = StripeTextUtils.nullIfBlank(builder.addressZipCheck); this.addressCountry = StripeTextUtils.nullIfBlank(builder.addressCountry); this.last4 = StripeTextUtils.nullIfBlank(builder.last4) == null ? getLast4() : builder.last4; this.brand = asCardBrand(builder.brand) == null ? getBrand() : builder.brand; this.fingerprint = StripeTextUtils.nullIfBlank(builder.fingerprint); this.funding = asFundingType(builder.funding); this.country = StripeTextUtils.nullIfBlank(builder.country); this.currency = StripeTextUtils.nullIfBlank(builder.currency); this.customerId = StripeTextUtils.nullIfBlank(builder.customer); this.cvcCheck = StripeTextUtils.nullIfBlank(builder.cvcCheck); this.id = StripeTextUtils.nullIfBlank(builder.id); this.tokenizationMethod = StripeTextUtils.nullIfBlank(builder.tokenizationMethod); this.metadata = builder.metadata; }
/** * Builder class for a {@link Card} model. */ public static class Builder { private final String number; private final String cvc; private final Integer expMonth; private final Integer expYear; private String name; private String addressLine1; private String addressLine1Check; private String addressLine2; private String addressCity; private String addressState; private String addressZip; private String addressZipCheck; private String addressCountry; private @CardBrand String brand; private @FundingType String funding; private @Size(4) String last4; private String fingerprint; private String country; private String currency; private String customer; private String cvcCheck; private String id; private String tokenizationMethod; private Map<String, String> metadata; /**
@Test public void createCardTokenSynchronous_withValidDataAndConnectAccount_returnsToken() { try { Stripe stripe = new Stripe(mContext, "pk_test_fdjfCYpGSwAX24KUEiuaAAWX"); stripe.setStripeAccount("acct_1Acj2PBUgO3KuWzz"); TestLoggingListener listener = new TestLoggingListener(true); stripe.setLoggingResponseListener(listener); Token token = stripe.createTokenSynchronous(mCard); assertNotNull(token); Card returnedCard = token.getCard(); assertNotNull(returnedCard); assertNull(token.getBankAccount()); assertEquals(Token.TYPE_CARD, token.getType()); assertEquals(mCard.getLast4(), returnedCard.getLast4()); assertEquals(Card.VISA, returnedCard.getBrand()); assertEquals(mCard.getExpYear(), returnedCard.getExpYear()); assertEquals(mCard.getExpMonth(), returnedCard.getExpMonth()); assertEquals(Card.FUNDING_CREDIT, returnedCard.getFunding()); assertAllLogsAreValid(listener, 2); } catch (AuthenticationException authEx) { fail("Unexpected error: " + authEx.getLocalizedMessage()); } catch (StripeException stripeEx) { fail("Unexpected error when connecting to Stripe API: " + stripeEx.getLocalizedMessage()); } }
@Test public void createCardTokenSynchronous_withValidData_returnsToken() { try { Stripe stripe = new Stripe(mContext, FUNCTIONAL_SOURCE_PUBLISHABLE_KEY); TestLoggingListener listener = new TestLoggingListener(true); stripe.setLoggingResponseListener(listener); Token token = stripe.createTokenSynchronous(mCard); assertNotNull(token); Card returnedCard = token.getCard(); assertNotNull(returnedCard); assertNull(token.getBankAccount()); assertEquals(Token.TYPE_CARD, token.getType()); assertEquals(mCard.getLast4(), returnedCard.getLast4()); assertEquals(Card.VISA, returnedCard.getBrand()); assertEquals(mCard.getExpYear(), returnedCard.getExpYear()); assertEquals(mCard.getExpMonth(), returnedCard.getExpMonth()); assertEquals(Card.FUNDING_CREDIT, returnedCard.getFunding()); assertAllLogsAreValid(listener, 2); } catch (AuthenticationException authEx) { fail("Unexpected error: " + authEx.getLocalizedMessage()); } catch (StripeException stripeEx) { fail("Unexpected error when connecting to Stripe API: " + stripeEx.getLocalizedMessage()); } }
@Test public void fromString_whenStringIsValidJson_returnsExpectedCard() { final Card expectedCard = buildEquivalentJsonCard(); final Card cardFromJson = Card.fromString(JSON_CARD); assertNotNull(cardFromJson); assertEquals(expectedCard.getBrand(), cardFromJson.getBrand()); assertEquals(expectedCard.getFunding(), cardFromJson.getFunding()); assertEquals(expectedCard.getCountry(), cardFromJson.getCountry()); assertEquals(expectedCard.getLast4(), cardFromJson.getLast4()); assertEquals(expectedCard.getExpMonth(), cardFromJson.getExpMonth()); assertEquals(expectedCard.getExpYear(), cardFromJson.getExpYear()); assertEquals(expectedCard.getCurrency(), cardFromJson.getCurrency()); assertEquals(expectedCard.getAddressCity(), cardFromJson.getAddressCity()); assertEquals(expectedCard.getAddressCountry(), cardFromJson.getAddressCountry()); assertEquals(expectedCard.getAddressLine1(), cardFromJson.getAddressLine1()); assertEquals(expectedCard.getAddressLine1Check(), cardFromJson.getAddressLine1Check()); assertEquals(expectedCard.getAddressLine2(), cardFromJson.getAddressLine2()); assertEquals(expectedCard.getAddressState(), cardFromJson.getAddressState()); assertEquals(expectedCard.getAddressZip(), cardFromJson.getAddressZip()); assertEquals(expectedCard.getAddressZipCheck(), cardFromJson.getAddressZipCheck()); assertEquals(expectedCard.getCvcCheck(), cardFromJson.getCvcCheck()); assertEquals(expectedCard.getName(), cardFromJson.getName()); assertEquals(expectedCard.getCustomerId(), cardFromJson.getCustomerId()); assertEquals(expectedCard.getFingerprint(), cardFromJson.getFingerprint()); assertEquals(expectedCard.getId(), cardFromJson.getId()); assertEquals(expectedCard.getMetadata(), cardFromJson.getMetadata()); }