diff --git a/paymentsheet/src/main/java/com/stripe/android/customersheet/CustomerSheetViewModel.kt b/paymentsheet/src/main/java/com/stripe/android/customersheet/CustomerSheetViewModel.kt index 92ac34b0b87..f87fa266087 100644 --- a/paymentsheet/src/main/java/com/stripe/android/customersheet/CustomerSheetViewModel.kt +++ b/paymentsheet/src/main/java/com/stripe/android/customersheet/CustomerSheetViewModel.kt @@ -193,7 +193,7 @@ internal class CustomerSheetViewModel( isLiveMode = isLiveModeProvider(), canRemovePaymentMethods = customerState.canRemove, primaryButtonVisible = primaryButtonVisible, - isGooglePayEnabled = paymentMethodMetadata?.isGooglePayReady == true, + showGooglePay = shouldShowGooglePay(paymentMethodMetadata), isEditing = userCanEditAndIsEditing, isProcessing = selectionConfirmationState.isConfirming, errorMessage = selectionConfirmationState.error, @@ -1220,7 +1220,7 @@ internal class CustomerSheetViewModel( isModifiable(method, cbcEligibility) } - val canShowSavedPaymentMethods = paymentMethods.isNotEmpty() || metadata?.isGooglePayReady == true + val canShowSavedPaymentMethods = paymentMethods.isNotEmpty() || shouldShowGooglePay(metadata) } private data class SelectionConfirmationState( @@ -1228,8 +1228,13 @@ internal class CustomerSheetViewModel( val error: String?, ) - private companion object { + internal companion object { const val REMOVAL_TRANSITION_DELAY = 50L + + fun shouldShowGooglePay(paymentMethodMetadata: PaymentMethodMetadata?): Boolean { + return paymentMethodMetadata?.isGooglePayReady == true && + paymentMethodMetadata.customerMetadata?.isPaymentMethodSetAsDefaultEnabled != true + } } class Factory( diff --git a/paymentsheet/src/main/java/com/stripe/android/customersheet/CustomerSheetViewState.kt b/paymentsheet/src/main/java/com/stripe/android/customersheet/CustomerSheetViewState.kt index 131766a122a..329e2ab021d 100644 --- a/paymentsheet/src/main/java/com/stripe/android/customersheet/CustomerSheetViewState.kt +++ b/paymentsheet/src/main/java/com/stripe/android/customersheet/CustomerSheetViewState.kt @@ -60,7 +60,7 @@ internal sealed class CustomerSheetViewState( override val isLiveMode: Boolean, override val isProcessing: Boolean, val isEditing: Boolean, - val isGooglePayEnabled: Boolean, + val showGooglePay: Boolean, val primaryButtonVisible: Boolean, val canEdit: Boolean, val canRemovePaymentMethods: Boolean, diff --git a/paymentsheet/src/main/java/com/stripe/android/customersheet/ui/CustomerSheetScreen.kt b/paymentsheet/src/main/java/com/stripe/android/customersheet/ui/CustomerSheetScreen.kt index 496e6efe5b3..356f51d9105 100644 --- a/paymentsheet/src/main/java/com/stripe/android/customersheet/ui/CustomerSheetScreen.kt +++ b/paymentsheet/src/main/java/com/stripe/android/customersheet/ui/CustomerSheetScreen.kt @@ -137,7 +137,7 @@ internal fun SelectPaymentMethod( val paymentOptionsState = PaymentOptionsStateFactory.create( paymentMethods = viewState.savedPaymentMethods, - showGooglePay = viewState.isGooglePayEnabled, + showGooglePay = viewState.showGooglePay, showLink = false, currentSelection = viewState.paymentSelection, nameProvider = paymentMethodNameProvider, diff --git a/paymentsheet/src/test/java/com/stripe/android/customersheet/CustomerSheetScreenshotTest.kt b/paymentsheet/src/test/java/com/stripe/android/customersheet/CustomerSheetScreenshotTest.kt index e4179081156..322087561fc 100644 --- a/paymentsheet/src/test/java/com/stripe/android/customersheet/CustomerSheetScreenshotTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/customersheet/CustomerSheetScreenshotTest.kt @@ -84,7 +84,7 @@ internal class CustomerSheetScreenshotTest { isLiveMode = false, isProcessing = false, isEditing = false, - isGooglePayEnabled = false, + showGooglePay = false, primaryButtonVisible = false, canEdit = true, canRemovePaymentMethods = true, @@ -200,7 +200,7 @@ internal class CustomerSheetScreenshotTest { savedPaymentMethods.first() ), isEditing = true, - isGooglePayEnabled = true, + showGooglePay = true, errorMessage = "This is an error message.", ), paymentMethodNameProvider = { @@ -218,7 +218,7 @@ internal class CustomerSheetScreenshotTest { viewState = selectPaymentMethodViewState.copy( title = "Screenshot testing", paymentSelection = PaymentSelection.GooglePay, - isGooglePayEnabled = true, + showGooglePay = true, errorMessage = "This is an error message.", ), paymentMethodNameProvider = { it!!.resolvableString }, @@ -239,7 +239,7 @@ internal class CustomerSheetScreenshotTest { paymentSelection = PaymentSelection.Saved( PaymentMethodFixtures.US_BANK_ACCOUNT ), - isGooglePayEnabled = false, + showGooglePay = false, primaryButtonVisible = true, mandateText = "Some mandate text.".resolvableString ), diff --git a/paymentsheet/src/test/java/com/stripe/android/customersheet/CustomerSheetViewModelTest.kt b/paymentsheet/src/test/java/com/stripe/android/customersheet/CustomerSheetViewModelTest.kt index 9640f37c800..d66e91abb81 100644 --- a/paymentsheet/src/test/java/com/stripe/android/customersheet/CustomerSheetViewModelTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/customersheet/CustomerSheetViewModelTest.kt @@ -1268,7 +1268,7 @@ class CustomerSheetViewModelTest { ) viewModel.viewState.test { - assertThat(awaitViewState().isGooglePayEnabled).isFalse() + assertThat(awaitViewState().showGooglePay).isFalse() } } @@ -1286,7 +1286,47 @@ class CustomerSheetViewModelTest { ) viewModel.viewState.test { - assertThat(awaitViewState().isGooglePayEnabled).isTrue() + assertThat(awaitViewState().showGooglePay).isTrue() + } + } + + @Test + fun `When default PM feature enabled, then Google Pay should not be shown`() = runTest(testDispatcher) { + val viewModel = createViewModel( + workContext = testDispatcher, + configuration = CustomerSheet.Configuration( + merchantDisplayName = "Example", + googlePayEnabled = true, + ), + customerSheetLoader = FakeCustomerSheetLoader( + customerPaymentMethods = listOf(CARD_PAYMENT_METHOD), + isGooglePayAvailable = true, + isPaymentMethodSyncDefaultEnabled = true, + ), + ) + + viewModel.viewState.test { + assertThat(awaitViewState().showGooglePay).isFalse() + } + } + + @Test + fun `When default PM feature enabled and no saved PMs, then initial screen is add payment method`() = runTest(testDispatcher) { + val viewModel = createViewModel( + workContext = testDispatcher, + configuration = CustomerSheet.Configuration( + merchantDisplayName = "Example", + googlePayEnabled = true, + ), + customerSheetLoader = FakeCustomerSheetLoader( + customerPaymentMethods = emptyList(), + isGooglePayAvailable = true, + isPaymentMethodSyncDefaultEnabled = true, + ), + ) + + viewModel.viewState.test { + assertThat(awaitItem()).isInstanceOf(AddPaymentMethod::class.java) } } diff --git a/paymentsheet/src/test/java/com/stripe/android/customersheet/utils/FakeCustomerSheetLoader.kt b/paymentsheet/src/test/java/com/stripe/android/customersheet/utils/FakeCustomerSheetLoader.kt index b34ada77229..c051eea3f63 100644 --- a/paymentsheet/src/test/java/com/stripe/android/customersheet/utils/FakeCustomerSheetLoader.kt +++ b/paymentsheet/src/test/java/com/stripe/android/customersheet/utils/FakeCustomerSheetLoader.kt @@ -32,6 +32,7 @@ internal class FakeCustomerSheetLoader( canRemovePaymentMethods = true, canRemoveLastPaymentMethod = true, ), + private val isPaymentMethodSyncDefaultEnabled: Boolean = false, ) : CustomerSheetLoader { override suspend fun load(configuration: CustomerSheet.Configuration): Result { @@ -48,6 +49,7 @@ internal class FakeCustomerSheetLoader( financialConnectionsAvailable = financialConnectionsAvailable, paymentMethodOrder = configuration.paymentMethodOrder, isGooglePayReady = isGooglePayAvailable, + isPaymentMethodSetAsDefaultEnabled = isPaymentMethodSyncDefaultEnabled, ), supportedPaymentMethods = supportedPaymentMethods, customerPaymentMethods = customerPaymentMethods,