Skip to content

Commit

Permalink
Add BecsDebitWidget.ValidParamsCallback (#2297)
Browse files Browse the repository at this point in the history
When `BecsDebitWidget.validParamsCallback` is set,
`onInputChanged(isValid)` will be called on that instance.
  • Loading branch information
mshafrir-stripe authored Mar 18, 2020
1 parent 25baf47 commit cd873bc
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/res/layout/becs_debit_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:enabled="false"
android:text="@string/create_becs_debit_params" />

<TextView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.stripe.android.ApiResultCallback
import com.stripe.android.PaymentConfiguration
import com.stripe.android.Stripe
import com.stripe.android.model.PaymentMethod
import com.stripe.android.view.BecsDebitWidget
import com.stripe.example.databinding.BecsDebitActivityBinding

class BecsDebitPaymentMethodActivity : AppCompatActivity() {
Expand All @@ -23,6 +24,12 @@ class BecsDebitPaymentMethodActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(viewBinding.root)

viewBinding.element.validParamsCallback = object : BecsDebitWidget.ValidParamsCallback {
override fun onInputChanged(isValid: Boolean) {
viewBinding.submit.isEnabled = isValid
}
}

viewBinding.mandateAcceptance.merchantName = "Rocketship Inc."

viewBinding.submit.setOnClickListener {
Expand Down
40 changes: 40 additions & 0 deletions stripe/src/main/java/com/stripe/android/view/BecsDebitWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,45 @@ class BecsDebitWidget @JvmOverloads constructor(
)
}

var validParamsCallback: ValidParamsCallback = object : ValidParamsCallback {
override fun onInputChanged(isValid: Boolean) {
// no-op default implementation
}
}

private val validParamsTextWatcher = object : StripeTextWatcher() {
override fun afterTextChanged(s: Editable?) {
super.afterTextChanged(s)
validParamsCallback.onInputChanged(isInputValid)
}
}

private val isInputValid: Boolean
get() {
val name = viewBinding.nameEditText.fieldText
val email = viewBinding.emailEditText.email
val bsbNumber = viewBinding.bsbEditText.bsb
val accountNumber = viewBinding.accountNumberEditText.accountNumber

return !(name.isBlank() || email.isNullOrBlank() || bsbNumber.isNullOrBlank() ||
accountNumber.isNullOrBlank())
}

init {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
viewBinding.nameEditText.setAutofillHints(View.AUTOFILL_HINT_NAME)
viewBinding.emailEditText.setAutofillHints(View.AUTOFILL_HINT_EMAIL_ADDRESS)
}

setOf(
viewBinding.nameEditText,
viewBinding.emailEditText,
viewBinding.bsbEditText,
viewBinding.accountNumberEditText
).forEach {
it.addTextChangedListener(validParamsTextWatcher)
}

viewBinding.bsbEditText.onBankChangedCallback = { bank ->
if (bank != null) {
viewBinding.bsbTextInputLayout.helperText = bank.name
Expand Down Expand Up @@ -145,4 +178,11 @@ class BecsDebitWidget @JvmOverloads constructor(
)
)
}

interface ValidParamsCallback {
/**
* @param isValid if the current input is valid
*/
fun onInputChanged(isValid: Boolean)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,48 @@ internal class BecsDebitWidgetTest : BaseViewTest<BecsDebitWidgetTestActivity>(
.isEqualTo(4)
}

@Test
fun validParamsCallback_shouldBeCalledWhenInputChanges() {
var isValidArg = false
var callbacks = 0
becsDebitWidget.validParamsCallback = object : BecsDebitWidget.ValidParamsCallback {
override fun onInputChanged(isValid: Boolean) {
callbacks++
isValidArg = isValid
}
}

assertThat(isValidArg)
.isFalse()
assertThat(callbacks)
.isEqualTo(0)

nameEditText.setText("Jenny Rosen")
assertThat(isValidArg)
.isFalse()
assertThat(callbacks)
.isEqualTo(1)

emailEditText.setText("jrosen@example.com")
assertThat(isValidArg)
.isFalse()
assertThat(callbacks)
.isEqualTo(2)

bsbEditText.setText("000000")
assertThat(isValidArg)
.isFalse()
// there is an additional callback because "000000" is formatted to "000-000"
assertThat(callbacks)
.isEqualTo(4)

accountNumberEditText.setText("000123456")
assertThat(isValidArg)
.isTrue()
assertThat(callbacks)
.isEqualTo(5)
}

private companion object {
private const val VALID_BSB_NUMBER = "000000"
private const val VALID_ACCOUNT_NUMBER = "000123456"
Expand Down

0 comments on commit cd873bc

Please sign in to comment.