Updated on 2026-08-14

This commit is contained in:
Tangem 2020-12-04 11:48:21 +03:00
parent 91a975a9c4
commit e3d0d7e212

View file

@ -8,10 +8,10 @@ class TwinsHelper {
fun getTwinCardNumber(cardId: String): TwinCardNumber? {
return when {
firstCardBatches.map { cardId.startsWith(it) }.contains(true) -> {
firstCardSeries.map { cardId.startsWith(it) }.contains(true) -> {
TwinCardNumber.First
}
secondCardBatches.map { cardId.startsWith(it) }.contains(true) -> {
secondCardSeries.map { cardId.startsWith(it) }.contains(true) -> {
TwinCardNumber.Second
}
else -> {
@ -21,23 +21,23 @@ class TwinsHelper {
}
fun getTwinsCardId(cardId: String): String? {
val cardIdWithNewBatch = when (getTwinCardNumber(cardId) ?: return null) {
val cardIdWithNewSeries = when (getTwinCardNumber(cardId) ?: return null) {
TwinCardNumber.First -> {
val batchIndex = if (cardId.startsWith(firstCardBatches[1])) 1 else 2
cardId.replace(firstCardBatches[batchIndex], secondCardBatches[batchIndex])
val index = if (cardId.startsWith(firstCardSeries[0])) 0 else 1
cardId.replace(firstCardSeries[index], secondCardSeries[index])
}
TwinCardNumber.Second -> {
val batchIndex = if (cardId.startsWith(secondCardBatches[1])) 1 else 2
cardId.replace(secondCardBatches[batchIndex], firstCardBatches[batchIndex])
val index = if (cardId.startsWith(secondCardSeries[0])) 0 else 1
cardId.replace(secondCardSeries[index], firstCardSeries[index])
}
}
val cardIdWithoutChecksum = cardIdWithNewBatch.dropLast(1)
val cardIdWithoutChecksum = cardIdWithNewSeries.dropLast(1)
val checkSum = cardIdWithoutChecksum.calculateLuhn()
return cardIdWithoutChecksum + checkSum
}
private val firstCardBatches = listOf("CB61", "CB64")
private val secondCardBatches = listOf("CB62", "CB65")
private val firstCardSeries = listOf("CB61", "CB64")
private val secondCardSeries = listOf("CB62", "CB65")
}
}