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

Implement 8 chars bin #396

Merged
merged 1 commit into from
Aug 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.verygoodsecurity.vgscollect.core.model.state.tokenization.VGSVaultSto
import com.verygoodsecurity.vgscollect.util.extension.isNumeric
import com.verygoodsecurity.vgscollect.view.card.CardType
import com.verygoodsecurity.vgscollect.view.core.serializers.FieldDataSerializer
import java.lang.Integer.min
import java.text.SimpleDateFormat
import java.util.*

Expand Down Expand Up @@ -99,14 +100,13 @@ internal fun FieldContent.CreditCardExpDateContent.handleOutputFormat(

/** @suppress */
internal fun FieldContent.CardNumberContent.parseCardBin(): String {
return data?.run {
val numberSTR = data!!.replace("\\D".toRegex(), "")
if (numberSTR.length >= 6) {
numberSTR.substring(0, 6)
} else {
numberSTR.substring(0, numberSTR.length)
}
} ?: ""
val number = data?.replace("\\D".toRegex(), "") ?: ""
val binLength = if (allowed8DigitBIN(cardBrandName, number.length)) 8 else 6
return number.substring(0, min(number.length, binLength))
}

private fun allowed8DigitBIN(brand: String?, length: Int): Boolean {
return length >= 16 && (brand == CardType.MAESTRO.name || brand == CardType.MASTERCARD.name || brand == CardType.VISA.name)
}

/** @suppress */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.verygoodsecurity.vgscollect.view.card.validation.payment.ChecksumAlgo
import com.verygoodsecurity.vgscollect.view.card.validation.rules.PaymentCardNumberRule
import com.verygoodsecurity.vgscollect.view.internal.BaseInputField
import com.verygoodsecurity.vgscollect.view.internal.CardInputField
import com.verygoodsecurity.vgscollect.view.internal.DateInputField
import com.verygoodsecurity.vgscollect.widget.VGSCardNumberEditText
import org.junit.Assert.*
import org.junit.Before
Expand Down Expand Up @@ -264,7 +263,7 @@ class VGSCardNumberEditTextTest {
stateResult.contentLength = text.length
stateResult.fieldName = "number"
stateResult.fieldType = FieldType.CARD_NUMBER
stateResult.bin = "411111"
stateResult.bin = "41111111"
stateResult.last = "1111"
stateResult.number = "411111######1111"
stateResult.drawableBrandResId = R.drawable.ic_visa_dark
Expand Down Expand Up @@ -553,7 +552,7 @@ class VGSCardNumberEditTextTest {
assertNotNull(state0)
assertEquals(true, state0!!.isValid)
assertEquals(16, state0.contentLength)
assertEquals("411111", state0.bin)
assertEquals("41111111", state0.bin)


view.setText("0111111111111111")
Expand Down Expand Up @@ -655,7 +654,7 @@ class VGSCardNumberEditTextTest {
assertNotNull(state1)
assertEquals(true, state1!!.isValid)
assertEquals(16, state1.contentLength)
assertEquals("411111", state1.bin)
assertEquals("41111111", state1.bin)


view.setText("411111111111111")
Expand Down Expand Up @@ -779,7 +778,7 @@ class VGSCardNumberEditTextTest {
)

val stateResult = FieldState.CardNumberState()
stateResult.bin = "411111"
stateResult.bin = "41111111"
stateResult.last = "1111"
stateResult.number = "411111######1111"
stateResult.cardBrand = CardType.VISA.name
Expand Down Expand Up @@ -813,9 +812,9 @@ class VGSCardNumberEditTextTest {
with(view.getState()) {
assertNotNull(this)

assertEquals(stateResult.bin, this!!.bin)
assertEquals(stateResult.last, this.last)
assertEquals(stateResult.last, this!!.last)
assertEquals(stateResult.number, this.number)
assertNotEquals(stateResult.bin, this.bin)
assertNotEquals(stateResult.cardBrand, this.cardBrand)
assertNotEquals(stateResult.drawableBrandResId, this.drawableBrandResId)

Expand Down