Skip to content

Commit

Permalink
Fix issue where FlowController shows an empty saved payment method …
Browse files Browse the repository at this point in the history
…screen when `Google Pay` is enabled.
  • Loading branch information
samer-stripe committed Jun 24, 2024
1 parent 36fd0ac commit 54273f6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ internal abstract class BaseSheetViewModel(
* The list of saved payment methods for the current customer.
* Value is null until it's loaded, and non-null (could be empty) after that.
*/
internal val paymentMethods: StateFlow<List<PaymentMethod>?> = savedStateHandle
internal val paymentMethods: StateFlow<List<PaymentMethod>> = savedStateHandle
.getStateFlow<CustomerState?>(SAVED_CUSTOMER, null)
.mapAsStateFlow { state ->
state?.paymentMethods
state?.paymentMethods ?: emptyList()
}

protected val backStack = MutableStateFlow<List<PaymentSheetScreen>>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.stripe.android.uicore.utils.combineAsStateFlow
import kotlinx.coroutines.flow.StateFlow

internal class PaymentOptionsStateMapper(
private val paymentMethods: StateFlow<List<PaymentMethod>?>,
private val paymentMethods: StateFlow<List<PaymentMethod>>,
private val googlePayState: StateFlow<GooglePayState>,
private val isLinkEnabled: StateFlow<Boolean?>,
private val currentSelection: StateFlow<PaymentSelection?>,
Expand Down Expand Up @@ -39,13 +39,12 @@ internal class PaymentOptionsStateMapper(

@Suppress("ReturnCount")
private fun createPaymentOptionsState(
paymentMethods: List<PaymentMethod>?,
paymentMethods: List<PaymentMethod>,
currentSelection: PaymentSelection?,
isLinkEnabled: Boolean?,
canRemovePaymentMethods: Boolean?,
googlePayState: GooglePayState,
): PaymentOptionsState? {
if (paymentMethods == null) return null
if (isLinkEnabled == null) return null

return PaymentOptionsStateFactory.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,24 @@ internal class PaymentOptionsViewModelTest {
)
}

@Test
fun `paymentMethods is not null when loading is complete`() = runTest {
val args = PAYMENT_OPTION_CONTRACT_ARGS.updateState(
isGooglePayReady = true,
).run {
copy(state = state.copy(customer = null))
}

val viewModel = createViewModel(args = args)

viewModel.currentScreen.test {
assertThat(awaitItem()).isInstanceOf(SelectSavedPaymentMethods::class.java)
}
viewModel.paymentMethods.test {
assertThat(awaitItem()).isEmpty()
}
}

private fun createLinkViewModel(): PaymentOptionsViewModel {
val linkConfigurationCoordinator = FakeLinkConfigurationCoordinator(
attachNewCardToAccountResult = Result.success(LinkTestUtils.LINK_NEW_PAYMENT_DETAILS),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PaymentOptionsStateMapperTest {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

private val paymentMethodsFlow = MutableStateFlow<List<PaymentMethod>?>(null)
private val paymentMethodsFlow = MutableStateFlow<List<PaymentMethod>>(emptyList())
private val currentSelectionFlow = MutableStateFlow<PaymentSelection?>(null)
private val googlePayStateFlow = MutableStateFlow<GooglePayState>(GooglePayState.Indeterminate)
private val isLinkEnabledFlow = MutableStateFlow<Boolean?>(null)
Expand Down

0 comments on commit 54273f6

Please sign in to comment.