Skip to content

Commit

Permalink
feat: updating card bin depending on card length (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
jleon15 authored Mar 29, 2023
1 parent 0c49a17 commit 6d6585b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CardNumberElement @JvmOverloads constructor(

cardMetadata = CardMetadata(
cardBrandDetails?.brand,
cardDigits?.take(6).takeIf { isMaskSatisfied },
cardDigits?.take(cardDigits.binLength()).takeIf { isMaskSatisfied },
cardDigits?.takeLast(4).takeIf { isMaskSatisfied },
)
cvcMask = cardBrandDetails?.cvcMask
Expand All @@ -72,7 +72,7 @@ class CardNumberElement @JvmOverloads constructor(
eventDetails.add(
EventDetails(
EventDetails.CardBin,
value.take(6)
value.take(value.binLength())
)
)

Expand All @@ -98,6 +98,8 @@ class CardNumberElement @JvmOverloads constructor(
return transform?.apply(maskedValue) ?: maskedValue
}

private fun String.binLength() = if(this.length >= 16) 8 else 6

companion object {
private val digit = Regex("""\d""")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class CardNumberElementTests {
}
get { details }.any {
get { type }.isEqualTo(EventDetails.CardBin)
get { message }.isEqualTo("411111")
get { message }.isEqualTo("41111111")
}
get { details }.any {
get { type }.isEqualTo(EventDetails.CardLast4)
Expand All @@ -161,7 +161,7 @@ class CardNumberElementTests {
}
get { details }.any {
get { type }.isEqualTo(EventDetails.CardBin)
get { message }.isEqualTo("424242")
get { message }.isEqualTo("42424242")
}
get { details }.any {
get { type }.isEqualTo(EventDetails.CardLast4)
Expand All @@ -170,14 +170,38 @@ class CardNumberElementTests {
}
}

@Test
fun `ChangeEvent bin is computed properly for 16 digit or longer card numbers`() {
val changeEvents = mutableListOf<ChangeEvent>()
cardNumberElement.addChangeEventListener { changeEvents.add(it) }

cardNumberElement.setText("4111 1111 1111 1111")
expectThat(changeEvents.single().details).any {
get { type }.isEqualTo(EventDetails.CardBin)
get { message }.isEqualTo("41111111")
}
}

@Test
fun `ChangeEvent bin is computed properly for 15 digit or shorter card numbers`() {
val changeEvents = mutableListOf<ChangeEvent>()
cardNumberElement.addChangeEventListener { changeEvents.add(it) }

cardNumberElement.setText("3782 822463 10005")
expectThat(changeEvents.single().details).any {
get { type }.isEqualTo(EventDetails.CardBin)
get { message }.isEqualTo("378282")
}
}

@Test
fun `exposes card metadata for cards with satisfied masks`() {
cardNumberElement.setText("5432 1098 7654 3210")

expectThat(cardNumberElement) {
get { cardMetadata }.isNotNull().and {
get { brand }.isEqualTo(CardBrands.MASTERCARD.label)
get { bin }.isEqualTo("543210")
get { bin }.isEqualTo("54321098")
get { last4 }.isEqualTo("3210")
}
get { mask }.isEqualTo(ElementMask(CardBrandEnricher.CardMasks.MASK_4_8_12GAPS_16DIGITS))
Expand Down Expand Up @@ -217,4 +241,18 @@ class CardNumberElementTests {
get { isMaskSatisfied }.isFalse()
}
}

@Test
fun `card metadata bin is computed properly for 16 digit or longer card numbers`() {
cardNumberElement.setText("5432 1098 7654 3210")

expectThat(cardNumberElement.cardMetadata?.bin).isEqualTo("54321098")
}

@Test
fun `card metadata bin is computed properly for 15 digit or shorter card numbers`() {
cardNumberElement.setText("3782 822463 10005")

expectThat(cardNumberElement.cardMetadata?.bin).isEqualTo("378282")
}
}

0 comments on commit 6d6585b

Please sign in to comment.