-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from Trendyol/Trendyol/feature/cart_input_view…
…/american_express_support Provide American Express support to cartInputView
- Loading branch information
Showing
13 changed files
with
166 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
libraries/card-input-view/src/main/java/com/trendyol/cardinputview/CreditCardType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.trendyol.cardinputview | ||
|
||
enum class CreditCardType( | ||
val prefixLength: Int, | ||
val prefixes: List<Int>, | ||
val cvvLength: Int, | ||
val digitPattern: String, | ||
) { | ||
MASTER_CARD(2, (51..55).toList(), 3, "xxxx xxxx xxxx xxxx"), | ||
VISA(1, listOf(4), 3, "xxxx xxxx xxxx xxxx"), | ||
AMERICAN_EXPRESS(2, listOf(34, 37), 4, "xxxx xxxxxx xxxxx"), | ||
DEFAULT(1, (0..9).toList(), 3, "xxxx xxxx xxxx xxxx"); | ||
|
||
companion object { | ||
|
||
fun getCreditCardType(creditCardTypes: List<CreditCardType>, creditCardNumber: String?): CreditCardType { | ||
if (creditCardNumber.isNullOrBlank() || creditCardTypes.isEmpty()) return DEFAULT | ||
|
||
run loop@{ | ||
creditCardTypes.forEach { type -> | ||
if (creditCardNumber.length < type.prefixLength) return@loop | ||
|
||
val uniquePrefix = creditCardNumber.substring(0 until type.prefixLength).toInt() | ||
val matches = type.prefixes.contains(uniquePrefix) | ||
if (matches) return type | ||
} | ||
} | ||
return DEFAULT | ||
} | ||
|
||
const val DIGIT_PATTERN_MARK = 'x' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...es/card-input-view/src/main/java/com/trendyol/cardinputview/formatter/CreditCardMasker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.trendyol.cardinputview.formatter | ||
|
||
import com.trendyol.cardinputview.CreditCardType | ||
import com.trendyol.cardinputview.CreditCardType.Companion.DIGIT_PATTERN_MARK | ||
import com.trendyol.cardinputview.removeNonDigitCharacters | ||
|
||
internal class CreditCardMasker { | ||
|
||
fun mask(creditCardNumber: String, supportedCardTypes: List<CreditCardType>): String { | ||
val clearedCreditCardNumber = creditCardNumber.removeNonDigitCharacters() | ||
val creditCardType = CreditCardType.getCreditCardType(supportedCardTypes, clearedCreditCardNumber) | ||
return format(clearedCreditCardNumber, creditCardType.digitPattern) | ||
} | ||
|
||
private fun format(cardNumber: String, pattern: String): String { | ||
var formattedCardNumber = "" | ||
var cardNumberIteratorIndex = 0 | ||
run loop@{ | ||
pattern.forEach { character -> | ||
if (character == DIGIT_PATTERN_MARK) { | ||
val digit = cardNumber.getOrNull(cardNumberIteratorIndex) ?: return@loop | ||
formattedCardNumber += digit | ||
cardNumberIteratorIndex++ | ||
} else { | ||
formattedCardNumber += character | ||
} | ||
} | ||
} | ||
return formattedCardNumber.trim() | ||
} | ||
} |
18 changes: 15 additions & 3 deletions
18
...card-input-view/src/main/java/com/trendyol/cardinputview/validator/CreditCardValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
package com.trendyol.cardinputview.validator | ||
|
||
import com.trendyol.cardinputview.CreditCardType | ||
|
||
internal class CreditCardValidator { | ||
|
||
fun isCardNumberValid(cardNumber: String?): Boolean { | ||
return LuhnValidator().isValid(cardNumber) | ||
fun isCardNumberValid(cardNumber: String?, supportedCardTypes: List<CreditCardType>): Boolean { | ||
return LuhnValidator().isValid(cardNumber, supportedCardTypes) | ||
} | ||
|
||
fun isCvvValid(cvv: String?): Boolean = cvv?.matches("\\d\\d\\d".toRegex()) == true | ||
fun isCvvValid(cvv: String?, creditCardNumber: String?, supportedCardTypes: List<CreditCardType>): Boolean { | ||
val cvvLength = CreditCardType.getCreditCardType(supportedCardTypes, creditCardNumber).cvvLength | ||
var cvvRegex = "" | ||
repeat(cvvLength) { cvvRegex += NUMBER_REGEX_PATTERN } | ||
|
||
return cvv?.matches(cvvRegex.toRegex()) == true | ||
} | ||
|
||
fun isExpiryMonthValid(month: String?) = month?.matches("\\d\\d".toRegex()) == true | ||
|
||
fun isExpiryYearValid(year: String?) = year?.matches("\\d\\d".toRegex()) == true | ||
|
||
companion object { | ||
private const val NUMBER_REGEX_PATTERN = "\\d" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters