Skip to content

Commit

Permalink
Add min length validation to BECS account number (#2293)
Browse files Browse the repository at this point in the history
- Add validation for BECS account number min length
- Update account number min length based on BSB prefix
  • Loading branch information
mshafrir-stripe authored Mar 17, 2020
1 parent 1955cc3 commit c6512a9
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 8 deletions.
2 changes: 2 additions & 0 deletions stripe/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@
<string name="becs_widget_bsb_incomplete" tools:ignore="MissingTranslation">The BSB you entered is incomplete.</string>
<!-- BECS Debit Widget - account number field - required error message -->
<string name="becs_widget_account_number_required" tools:ignore="MissingTranslation">Your account number is required.</string>
<!-- BECS Debit Widget - account number field - incomplete error message -->
<string name="becs_widget_account_number_incomplete" tools:ignore="MissingTranslation">Your account number is incomplete.</string>
<!-- BECS Debit Widget - name field - required error message -->
<string name="becs_widget_name_required" tools:ignore="MissingTranslation">Your name is required.</string>
<!-- BECS Debit Widget - email address field - required error message -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,32 @@ import android.text.Editable
import android.text.InputFilter
import android.text.method.DigitsKeyListener
import android.util.AttributeSet
import com.stripe.android.R

internal class BecsDebitAccountNumberEditText @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = androidx.appcompat.R.attr.editTextStyle
) : StripeEditText(context, attrs, defStyleAttr) {

val accountNumber: String?
get() {
errorMessage = when {
fieldText.isBlank() -> {
resources.getString(R.string.becs_widget_account_number_required)
}
fieldText.length < minLength -> {
resources.getString(R.string.becs_widget_account_number_incomplete)
}
else -> {
null
}
}
return fieldText.takeIf { errorMessage == null }
}

var minLength: Int = DEFAULT_MIN_LENGTH

init {
filters = arrayOf(InputFilter.LengthFilter(MAX_LENGTH))
keyListener = DigitsKeyListener.getInstance(false, true)
Expand All @@ -23,7 +42,8 @@ internal class BecsDebitAccountNumberEditText @JvmOverloads constructor(
})
}

private companion object {
internal companion object {
internal const val DEFAULT_MIN_LENGTH = 5
private const val MAX_LENGTH = 9
}
}
33 changes: 27 additions & 6 deletions stripe/src/main/java/com/stripe/android/view/BecsDebitWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ class BecsDebitWidget @JvmOverloads constructor(
viewBinding.bsbTextInputLayout.helperText = null
viewBinding.bsbTextInputLayout.isHelperTextEnabled = false
}

viewBinding.accountNumberEditText.minLength = when (bank?.prefix?.take(2)) {
// Stripe
"00" -> 9

// ANZ: 9 digits https://www.anz.com.au/support/help/
"01" -> 9

// NAB: 9 digits
// https://www.nab.com.au/business/accounts/business-accounts-online-application-help
"08" -> 9

// Commonwealth/CBA: 8 digits
// https://www.commbank.com.au/support.digital-banking.confirm-account-number-digits.html
"06" -> 8

// Westpac/WBC: 6 digits
"03", "73" -> 6

// Cuscal: 4 digits(?)
"80" -> 4

else -> BecsDebitAccountNumberEditText.DEFAULT_MIN_LENGTH
}
}

viewBinding.bsbEditText.onCompletedCallback = {
Expand Down Expand Up @@ -79,9 +103,6 @@ class BecsDebitWidget @JvmOverloads constructor(
ErrorListener(viewBinding.bsbTextInputLayout)
)

viewBinding.accountNumberEditText.errorMessage = resources.getString(
R.string.becs_widget_account_number_required
)
viewBinding.accountNumberEditText.setErrorMessageListener(
ErrorListener(viewBinding.accountNumberTextInputLayout)
)
Expand All @@ -104,15 +125,15 @@ class BecsDebitWidget @JvmOverloads constructor(
val name = viewBinding.nameEditText.fieldText
val email = viewBinding.emailEditText.fieldText
val bsbNumber = viewBinding.bsbEditText.bsb
val accountNumber = viewBinding.accountNumberEditText.fieldText
val accountNumber = viewBinding.accountNumberEditText.accountNumber

viewBinding.nameEditText.shouldShowError = name.isBlank()
viewBinding.emailEditText.shouldShowError = email.isBlank()
viewBinding.bsbEditText.shouldShowError = bsbNumber.isNullOrBlank()
viewBinding.accountNumberEditText.shouldShowError = accountNumber.isBlank()
viewBinding.accountNumberEditText.shouldShowError = accountNumber.isNullOrBlank()

if (name.isBlank() || email.isBlank() || bsbNumber.isNullOrBlank() ||
accountNumber.isBlank()) {
accountNumber.isNullOrBlank()) {
return null
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.stripe.android.view

import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import kotlin.test.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class BecsDebitAccountNumberEditTextTest {
private val accountNumberEditText = BecsDebitAccountNumberEditText(
ApplicationProvider.getApplicationContext()
)

@Test
fun accountNumber_whenEmpty_shouldReturnCorrectErrorMessage() {
accountNumberEditText.setText("")
assertThat(accountNumberEditText.accountNumber)
.isNull()
assertThat(accountNumberEditText.errorMessage)
.isEqualTo("Your account number is required.")
}

@Test
fun accountNumber_whenIncomplete_shouldReturnCorrectErrorMessage() {
accountNumberEditText.minLength = 9
accountNumberEditText.setText("1234")
assertThat(accountNumberEditText.accountNumber)
.isNull()
assertThat(accountNumberEditText.errorMessage)
.isEqualTo("Your account number is incomplete.")
}

@Test
fun accountNumber_whenComplete_shouldNotHaveErrorMessage() {
accountNumberEditText.minLength = 5
accountNumberEditText.setText("123456")
assertThat(accountNumberEditText.accountNumber)
.isEqualTo("123456")
assertThat(accountNumberEditText.errorMessage)
.isNull()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class BecsDebitWidgetTest : BaseViewTest<BecsDebitWidgetTestActivity>(
private val bsbEditText: BecsDebitBsbEditText by lazy {
becsDebitWidget.viewBinding.bsbEditText
}
private val accountNumberEditText: StripeEditText by lazy {
private val accountNumberEditText: BecsDebitAccountNumberEditText by lazy {
becsDebitWidget.viewBinding.accountNumberEditText
}

Expand Down Expand Up @@ -95,6 +95,23 @@ internal class BecsDebitWidgetTest : BaseViewTest<BecsDebitWidgetTestActivity>(
)
}

@Test
fun bsbChange_shouldUpdateAccountNumberEditTextMinLength() {
// default
assertThat(accountNumberEditText.minLength)
.isEqualTo(5)

// Stripe BSB
bsbEditText.setText("00")
assertThat(accountNumberEditText.minLength)
.isEqualTo(9)

// Stripe BSB
bsbEditText.setText("80")
assertThat(accountNumberEditText.minLength)
.isEqualTo(4)
}

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

0 comments on commit c6512a9

Please sign in to comment.