Java Code Examples for com.braintreepayments.cardform.utils.CardType

The following examples show how to use com.braintreepayments.cardform.utils.CardType. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: braintree-android-drop-in   Source File: AddCardViewUnitTest.java    License: MIT License 6 votes vote down vote up
@Test
public void setup_showsSupportedCardTypesFromConfiguration() throws NoSuchFieldException,
        IllegalAccessException {
    Configuration configuration = new TestConfigurationBuilder()
            .creditCards(new TestConfigurationBuilder.TestCardConfigurationBuilder()
                    .supportedCardTypes(PaymentMethodType.AMEX.getCanonicalName(),
                            PaymentMethodType.VISA.getCanonicalName(),
                            PaymentMethodType.MASTERCARD.getCanonicalName(),
                            PaymentMethodType.DISCOVER.getCanonicalName(),
                            PaymentMethodType.JCB.getCanonicalName()))
            .buildConfiguration();

    mView.setup(mActivity, configuration, true);

    List<CardType> cardTypes = (List<CardType>) getField(
            mView.findViewById(R.id.bt_supported_card_types), "mSupportedCardTypes");
    assertEquals(5, cardTypes.size());
    assertTrue(cardTypes.contains(CardType.AMEX));
    assertTrue(cardTypes.contains(CardType.VISA));
    assertTrue(cardTypes.contains(CardType.MASTERCARD));
    assertTrue(cardTypes.contains(CardType.DISCOVER));
    assertTrue(cardTypes.contains(CardType.JCB));
}
 
Example 2
Source Project: braintree-android-drop-in   Source File: AddCardViewUnitTest.java    License: MIT License 6 votes vote down vote up
@Test
public void setup_showsUnionPaySupportedWhenInTheConfigurationAndSupported()
        throws NoSuchFieldException, IllegalAccessException {
    Configuration configuration = new TestConfigurationBuilder()
            .creditCards(new TestConfigurationBuilder.TestCardConfigurationBuilder()
                    .supportedCardTypes(PaymentMethodType.UNIONPAY.getCanonicalName(),
                            PaymentMethodType.VISA.getCanonicalName()))
            .buildConfiguration();

    mView.setup(mActivity, configuration, true);

    List<CardType> cardTypes = (List<CardType>) getField(
            mView.findViewById(R.id.bt_supported_card_types), "mSupportedCardTypes");
    assertEquals(2, cardTypes.size());
    assertTrue(cardTypes.contains(CardType.UNIONPAY));
    assertTrue(cardTypes.contains(CardType.VISA));
}
 
Example 3
Source Project: braintree-android-drop-in   Source File: AddCardViewUnitTest.java    License: MIT License 6 votes vote down vote up
@Test
public void setup_doesNotShowUnionPaySupportedWhenInTheConfigurationAndNotSupported()
        throws NoSuchFieldException, IllegalAccessException {
    Configuration configuration = new TestConfigurationBuilder()
            .creditCards(new TestConfigurationBuilder.TestCardConfigurationBuilder()
                    .supportedCardTypes(PaymentMethodType.UNIONPAY.getCanonicalName(),
                            PaymentMethodType.VISA.getCanonicalName()))
            .buildConfiguration();

    mView.setup(mActivity, configuration, false);

    List<CardType> cardTypes = (List<CardType>) getField(
            mView.findViewById(R.id.bt_supported_card_types), "mSupportedCardTypes");
    assertEquals(1, cardTypes.size());
    assertTrue(cardTypes.contains(CardType.VISA));
}
 
Example 4
Source Project: android-card-form   Source File: SupportedCardTypesView.java    License: MIT License 6 votes vote down vote up
/**
 * Selects the intersection between the {@link CardType}s passed into
 * {@link #setSupportedCardTypes(CardType...)} and {@link CardType}s passed into
 * this method as visually enabled.
 *
 * The remaining supported card types will become visually disabled.
 *
 * {@link #setSupportedCardTypes(CardType...)} must be called prior to using this method.
 *
 * @param cardTypes The {@link CardType}s to set as visually enabled.
 */
public void setSelected(@Nullable CardType... cardTypes) {
    if (cardTypes == null) {
        cardTypes = new CardType[]{};
    }

    SpannableString spannableString = new SpannableString(new String(new char[mSupportedCardTypes.size()]));
    PaddedImageSpan span;
    for (int i = 0; i < mSupportedCardTypes.size(); i++) {
        span = new PaddedImageSpan(getContext(), mSupportedCardTypes.get(i).getFrontResource());
        span.setDisabled(!Arrays.asList(cardTypes).contains(mSupportedCardTypes.get(i)));
        spannableString.setSpan(span, i, i + 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    setText(spannableString);
}
 
Example 5
Source Project: android-card-form   Source File: CardFormTest.java    License: MIT License 6 votes vote down vote up
@Test
public void onCardTypeChangeListenerIsCalledWhenCardTypeChanges() {
    mCardForm.cardRequired(true)
            .expirationRequired(false)
            .cvvRequired(false)
            .cardholderName(CardForm.FIELD_DISABLED)
            .postalCodeRequired(false)
            .mobileNumberRequired(false)
            .mobileNumberExplanation("Make sure SMS is supported")
            .setup(mActivity);
    final AtomicBoolean wasCalled = new AtomicBoolean(false);
    mCardForm.setOnCardTypeChangedListener(new CardEditText.OnCardTypeChangedListener() {
        @Override
        public void onCardTypeChanged(CardType cardType) {
            assertEquals(CardType.VISA, cardType);
            wasCalled.set(true);
        }
    });

    setText((EditText) mCardForm.findViewById(R.id.bt_card_form_card_number), VISA);

    assertTrue(wasCalled.get());
}
 
Example 6
Source Project: android-card-form   Source File: SupportedCardTypesViewTest.java    License: MIT License 6 votes vote down vote up
@Test
public void setSupportedCardTypes_addsAllCardTypes() {
    SupportedCardTypesView supportedCardTypesView = new SupportedCardTypesView(RuntimeEnvironment.application);

    supportedCardTypesView.setSupportedCardTypes(CardType.VISA, CardType.MASTERCARD, CardType.DISCOVER,
            CardType.AMEX, CardType.DINERS_CLUB, CardType.JCB, CardType.MAESTRO, CardType.UNIONPAY,
            CardType.HIPER, CardType.HIPERCARD);

    List<PaddedImageSpan> allSpans = Arrays.asList(new SpannableString(supportedCardTypesView.getText())
            .getSpans(0, supportedCardTypesView.length(), PaddedImageSpan.class));
    assertEquals(CardType.VISA.getFrontResource(), allSpans.get(0).getResourceId());
    assertEquals(CardType.MASTERCARD.getFrontResource(), allSpans.get(1).getResourceId());
    assertEquals(CardType.DISCOVER.getFrontResource(), allSpans.get(2).getResourceId());
    assertEquals(CardType.AMEX.getFrontResource(), allSpans.get(3).getResourceId());
    assertEquals(CardType.DINERS_CLUB.getFrontResource(), allSpans.get(4).getResourceId());
    assertEquals(CardType.JCB.getFrontResource(), allSpans.get(5).getResourceId());
    assertEquals(CardType.MAESTRO.getFrontResource(), allSpans.get(6).getResourceId());
    assertEquals(CardType.UNIONPAY.getFrontResource(), allSpans.get(7).getResourceId());
    assertEquals(CardType.HIPER.getFrontResource(), allSpans.get(8).getResourceId());
    assertEquals(CardType.HIPERCARD.getFrontResource(), allSpans.get(9).getResourceId());
}
 
Example 7
Source Project: android-card-form   Source File: SupportedCardTypesViewTest.java    License: MIT License 6 votes vote down vote up
@Test
public void setSelectedCardTypes_disablesNonSelectedCardTypes() {
    SupportedCardTypesView supportedCardTypesView = new SupportedCardTypesView(RuntimeEnvironment.application);
    supportedCardTypesView.setSupportedCardTypes(CardType.VISA, CardType.MASTERCARD, CardType.DISCOVER,
            CardType.AMEX, CardType.DINERS_CLUB, CardType.JCB, CardType.MAESTRO, CardType.UNIONPAY,
            CardType.HIPER, CardType.HIPERCARD);

    supportedCardTypesView.setSelected(CardType.VISA);

    List<PaddedImageSpan> allSpans = Arrays.asList(new SpannableString(supportedCardTypesView.getText())
            .getSpans(0, supportedCardTypesView.length(), PaddedImageSpan.class));
    assertFalse(allSpans.get(0).isDisabled());
    assertTrue(allSpans.get(1).isDisabled());
    assertTrue(allSpans.get(2).isDisabled());
    assertTrue(allSpans.get(3).isDisabled());
    assertTrue(allSpans.get(4).isDisabled());
    assertTrue(allSpans.get(5).isDisabled());
    assertTrue(allSpans.get(6).isDisabled());
    assertTrue(allSpans.get(7).isDisabled());
    assertTrue(allSpans.get(8).isDisabled());
    assertTrue(allSpans.get(9).isDisabled());
}
 
Example 8
Source Project: android-card-form   Source File: SupportedCardTypesViewTest.java    License: MIT License 6 votes vote down vote up
@Test
public void setSelectedCardTypes_handlesNull() {
    SupportedCardTypesView supportedCardTypesView = new SupportedCardTypesView(RuntimeEnvironment.application);
    supportedCardTypesView.setSupportedCardTypes(CardType.VISA, CardType.MASTERCARD, CardType.DISCOVER,
            CardType.AMEX, CardType.DINERS_CLUB, CardType.JCB, CardType.MAESTRO, CardType.UNIONPAY,
            CardType.HIPER, CardType.HIPERCARD);

    supportedCardTypesView.setSelected((CardType[]) null);

    List<PaddedImageSpan> allSpans = Arrays.asList(new SpannableString(supportedCardTypesView.getText())
            .getSpans(0, supportedCardTypesView.length(), PaddedImageSpan.class));
    assertTrue(allSpans.get(0).isDisabled());
    assertTrue(allSpans.get(1).isDisabled());
    assertTrue(allSpans.get(2).isDisabled());
    assertTrue(allSpans.get(3).isDisabled());
    assertTrue(allSpans.get(4).isDisabled());
    assertTrue(allSpans.get(5).isDisabled());
    assertTrue(allSpans.get(6).isDisabled());
    assertTrue(allSpans.get(7).isDisabled());
    assertTrue(allSpans.get(8).isDisabled());
    assertTrue(allSpans.get(9).isDisabled());
}
 
Example 9
Source Project: braintree_android   Source File: CardActivity.java    License: MIT License 6 votes vote down vote up
@Override
public void onCardFormFieldFocused(View field) {
    if (mBraintreeFragment == null) {
        return;
    }

    if (!(field instanceof CardEditText) && !TextUtils.isEmpty(mCardForm.getCardNumber())) {
        CardType cardType = CardType.forCardNumber(mCardForm.getCardNumber());
        if (mCardType != cardType) {
            mCardType  = cardType;

            if (mConfiguration.getUnionPay().isEnabled() && !Settings.useTokenizationKey(getApplicationContext())) {
                UnionPay.fetchCapabilities(mBraintreeFragment, mCardForm.getCardNumber());
            }
        }
    }
}
 
Example 10
Source Project: braintree-android-drop-in   Source File: AddCardView.java    License: MIT License 5 votes vote down vote up
@Override
public void onCardTypeChanged(CardType cardType) {
    if (cardType == CardType.EMPTY) {
        mSupportedCardTypesView.setSupportedCardTypes(mSupportedCardTypes);
    } else {
        mSupportedCardTypesView.setSelected(cardType);
    }
}
 
Example 11
Source Project: braintree-android-drop-in   Source File: AddCardViewUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
public void setup_doesNotShowUnionPaySupportedWhenNotInTheConfigurationAndNotSupported()
        throws NoSuchFieldException, IllegalAccessException {
    Configuration configuration = new TestConfigurationBuilder()
            .creditCards(new TestConfigurationBuilder.TestCardConfigurationBuilder()
                    .supportedCardTypes(PaymentMethodType.VISA.getCanonicalName()))
            .buildConfiguration();

    mView.setup(mActivity, configuration, false);

    List<CardType> cardTypes = (List<CardType>) getField(
            mView.findViewById(R.id.bt_supported_card_types), "mSupportedCardTypes");
    assertEquals(1, cardTypes.size());
    assertTrue(cardTypes.contains(CardType.VISA));
}
 
Example 12
Source Project: braintree-android-drop-in   Source File: AddCardViewUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
public void onCardTypeChanged_showsAllCardTypesWhenEmpty() throws NoSuchFieldException,
        IllegalAccessException {
    mView.onCardTypeChanged(CardType.EMPTY);

    SupportedCardTypesView supportedCardTypesView = mView.findViewById(
            R.id.bt_supported_card_types);
    List<PaddedImageSpan> allSpans = Arrays.asList(new SpannableString(supportedCardTypesView.getText())
            .getSpans(0, supportedCardTypesView.length(), PaddedImageSpan.class));
    assertEquals(1, allSpans.size());
    assertFalse((Boolean) getField(allSpans.get(0), "mDisabled"));
}
 
Example 13
@Test
public void getCardTypes_returnsCorrectCardTypeArray() throws JSONException {
    Set<String> supportedCardTypes = new LinkedHashSet<>();
    supportedCardTypes.add(PaymentMethodType.VISA.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.MASTERCARD.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.DISCOVER.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.AMEX.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.JCB.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.DINERS.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.MAESTRO.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.UNIONPAY.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.PAYPAL.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.UNKNOWN.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.PAY_WITH_VENMO.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.HIPER.getCanonicalName());
    supportedCardTypes.add(PaymentMethodType.HIPERCARD.getCanonicalName());

    CardType[] cardTypes = PaymentMethodType.getCardsTypes(supportedCardTypes);

    assertEquals(10, cardTypes.length);
    assertEquals(CardType.VISA, cardTypes[0]);
    assertEquals(CardType.MASTERCARD, cardTypes[1]);
    assertEquals(CardType.DISCOVER, cardTypes[2]);
    assertEquals(CardType.AMEX, cardTypes[3]);
    assertEquals(CardType.JCB, cardTypes[4]);
    assertEquals(CardType.DINERS_CLUB, cardTypes[5]);
    assertEquals(CardType.MAESTRO, cardTypes[6]);
    assertEquals(CardType.UNIONPAY, cardTypes[7]);
    assertEquals(CardType.HIPER, cardTypes[8]);
    assertEquals(CardType.HIPERCARD, cardTypes[9]);
}
 
Example 14
Source Project: android-card-form   Source File: CardEditText.java    License: MIT License 5 votes vote down vote up
private void updateCardType() {
    CardType type = CardType.forCardNumber(getText().toString());
    if (mCardType != type) {
        mCardType = type;

        InputFilter[] filters = { new LengthFilter(mCardType.getMaxCardLength()) };
        setFilters(filters);
        invalidate();

        if (mOnCardTypeChangedListener != null) {
            mOnCardTypeChangedListener.onCardTypeChanged(mCardType);
        }
    }
}
 
Example 15
Source Project: android-card-form   Source File: CardForm.java    License: MIT License 5 votes vote down vote up
@Override
public void onCardTypeChanged(CardType cardType) {
    mCvv.setCardType(cardType);

    if (mOnCardTypeChangedListener != null) {
        mOnCardTypeChangedListener.onCardTypeChanged(cardType);
    }
}
 
Example 16
Source Project: android-card-form   Source File: CvvEditText.java    License: MIT License 5 votes vote down vote up
/**
 * Sets the card type associated with the security code type. {@link CardType#AMEX} has a
 * different icon and length than other card types. Typically handled through
 * {@link com.braintreepayments.cardform.view.CardEditText.OnCardTypeChangedListener#onCardTypeChanged(com.braintreepayments.cardform.utils.CardType)}.
 *
 * @param cardType Type of card represented by the current value of card number input.
 */
public void setCardType(CardType cardType) {
    mCardType = cardType;

    InputFilter[] filters = { new LengthFilter(cardType.getSecurityCodeLength()) };
    setFilters(filters);

    setContentDescription(getContext().getString(cardType.getSecurityCodeName()));
    setFieldHint(cardType.getSecurityCodeName());

    invalidate();
}
 
Example 17
Source Project: android-card-form   Source File: SupportedCardTypesView.java    License: MIT License 5 votes vote down vote up
/**
 * Sets the supported {@link CardType}s on the view to display the card icons.
 *
 * @param cardTypes The {@link CardType}s to display
 */
public void setSupportedCardTypes(@Nullable CardType... cardTypes) {
    if (cardTypes == null) {
        cardTypes = new CardType[]{};
    }

    mSupportedCardTypes.clear();
    mSupportedCardTypes.addAll(Arrays.asList(cardTypes));

    setSelected(cardTypes);
}
 
Example 18
Source Project: android-card-form   Source File: CvvEditTextTest.java    License: MIT License 5 votes vote down vote up
@Test
public void customLimits() {
    for (CardType type : CardType.values()) {
        mView.setCardType(type);
        if (type == CardType.AMEX) {
            type("1234").assertTextIs("1234");
            type("5").assertTextIs("1234");
        } else {
            type("123").assertTextIs("123");
            type("4").assertTextIs("123");
        }
        mView.getText().clear();
    }
}
 
Example 19
Source Project: android-card-form   Source File: CvvEditTextTest.java    License: MIT License 5 votes vote down vote up
@Test
public void hintChangesForCardType() {
    for (CardType cardType : CardType.values()) {
        mView.setCardType(cardType);

        assertEquals(RuntimeEnvironment.application.getString(cardType.getSecurityCodeName()),
                mView.getTextInputLayoutParent().getHint());
        assertEquals(RuntimeEnvironment.application.getString(cardType.getSecurityCodeName()),
                mView.getContentDescription());
    }
}
 
Example 20
Source Project: android-card-form   Source File: CvvEditTextTest.java    License: MIT License 5 votes vote down vote up
@Test
public void getErrorMessage_returnsCorrectErrorMessageForCardTypeWhenEmpty() {
    for (CardType cardType : CardType.values()) {
        mView.setCardType(cardType);

        String expectedMessage = RuntimeEnvironment.application.getString(R.string.bt_cvv_required,
                RuntimeEnvironment.application.getString(cardType.getSecurityCodeName()));
        assertEquals(expectedMessage, mView.getErrorMessage());
    }
}
 
Example 21
Source Project: android-card-form   Source File: CvvEditTextTest.java    License: MIT License 5 votes vote down vote up
@Test
public void getErrorMessage_returnsCorrectErrorMessageForCardTypeWhenNotEmpty() {
    type("4");

    for (CardType cardType : CardType.values()) {
        mView.setCardType(cardType);

        String expectedMessage = RuntimeEnvironment.application.getString(R.string.bt_cvv_invalid,
                RuntimeEnvironment.application.getString(cardType.getSecurityCodeName()));
        assertEquals(expectedMessage, mView.getErrorMessage());
    }
}
 
Example 22
Source Project: android-card-form   Source File: CardEditTextTest.java    License: MIT License 5 votes vote down vote up
@Test
public void callsCardTypeChangedListenerWhenSetAndCardTypeChanges() {
    CardEditText.OnCardTypeChangedListener listener = mock(CardEditText.OnCardTypeChangedListener.class);
    mView.setOnCardTypeChangedListener(listener);

    type("4");

    verify(listener).onCardTypeChanged(CardType.VISA);
}
 
Example 23
Source Project: android-card-form   Source File: CardEditTextTest.java    License: MIT License 5 votes vote down vote up
@Test
public void doesNotCallCardTypeChangedListenerWhenSetAndCardTypeDoesNotChange() {
    CardEditText.OnCardTypeChangedListener listener = mock(CardEditText.OnCardTypeChangedListener.class);
    mView.setOnCardTypeChangedListener(listener);

    type("4");
    type("111");

    verify(listener).onCardTypeChanged(CardType.VISA);
}
 
Example 24
Source Project: android-card-form   Source File: CardEditTextTest.java    License: MIT License 5 votes vote down vote up
@Test
public void callsCardTypeChangedListenerWhenEditTextChangesFromEmptyToUnknown() {
    CardEditText.OnCardTypeChangedListener listener = mock(CardEditText.OnCardTypeChangedListener.class);
    mView.setOnCardTypeChangedListener(listener);

    type("1");

    verify(listener).onCardTypeChanged(CardType.UNKNOWN);
}
 
Example 25
Source Project: android-card-form   Source File: CardEditTextTest.java    License: MIT License 5 votes vote down vote up
@Test
public void callsCardTypeChangeListenerWhenEditTextChangesFromUnknownToEmpty() {
    CardEditText.OnCardTypeChangedListener listener = mock(CardEditText.OnCardTypeChangedListener.class);
    mView.setOnCardTypeChangedListener(listener);

    type("1");
    mView.getText().clear();

    verify(listener).onCardTypeChanged(CardType.UNKNOWN);
    verify(listener).onCardTypeChanged(CardType.EMPTY);
}
 
Example 26
Source Project: android-card-form   Source File: SupportedCardTypesViewTest.java    License: MIT License 5 votes vote down vote up
@Test
public void setSupportedCardTypes_handlesNull() {
    SupportedCardTypesView supportedCardTypesView = new SupportedCardTypesView(RuntimeEnvironment.application);

    supportedCardTypesView.setSupportedCardTypes((CardType[]) null);

    List<PaddedImageSpan> allSpans = Arrays.asList(new SpannableString(supportedCardTypesView.getText())
            .getSpans(0, supportedCardTypesView.length(), PaddedImageSpan.class));
    assertEquals(0, allSpans.size());
}
 
Example 27
Source Project: android-card-form   Source File: SupportedCardTypesViewTest.java    License: MIT License 5 votes vote down vote up
@Test
public void setSelected_withoutSettingSupportedCardTypes_doesNothing() {
    SupportedCardTypesView supportedCardTypesView = new SupportedCardTypesView(RuntimeEnvironment.application);

    supportedCardTypesView.setSelected(CardType.VISA);

    assertEquals("", supportedCardTypesView.getText().toString());
}
 
Example 28
Source Project: android-card-form   Source File: BaseCardFormActivity.java    License: MIT License 5 votes vote down vote up
@Override
public void onCardTypeChanged(CardType cardType) {
    if (cardType == CardType.EMPTY) {
        mSupportedCardTypesView.setSupportedCardTypes(SUPPORTED_CARD_TYPES);
    } else {
        mSupportedCardTypesView.setSelected(cardType);
    }
}
 
Example 29
Source Project: android-card-form   Source File: CardEditText.java    License: MIT License 2 votes vote down vote up
/**
 * @return The {@link com.braintreepayments.cardform.utils.CardType} currently entered in
 * the {@link android.widget.EditText}
 */
public CardType getCardType() {
    return mCardType;
}
 
Example 30
Source Project: android-card-form   Source File: CardEditText.java    License: MIT License votes vote down vote up
void onCardTypeChanged(CardType cardType);