Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set card number and validate it #671

Merged
merged 3 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ public void setShouldShowPostalCode(boolean shouldShowPostalCode) {
adjustViewForPostalCodeAttribute();
}

/**
* Set the card number. Method does not change text field focus.
*
* @param cardNumber card number to be set
*/
public void setCardNumber(@Nullable String cardNumber) {
mCardNumberEditText.setText(cardNumber);
}

/**
* Checks whether the current card number is valid
*/
public boolean validateCardNumber() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appears to be unused, is that intentional? Does it need to be a public method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is intentional. We are adding the ability for our users to call this. I think there's cases where they will want to just add the number and cases when they will want to add it validate it so we allow them to do both.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a test for this method then?

boolean cardNumberIsValid =
CardUtils.isValidCardNumber(mCardNumberEditText.getCardNumber());
mCardNumberEditText.setShouldShowError(!cardNumberIsValid);
return cardNumberIsValid;
}

/**
* Expose a text watcher to receive updates when the card number is changed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import android.view.View;
import android.widget.LinearLayout;

import com.stripe.android.R;
import com.stripe.android.BuildConfig;
import com.stripe.android.R;
import com.stripe.android.model.Card;
import com.stripe.android.testharness.ViewTestUtils;

Expand Down Expand Up @@ -51,15 +51,17 @@
public class CardMultilineWidgetTest {

// Every Card made by the CardInputView should have the card widget token.
private static final String[] EXPECTED_LOGGING_ARRAY = { CARD_MULTILINE_TOKEN };
private static final String[] EXPECTED_LOGGING_ARRAY = {CARD_MULTILINE_TOKEN};

private CardMultilineWidget mCardMultilineWidget;
private CardMultilineWidget mNoZipCardMultilineWidget;
private WidgetControlGroup mFullGroup;
private WidgetControlGroup mNoZipGroup;

@Mock CardInputListener mFullCardListener;
@Mock CardInputListener mNoZipCardListener;
@Mock
CardInputListener mFullCardListener;
@Mock
CardInputListener mNoZipCardListener;

@Before
public void setup() {
Expand Down Expand Up @@ -465,6 +467,58 @@ public void deleteWhenEmpty_fromPostalCode_shiftsToCvc() {
assertEquals("12", mFullGroup.cvcEditText.getText().toString());
}

@Test
public void setCardNumber_whenHasSpaces_canCreateValidCard() {
mCardMultilineWidget.setCardNumber(VALID_VISA_NO_SPACES);
mFullGroup.expiryDateEditText.append("12");
mFullGroup.expiryDateEditText.append("50");
mFullGroup.cvcEditText.append("123");
mFullGroup.postalCodeEditText.append("12345");

Card card = mCardMultilineWidget.getCard();

assertNotNull(card);
assertEquals(VALID_VISA_NO_SPACES, card.getNumber());
}

@Test
public void setCardNumber_whenHasNoSpaces_canCreateValidCard() {
mCardMultilineWidget.setCardNumber(VALID_VISA_WITH_SPACES);
mFullGroup.expiryDateEditText.append("12");
mFullGroup.expiryDateEditText.append("50");
mFullGroup.cvcEditText.append("123");
mFullGroup.postalCodeEditText.append("12345");

Card card = mCardMultilineWidget.getCard();

assertNotNull(card);
assertEquals(VALID_VISA_NO_SPACES, card.getNumber());
}

@Test
public void validateCardNumber_whenValid_doesNotShowError() {
mCardMultilineWidget.setCardNumber(VALID_VISA_WITH_SPACES);

Boolean isValid = mCardMultilineWidget.validateCardNumber();
Boolean shouldShowError = mFullGroup.cardNumberEditText.getShouldShowError();

assertTrue(isValid);
assertFalse(shouldShowError);
}

@Test
public void validateCardNumber_whenInvalid_setsShowError() {
String invalidNumber = "1234 1234 1234 1234";
mCardMultilineWidget.setCardNumber(invalidNumber);

Boolean isValid = mCardMultilineWidget.validateCardNumber();
Boolean shouldShowError = mFullGroup.cardNumberEditText.getShouldShowError();

assertFalse(isValid);
assertTrue(shouldShowError);
}


@Test
public void setEnabled_setsEnabledPropertyOnAllChildWidgets() {
assertTrue(mCardMultilineWidget.isEnabled());
Expand Down