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

fix postal code callback not firing when enabled #4875

Merged
merged 3 commits into from
Apr 15, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

### Payments (`com.stripe:stripe-android`)
* [ADDED] [4874](https://github.com/stripe/stripe-android/pull/4874) `us_bank_account` PaymentMethod is now available for ACH Direct Debit payments, including APIs to collect customer bank information (requires Connections SDK) and verify microdeposits.
* [FIXED] [4875](https://github.com/stripe/stripe-android/pull/4875) fix postal code callback not firing when enabled

### PaymentSheet
* [FIXED] [4861](https://github.com/stripe/stripe-android/pull/4861) Remove font resource to save space and default to system default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,17 @@ class CardInputWidget @JvmOverloads constructor(
postalCodeTextInputLayout.visibility = View.VISIBLE

cvcEditText.imeOptions = EditorInfo.IME_ACTION_NEXT

// First remove if it's already added, to make sure it's not added multiple times.
postalCodeEditText.removeTextChangedListener(cardValidTextWatcher)
postalCodeEditText.addTextChangedListener(cardValidTextWatcher)
} else {
postalCodeEditText.isEnabled = false
postalCodeTextInputLayout.visibility = View.GONE

cvcEditText.imeOptions = EditorInfo.IME_ACTION_DONE

postalCodeEditText.removeTextChangedListener(cardValidTextWatcher)
}
updatePostalRequired()
}
Expand Down Expand Up @@ -333,16 +339,8 @@ class CardInputWidget @JvmOverloads constructor(
private fun updatePostalRequired() {
if (isPostalRequired()) {
requiredFields.add(postalCodeEditText)
cardValidCallback?.let {
// First remove if it's already added, to make sure it's not added multiple times.
postalCodeEditText.removeTextChangedListener(cardValidTextWatcher)
postalCodeEditText.addTextChangedListener(cardValidTextWatcher)
}
} else {
requiredFields.remove(postalCodeEditText)
cardValidCallback?.let {
postalCodeEditText.removeTextChangedListener(cardValidTextWatcher)
}
}
}

Expand Down Expand Up @@ -756,7 +754,7 @@ class CardInputWidget @JvmOverloads constructor(
}

postalCodeEditText.setAfterTextChangedListener {
if (isPostalRequired() && postalCodeEditText.hasValidPostal()) {
if (postalCodeEditText.isEnabled && postalCodeEditText.hasValidPostal()) {
cardInputListener?.onPostalCodeComplete()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,7 @@ internal class CardInputWidgetTest {
var currentIsValid = false
var currentInvalidFields = emptySet<CardValidCallback.Fields>()
cardInputWidget.postalCodeEnabled = true
cardInputWidget.postalCodeRequired = false
cardInputWidget.setCardValidCallback { isValid, invalidFields ->
currentIsValid = isValid
currentInvalidFields = invalidFields
Expand Down Expand Up @@ -1529,10 +1530,10 @@ internal class CardInputWidgetTest {
}

@Test
fun usZipCodeRequired_whenFalse_shouldNotCallOnPostalCodeComplete() {
fun usZipCodeRequired_whenFalse_shouldCallOnPostalCodeComplete() {
cardInputWidget.usZipCodeRequired = false
postalCodeEditText.setText(POSTAL_CODE_VALUE)
assertThat(cardInputListener.onPostalCodeCompleteCalls).isEqualTo(0)
assertThat(cardInputListener.onPostalCodeCompleteCalls).isEqualTo(1)
}

@Test
Expand Down Expand Up @@ -1625,15 +1626,15 @@ internal class CardInputWidgetTest {
}

@Test
fun `Removing postal code requirement removes CardValidCallback notifications for the field`() {
fun `Removing postal code requirement keeps CardValidCallback notifications for the field`() {
val callback = mock<CardValidCallback>()
cardInputWidget.postalCodeRequired = true
cardInputWidget.setCardValidCallback(callback)
cardInputWidget.postalCodeRequired = false

postalCodeEditText.setText("54321")

verify(callback, times(1)).onInputChanged(any(), any())
verify(callback, times(2)).onInputChanged(any(), any())
}

@Test
Expand All @@ -1653,6 +1654,19 @@ internal class CardInputWidgetTest {
verify(callback, times(2)).onInputChanged(any(), any())
}

@Test
fun `Enabled but not required postal code should fire card valid callback when changed`() {
val callback = mock<CardValidCallback>()
cardInputWidget.setCardValidCallback(callback)

cardInputWidget.postalCodeEnabled = true
cardInputWidget.postalCodeRequired = false
postalCodeEditText.setText("54321")

// Called only when the callback is set and when the text is set.
verify(callback, times(2)).onInputChanged(any(), any())
}

private fun updateCardNumberAndIdle(cardNumber: String) {
cardNumberEditText.setText(cardNumber)
idleLooper()
Expand Down