-
Notifications
You must be signed in to change notification settings - Fork 9
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 #332 from Adyen/feature/exposeCardValidators
Add support for card validators
- Loading branch information
Showing
22 changed files
with
1,025 additions
and
13 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
63 changes: 63 additions & 0 deletions
63
android/src/main/kotlin/com/adyen/checkout/flutter/apiOnly/CardValidation.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,63 @@ | ||
package com.adyen.checkout.flutter.apiOnly | ||
|
||
import CardExpiryDateValidationResultDTO | ||
import CardNumberValidationResultDTO | ||
import CardSecurityCodeValidationResultDTO | ||
import com.adyen.checkout.core.CardBrand | ||
import com.adyen.checkout.core.ui.model.ExpiryDate | ||
import com.adyen.checkout.core.ui.validation.CardExpiryDateValidationResult | ||
import com.adyen.checkout.core.ui.validation.CardExpiryDateValidator | ||
import com.adyen.checkout.core.ui.validation.CardNumberValidationResult | ||
import com.adyen.checkout.core.ui.validation.CardNumberValidator | ||
import com.adyen.checkout.core.ui.validation.CardSecurityCodeValidationResult | ||
import com.adyen.checkout.core.ui.validation.CardSecurityCodeValidator | ||
|
||
internal object CardValidation { | ||
fun validateCardNumber( | ||
cardNumber: String, | ||
enableLuhnCheck: Boolean | ||
): CardNumberValidationResultDTO { | ||
val validationResult = CardNumberValidator.validateCardNumber(cardNumber, enableLuhnCheck) | ||
return when (validationResult) { | ||
is CardNumberValidationResult.Valid -> CardNumberValidationResultDTO.VALID | ||
is CardNumberValidationResult.Invalid.IllegalCharacters -> | ||
CardNumberValidationResultDTO.INVALIDILLEGALCHARACTERS | ||
|
||
is CardNumberValidationResult.Invalid.TooLong -> CardNumberValidationResultDTO.INVALIDTOOLONG | ||
is CardNumberValidationResult.Invalid.TooShort -> CardNumberValidationResultDTO.INVALIDTOOSHORT | ||
is CardNumberValidationResult.Invalid.LuhnCheck -> CardNumberValidationResultDTO.INVALIDLUHNCHECK | ||
else -> CardNumberValidationResultDTO.INVALIDOTHERREASON | ||
} | ||
} | ||
|
||
fun validateCardExpiryDate( | ||
expiryMonth: String, | ||
expiryYear: String | ||
): CardExpiryDateValidationResultDTO { | ||
val expireMonth = expiryMonth.toIntOrNull() ?: return CardExpiryDateValidationResultDTO.NONPARSEABLEDATE | ||
val expireYear = expiryYear.toIntOrNull() ?: return CardExpiryDateValidationResultDTO.NONPARSEABLEDATE | ||
val expiryDate = ExpiryDate(expireMonth, expireYear) | ||
val validationResult = CardExpiryDateValidator.validateExpiryDate(expiryDate) | ||
return when (validationResult) { | ||
is CardExpiryDateValidationResult.Valid -> CardExpiryDateValidationResultDTO.VALID | ||
is CardExpiryDateValidationResult.Invalid.TooFarInTheFuture -> | ||
CardExpiryDateValidationResultDTO.INVALIDTOOFARINTHEFUTURE | ||
is CardExpiryDateValidationResult.Invalid.TooOld -> CardExpiryDateValidationResultDTO.INVALIDTOOOLD | ||
is CardExpiryDateValidationResult.Invalid.NonParseableDate -> | ||
CardExpiryDateValidationResultDTO.NONPARSEABLEDATE | ||
else -> CardExpiryDateValidationResultDTO.INVALIDOTHERREASON | ||
} | ||
} | ||
|
||
fun validateCardSecurityCode( | ||
securityCode: String, | ||
cardBrandTxVariant: String? | ||
): CardSecurityCodeValidationResultDTO { | ||
val cardBrand = cardBrandTxVariant?.let { CardBrand(it) } | ||
val validationResult = CardSecurityCodeValidator.validateSecurityCode(securityCode, cardBrand) | ||
return when (validationResult) { | ||
is CardSecurityCodeValidationResult.Valid -> CardSecurityCodeValidationResultDTO.VALID | ||
is CardSecurityCodeValidationResult.Invalid -> CardSecurityCodeValidationResultDTO.INVALID | ||
} | ||
} | ||
} |
Oops, something went wrong.