Skip to content

Commit

Permalink
Auto select if CBC only has one allowed choice
Browse files Browse the repository at this point in the history
  • Loading branch information
porter-stripe committed Jan 24, 2025
1 parent 7b5b154 commit a4ebd98
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,20 @@ internal class DefaultCardNumberController(
override val selectedCardBrandFlow: StateFlow<CardBrand> = combineAsStateFlow(
mostRecentUserSelectedBrand,
brandChoices,
) { previous, choices ->
when (previous) {
CardBrand.Unknown -> previous
in choices -> previous ?: CardBrand.Unknown
else -> {
val firstAvailablePreferred = preferredBrands.firstOrNull { it in choices }

firstAvailablePreferred ?: CardBrand.Unknown
) { previous, allChoices ->
// Determine which of the available brands are not blocked
val allowedChoices = allChoices.filter { cardBrandFilter.isAccepted(it) }
// If there's exactly one non-blocked brand, automatically pick it, otherwise use existing logic.
if (allowedChoices.size == 1 && allChoices.size > 1) {
allowedChoices.single()
} else {
when (previous) {
CardBrand.Unknown -> previous
in allChoices -> previous ?: CardBrand.Unknown
else -> {
val firstAvailablePreferred = preferredBrands.firstOrNull { it in allChoices }
firstAvailablePreferred ?: CardBrand.Unknown
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ internal class VerticalModePaymentSheetActivityTest {
formPage.assertErrorExists("American Express is not accepted")
verticalModePage.assertPrimaryButton(isNotEnabled())
formPage.fillCardNumber("")

// Entering an accepted card brand (Visa) should be allowed
formPage.fillOutCardDetails()
verticalModePage.assertPrimaryButton(isEnabled())
Expand Down

0 comments on commit a4ebd98

Please sign in to comment.