diff --git a/components/support/utils/src/main/java/mozilla/components/support/utils/CreditCardUtils.kt b/components/support/utils/src/main/java/mozilla/components/support/utils/CreditCardUtils.kt index a1f801e0a7a..20dff80e82e 100644 --- a/components/support/utils/src/main/java/mozilla/components/support/utils/CreditCardUtils.kt +++ b/components/support/utils/src/main/java/mozilla/components/support/utils/CreditCardUtils.kt @@ -5,6 +5,7 @@ package mozilla.components.support.utils import androidx.annotation.DrawableRes +import mozilla.components.support.utils.ext.toCreditCardNumber import kotlin.math.floor import kotlin.math.log10 @@ -253,22 +254,23 @@ internal object CreditCardUtils { */ @Suppress("ComplexMethod") fun getCreditCardIIN(cardNumber: String): CreditCardIIN? { + val safeCardNumber = cardNumber.toCreditCardNumber() for (issuer in creditCardIINs) { if (issuer.cardNumberMaxLength.size == 1 && - issuer.cardNumberMaxLength[0] != cardNumber.length + issuer.cardNumberMaxLength[0] != safeCardNumber.length ) { continue } else if (issuer.cardNumberMaxLength.size > 1 && ( - cardNumber.length < issuer.cardNumberMaxLength[0] || - cardNumber.length > issuer.cardNumberMaxLength[1] + safeCardNumber.length < issuer.cardNumberMaxLength[0] || + safeCardNumber.length > issuer.cardNumberMaxLength[1] ) ) { continue } val prefixLength = floor(log10(issuer.startRange.toDouble())) + 1 - val prefix = cardNumber.substring(0, prefixLength.toInt()).toInt() + val prefix = safeCardNumber.substring(0, prefixLength.toInt()).toInt() if (prefix >= issuer.startRange && prefix <= issuer.endRange) { return issuer diff --git a/components/support/utils/src/main/java/mozilla/components/support/utils/ext/String.kt b/components/support/utils/src/main/java/mozilla/components/support/utils/ext/String.kt new file mode 100644 index 00000000000..163624802cf --- /dev/null +++ b/components/support/utils/src/main/java/mozilla/components/support/utils/ext/String.kt @@ -0,0 +1,13 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package mozilla.components.support.utils.ext + +/** + * Strips characters other than digits from a string. + * Used to strip a credit card number user input of spaces and separators. + */ +fun String.toCreditCardNumber(): String { + return this.filter { it.isDigit() } +} diff --git a/components/support/utils/src/test/java/mozilla/components/support/utils/CreditCardUtilsTest.kt b/components/support/utils/src/test/java/mozilla/components/support/utils/CreditCardUtilsTest.kt index 230376abd40..791b288fc4a 100644 --- a/components/support/utils/src/test/java/mozilla/components/support/utils/CreditCardUtilsTest.kt +++ b/components/support/utils/src/test/java/mozilla/components/support/utils/CreditCardUtilsTest.kt @@ -70,7 +70,8 @@ class CreditCardUtilsTest { Pair("6278592974938779", "unionpay"), Pair("8171999927660000", "unionpay"), Pair("30569309025904", "diners"), - Pair("38520000023237", "diners") + Pair("38520000023237", "diners"), + Pair("3 8 5 2 0 0 0 0 0 2 3 2 3 7", "diners") ) for ((cardNumber, cardType) in recognizedCards) { diff --git a/components/support/utils/src/test/java/mozilla/components/support/utils/ext/StringTest.kt b/components/support/utils/src/test/java/mozilla/components/support/utils/ext/StringTest.kt new file mode 100644 index 00000000000..a7b8fcdb102 --- /dev/null +++ b/components/support/utils/src/test/java/mozilla/components/support/utils/ext/StringTest.kt @@ -0,0 +1,22 @@ +package mozilla.components.support.utils.ext + +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class StringTest { + + @Test + fun `GIVEN a string credit card number WHEN calling toCreditCardNumber THEN any character that is not a digit will removed`() { + val number = "385 - 2 0 0 - 0 0 0 2 3 2 3 7" + val validResult = "38520000023237" + + assertEquals(validResult, number.toCreditCardNumber()) + } +} diff --git a/docs/changelog.md b/docs/changelog.md index 01060bbb36a..eb2d6b0a632 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -15,6 +15,10 @@ permalink: /changelog/ * Removes deprecated `GeckoLoginDelegateWrapper`. Please use `GeckoAutocompleteStorageDelegate`. [#11311](https://github.com/mozilla-mobile/android-components/issues/11311) * Added setting for HTTPS-Only mode [#5935](https://github.com/mozilla-mobile/focus-android/issues/5935) +* **support-base** + * 🌟️ Add `String.toCreditCardNumber` for removing characters other than digits from a credit card string number. + * 🚒 Bug fixed [issue #11360](https://github.com/mozilla-mobile/android-components/issues/11360) - Crash when saving credit cards + # 95.0.0 * [Commits](https://github.com/mozilla-mobile/android-components/compare/v94.0.0...v95.0.0) * [Milestone](https://github.com/mozilla-mobile/android-components/milestone/142?closed=1)