Skip to content
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

Fix issue where FlowController shows an empty saved payment method screen when Google Pay is enabled. #8672

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class DefaultManageOneSavedPaymentMethodInteractor(
) : ManageOneSavedPaymentMethodInteractor {

constructor(sheetViewModel: BaseSheetViewModel) : this(
paymentMethod = sheetViewModel.paymentMethods.value!!.first(),
paymentMethod = sheetViewModel.paymentMethods.value.first(),
paymentMethodMetadata = sheetViewModel.paymentMethodMetadata.value!!,
providePaymentMethodName = sheetViewModel::providePaymentMethodName,
onDeletePaymentMethod = { sheetViewModel.removePaymentMethod(it) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.stripe.android.paymentsheet.viewmodels.BaseSheetViewModel

internal object VerticalModeInitialScreenFactory {
fun create(viewModel: BaseSheetViewModel): PaymentSheetScreen {
val savedPaymentMethods = viewModel.paymentMethods.value ?: emptyList()
val savedPaymentMethods = viewModel.paymentMethods.value
if (viewModel.supportedPaymentMethods.size == 1 && savedPaymentMethods.isEmpty()) {
return PaymentSheetScreen.Form(
interactor = DefaultVerticalModeFormInteractor(
Expand Down
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
samer-stripe marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -1454,15 +1454,6 @@ internal class PaymentSheetViewModelTest {
}
}

@Test
fun `paymentMethods is null if payment sheet state is not loaded`() = runTest {
val viewModel = createViewModel(delay = Duration.INFINITE)

viewModel.paymentMethods.test {
assertThat(awaitItem()).isNull()
}
}

@Test
fun `handleBackPressed is consumed when processing is true`() = runTest {
val viewModel = createViewModel(customer = EMPTY_CUSTOMER_STATE)
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
Loading