-
Notifications
You must be signed in to change notification settings - Fork 77
v6 - Improve expiry date input/output transformations and validation #2654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da60965
Extract DigitOnlyInputTransformation into separate class that can be …
3edb05b
Adjust input and output transformations for expiry date field
jreij 34edb51
Minor typo fix
jreij 8b44243
Simplify expiry date validation logic and classes
jreij 517b843
Update .api files
jreij 76358a0
Rely on SimpleDateFormat to parse and validate dates instead of manua…
jreij 6eaa082
Extract date formatter into class level properties to avoid reinitial…
jreij File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
card/src/main/java/com/adyen/checkout/card/internal/helper/ExpiryDateParser.kt
This file contains hidden or 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,60 @@ | ||
| /* | ||
| * Copyright (c) 2026 Adyen N.V. | ||
| * | ||
| * This file is open source and available under the MIT license. See the LICENSE file for more info. | ||
| * | ||
| * Created by josephj on 20/3/2026. | ||
| */ | ||
|
|
||
| package com.adyen.checkout.card.internal.helper | ||
|
|
||
| import com.adyen.checkout.core.common.internal.properties.ExpiryDateProperties.EXPIRY_DATE_MAX_LENGTH_NO_SEPARATORS | ||
| import java.text.ParseException | ||
| import java.text.SimpleDateFormat | ||
| import java.util.Date | ||
| import java.util.Locale | ||
|
|
||
| internal object ExpiryDateParser { | ||
| private val PARSING_FORMATTER = getFormatter("MMyy") | ||
| private val MONTH_FORMATTER = getFormatter("MM") | ||
| private val SHORT_YEAR_FORMATTER = getFormatter("yy") | ||
| private val FULL_YEAR_FORMATTER = getFormatter("yyyy") | ||
|
|
||
| /** | ||
| * Parses digit only input into a pair of expiryMonth and expiryYear | ||
| * The input must be 4 digits long, 2-digit month followed by 2-digit year (MMYY) | ||
| * Automatically adds the year prefix if returnFullYear is true (26 -> 2026) | ||
| * Returns null if input is invalid | ||
| */ | ||
| fun parseToMonthAndYear(expiryDate: String, returnFullYear: Boolean): Pair<String, String>? { | ||
| // this manual check is needed because SimpleDateFormat.parse will not enforce the length and will successfully | ||
| // parse some strings that are not 4 digits long | ||
| if (expiryDate.length != EXPIRY_DATE_MAX_LENGTH_NO_SEPARATORS) { | ||
| return null | ||
| } | ||
|
|
||
| val date = parseToDate(expiryDate) | ||
| return date?.let { | ||
| getMonthAndYear(date, returnFullYear) | ||
| } | ||
| } | ||
|
|
||
| private fun parseToDate(expiryDate: String): Date? { | ||
| return try { | ||
| PARSING_FORMATTER.parse(expiryDate) | ||
| } catch (_: ParseException) { | ||
| null | ||
| } | ||
| } | ||
|
|
||
| private fun getMonthAndYear(date: Date, returnFullYear: Boolean): Pair<String, String> { | ||
| val yearFormatter = if (returnFullYear) FULL_YEAR_FORMATTER else SHORT_YEAR_FORMATTER | ||
| return MONTH_FORMATTER.format(date) to yearFormatter.format(date) | ||
| } | ||
|
|
||
| private fun getFormatter(format: String): SimpleDateFormat { | ||
| return SimpleDateFormat(format, Locale.ROOT).apply { | ||
|
OscarSpruit marked this conversation as resolved.
|
||
| isLenient = false | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
64 changes: 64 additions & 0 deletions
64
card/src/test/java/com/adyen/checkout/card/internal/helper/ExpiryDateParserTest.kt
This file contains hidden or 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,64 @@ | ||
| /* | ||
| * Copyright (c) 2026 Adyen N.V. | ||
| * | ||
| * This file is open source and available under the MIT license. See the LICENSE file for more info. | ||
| * | ||
| * Created by josephj on 20/3/2026. | ||
| */ | ||
|
|
||
| package com.adyen.checkout.card.internal.helper | ||
|
|
||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Assertions.assertNull | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| internal class ExpiryDateParserTest { | ||
|
|
||
| @Test | ||
| fun `when input is valid and returnFullYear is true then return month and full year`() { | ||
| val result = ExpiryDateParser.parseToMonthAndYear("0326", true) | ||
| assertEquals("03" to "2026", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when input is valid and returnFullYear is false then return month and short year`() { | ||
| val result = ExpiryDateParser.parseToMonthAndYear("0326", false) | ||
| assertEquals("03" to "26", result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when input length is too short then return null`() { | ||
| val result = ExpiryDateParser.parseToMonthAndYear("032", true) | ||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when input length is too long then return null`() { | ||
| val result = ExpiryDateParser.parseToMonthAndYear("03261", true) | ||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when input is empty then return null`() { | ||
| val result = ExpiryDateParser.parseToMonthAndYear("", true) | ||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when input month is not valid then return null`() { | ||
| val result = ExpiryDateParser.parseToMonthAndYear("4520", true) | ||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when input has illegal characters then return null`() { | ||
| val result = ExpiryDateParser.parseToMonthAndYear("asbg", true) | ||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `when month is double digits then it is parsed correctly`() { | ||
| val result = ExpiryDateParser.parseToMonthAndYear("1212", true) | ||
| assertEquals("12" to "2012", result) | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.