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

Add support for Diners Club 16 #2211

Merged
merged 3 commits into from
Feb 24, 2020
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
8 changes: 4 additions & 4 deletions stripe/src/main/java/com/stripe/android/CardUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ object CardUtils {
* or [CardBrand.Unknown] if it can't be determined
*/
@JvmStatic
fun getPossibleCardType(cardNumber: String?): CardBrand {
return getPossibleCardType(cardNumber, true)
fun getPossibleCardBrand(cardNumber: String?): CardBrand {
return getPossibleCardBrand(cardNumber, true)
}

/**
Expand Down Expand Up @@ -76,10 +76,10 @@ object CardUtils {
*/
internal fun isValidCardLength(cardNumber: String?): Boolean {
return cardNumber != null &&
getPossibleCardType(cardNumber, false).isValidCardNumberLength(cardNumber)
getPossibleCardBrand(cardNumber, false).isValidCardNumberLength(cardNumber)
}

private fun getPossibleCardType(cardNumber: String?, shouldNormalize: Boolean): CardBrand {
private fun getPossibleCardBrand(cardNumber: String?, shouldNormalize: Boolean): CardBrand {
if (cardNumber.isNullOrBlank()) {
return CardBrand.Unknown
}
Expand Down
2 changes: 1 addition & 1 deletion stripe/src/main/java/com/stripe/android/model/Card.kt
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ data class Card internal constructor(
addressZipCheck = addressZipCheck.takeUnless { it.isNullOrBlank() },
addressCountry = addressCountry.takeUnless { it.isNullOrBlank() },
last4 = last4,
brand = brand ?: CardUtils.getPossibleCardType(number),
brand = brand ?: CardUtils.getPossibleCardBrand(number),
fingerprint = fingerprint.takeUnless { it.isNullOrBlank() },
funding = funding,
country = country.takeUnless { it.isNullOrBlank() },
Expand Down
55 changes: 41 additions & 14 deletions stripe/src/main/java/com/stripe/android/model/CardBrand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ enum class CardBrand(
val cvcLength: Set<Int> = setOf(3),

/**
* The max length when the card number is formatted with spaces (e.g. "4242 4242 4242 4242")
*/
val maxLengthWithSpaces: Int = 19,

/**
* The max length when the card number is formatted without spaces (e.g. "4242424242424242")
* The default max length when the card number is formatted without spaces (e.g. "4242424242424242")
*
* Note that [CardBrand.DinersClub]'s max length depends on the BIN (e.g. card number prefix).
* In the case of a [CardBrand.DinersClub] card, use [getMaxLengthForCardNumber].
*/
val maxLengthWithoutSpaces: Int = 16,
val defaultMaxLength: Int = 16,

/**
* Based on [Issuer identification number table](http://en.wikipedia.org/wiki/Bank_card_number#Issuer_identification_number_.28IIN.29)
Expand All @@ -37,16 +35,20 @@ enum class CardBrand(
* formatted to "4242 4242 4242 4242". The spaces in that number are at the positions
* specified by [spacePositions].
*/
val spacePositions: Set<Int> = setOf(4, 9, 14)
val spacePositions: Set<Int> = setOf(4, 9, 14),

/**
* By default, a [CardBrand] does not have variants.
*/
private val variantMaxLength: Map<String, Int> = emptyMap()
) {
AmericanExpress(
"amex",
"American Express",
R.drawable.stripe_ic_amex,
cvcIcon = R.drawable.stripe_ic_cvc_amex,
cvcLength = setOf(3, 4),
maxLengthWithSpaces = 17,
maxLengthWithoutSpaces = 15,
defaultMaxLength = 15,
prefixes = listOf("34", "37"),
spacePositions = setOf(4, 11)
),
Expand All @@ -65,14 +67,21 @@ enum class CardBrand(
prefixes = listOf("35")
),

// 14-digit Diners Club
DinersClub(
"diners",
"Diners Club",
R.drawable.stripe_ic_diners,
maxLengthWithSpaces = 17,
maxLengthWithoutSpaces = 14,
defaultMaxLength = 16,
prefixes = listOf(
"300", "301", "302", "303", "304", "305", "309", "36", "38", "39"
// 14-digits
"36",

// 16-digits
"30", "38", "39"
),
variantMaxLength = mapOf(
"36" to 14
)
),

Expand Down Expand Up @@ -108,6 +117,8 @@ enum class CardBrand(
cvcLength = setOf(3, 4)
);

val defaultMaxLengthWithSpaces: Int = defaultMaxLength + spacePositions.size

val maxCvcLength: Int
get() {
return cvcLength.max() ?: CVC_COMMON_LENGTH
Expand All @@ -122,7 +133,7 @@ enum class CardBrand(
*/
fun isValidCardNumberLength(cardNumber: String?): Boolean {
return cardNumber != null && Unknown != this &&
cardNumber.length == maxLengthWithoutSpaces
cardNumber.length == getMaxLengthForCardNumber(cardNumber)
}

fun isValidCvc(cvc: String): Boolean {
Expand All @@ -134,6 +145,22 @@ enum class CardBrand(
return maxCvcLength == cvcLength
}

/**
* If the [CardBrand] has variants, and the [cardNumber] starts with one of the variant
* prefixes, return the length for that variant. Otherwise, return [defaultMaxLength].
*
* Note: currently only [CardBrand.DinersClub] has variants
*/
fun getMaxLengthForCardNumber(cardNumber: String): Int {
return variantMaxLength.entries.firstOrNull { (key, _) ->
cardNumber.startsWith(key)
}?.value ?: defaultMaxLength
}

fun getMaxLengthWithSpacesForCardNumber(cardNumber: String): Int {
return getMaxLengthForCardNumber(cardNumber) + spacePositions.size
}

companion object {
/**
* @param cardNumber a card number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ class CardInputWidget @JvmOverloads constructor(
get() {
return when (brand) {
CardBrand.AmericanExpress -> PEEK_TEXT_AMEX
CardBrand.DinersClub -> PEEK_TEXT_DINERS
CardBrand.DinersClub -> PEEK_TEXT_DINERS_14
else -> PEEK_TEXT_COMMON
}
}
Expand Down Expand Up @@ -1290,7 +1290,7 @@ class CardInputWidget @JvmOverloads constructor(
internal const val LOGGING_TOKEN = "CardInputView"

private const val PEEK_TEXT_COMMON = "4242"
private const val PEEK_TEXT_DINERS = "88"
private const val PEEK_TEXT_DINERS_14 = "88"
private const val PEEK_TEXT_AMEX = "34343"

private const val CVC_PLACEHOLDER_COMMON = "CVC"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ class CardNumberEditText @JvmOverloads constructor(

@VisibleForTesting
var cardBrand: CardBrand = CardBrand.Unknown
internal set
internal set(value) {
val prevBrand = field
field = value
if (value != prevBrand) {
brandChangeCallback(cardBrand)
updateLengthFilter()
}
}

@JvmSynthetic
internal var brandChangeCallback: (CardBrand) -> Unit = {}
Expand All @@ -39,7 +46,7 @@ class CardNumberEditText @JvmOverloads constructor(

val lengthMax: Int
get() {
return cardBrand.maxLengthWithSpaces
return cardBrand.getMaxLengthWithSpacesForCardNumber(fieldText)
}

private var ignoreChanges = false
Expand Down Expand Up @@ -198,23 +205,9 @@ class CardNumberEditText @JvmOverloads constructor(
})
}

private fun updateCardBrand(brand: CardBrand) {
if (cardBrand == brand) {
return
}

val oldLength = lengthMax

cardBrand = brand
brandChangeCallback(cardBrand)
if (oldLength != lengthMax) {
updateLengthFilter()
}
}

@JvmSynthetic
internal fun updateCardBrandFromNumber(partialNumber: String) {
updateCardBrand(CardUtils.getPossibleCardType(partialNumber))
cardBrand = CardUtils.getPossibleCardBrand(partialNumber)
}

internal companion object {
Expand Down
34 changes: 28 additions & 6 deletions stripe/src/test/java/com/stripe/android/CardNumberFixtures.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
package com.stripe.android

/**
* See [Basic test card numbers](https://stripe.com/docs/testing#cards)
*/
internal object CardNumberFixtures {
const val VALID_AMEX_NO_SPACES: String = "378282246310005"
const val VALID_AMEX_WITH_SPACES: String = "3782 822463 10005"
const val VALID_DINERS_CLUB_NO_SPACES: String = "30569309025904"
const val VALID_DINERS_CLUB_WITH_SPACES: String = "3056 9309 0259 04"
const val VALID_VISA_NO_SPACES: String = "4242424242424242"
const val VALID_VISA_WITH_SPACES: String = "4242 4242 4242 4242"
const val AMEX_NO_SPACES: String = "378282246310005"
const val AMEX_WITH_SPACES: String = "3782 822463 10005"

const val VISA_NO_SPACES: String = "4242424242424242"
const val VISA_WITH_SPACES: String = "4242 4242 4242 4242"

const val VISA_DEBIT_NO_SPACES: String = "4000056655665556"

const val MASTERCARD_NO_SPACES = "5555555555554444"
const val MASTERCARD_WITH_SPACES = "5555 5555 5555 4444"

const val DINERS_CLUB_14_NO_SPACES: String = "36227206271667"
const val DINERS_CLUB_14_WITH_SPACES: String = "3622 7206 2716 67"

const val DINERS_CLUB_16_NO_SPACES = "3056930009020004"
const val DINERS_CLUB_16_WITH_SPACES = "3056 9300 0902 0004"

const val DISCOVER_NO_SPACES = "6011000990139424"
const val DISCOVER_WITH_SPACES = "6011 0009 9013 9424"

const val JCB_NO_SPACES = "3566002020360505"
const val JCB_WITH_SPACES = "3566 0020 2036 0505"

const val UNIONPAY_NO_SPACES = "6200000000000005"
const val UNIONPAY_WITH_SPACES = "6200 0000 0000 0005"
}
Loading