@Suppress("MagicNumber") @VisibleForTesting internal fun luhnAlgorithmValidation(creditCardNumber: String): Boolean { var checksum = 0 val reversedCardNumber = creditCardNumber.reversed() for (index in reversedCardNumber.indices) { val digit = Character.getNumericValue(reversedCardNumber[index]) checksum += if (index % 2 == 0) digit else (digit * 2).let { (it / 10) + (it % 10) } } return (checksum % 10) == 0 }
Implementation of Luhn Algorithm validation (https://en.wikipedia.org/wiki/Luhn_algorithm)