diff --git a/lib/src/main/java/com/raqun/oyster/card/CardType.kt b/lib/src/main/java/com/raqun/oyster/card/CardType.kt new file mode 100644 index 0000000..51d9dec --- /dev/null +++ b/lib/src/main/java/com/raqun/oyster/card/CardType.kt @@ -0,0 +1,12 @@ +package com.raqun.oyster.card + +enum class CardType { + UNKNOWN, + VISA, + MASTER_CARD, + AMERICAN_EXPRESS, + DINERS_CLUB, + DISCOVER, + JCB, + JCB15 +} \ No newline at end of file diff --git a/lib/src/main/java/com/raqun/oyster/card/CardUtil.kt b/lib/src/main/java/com/raqun/oyster/card/CardUtil.kt new file mode 100644 index 0000000..601358e --- /dev/null +++ b/lib/src/main/java/com/raqun/oyster/card/CardUtil.kt @@ -0,0 +1,24 @@ +package com.raqun.oyster.card + +class CardUtil { + + companion object { + fun getType(cardNumber: String): CardType { + val regexMap = mapOf( + TYPE_REGEX_VISA to CardType.VISA, + TYPE_REGEX_MASTER_CARD to CardType.MASTER_CARD, + TYPE_REGEX_AMEX to CardType.AMERICAN_EXPRESS, + TYPE_REGEX_DINERS_CLUB to CardType.DINERS_CLUB, + TYPE_REGEX_DISCOVER to CardType.DISCOVER, + TYPE_REGEX_JCB to CardType.JCB, + TYPE_REGEX_JCB15 to CardType.JCB15 + ) + for (regex in regexMap) { + if (cardNumber.cleanCardNumber().matches(regex.key.toRegex())) { + return regex.value + } + } + return CardType.UNKNOWN + } + } +} \ No newline at end of file