Skip to content

Commit

Permalink
Hide GooglePay in CustomerSheet when set as default feature is enabled (
Browse files Browse the repository at this point in the history
  • Loading branch information
amk-stripe authored Feb 12, 2025
1 parent 3e8729c commit 54f73e0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1220,16 +1220,21 @@ internal class CustomerSheetViewModel(
isModifiable(method, cbcEligibility)
}

val canShowSavedPaymentMethods = paymentMethods.isNotEmpty() || metadata?.isGooglePayReady == true
val canShowSavedPaymentMethods = paymentMethods.isNotEmpty() || shouldShowGooglePay(metadata)
}

private data class SelectionConfirmationState(
val isConfirming: Boolean,
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal class CustomerSheetScreenshotTest {
isLiveMode = false,
isProcessing = false,
isEditing = false,
isGooglePayEnabled = false,
showGooglePay = false,
primaryButtonVisible = false,
canEdit = true,
canRemovePaymentMethods = true,
Expand Down Expand Up @@ -200,7 +200,7 @@ internal class CustomerSheetScreenshotTest {
savedPaymentMethods.first()
),
isEditing = true,
isGooglePayEnabled = true,
showGooglePay = true,
errorMessage = "This is an error message.",
),
paymentMethodNameProvider = {
Expand All @@ -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 },
Expand All @@ -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
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ class CustomerSheetViewModelTest {
)

viewModel.viewState.test {
assertThat(awaitViewState<SelectPaymentMethod>().isGooglePayEnabled).isFalse()
assertThat(awaitViewState<SelectPaymentMethod>().showGooglePay).isFalse()
}
}

Expand All @@ -1286,7 +1286,47 @@ class CustomerSheetViewModelTest {
)

viewModel.viewState.test {
assertThat(awaitViewState<SelectPaymentMethod>().isGooglePayEnabled).isTrue()
assertThat(awaitViewState<SelectPaymentMethod>().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<SelectPaymentMethod>().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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomerSheetState.Full> {
Expand All @@ -48,6 +49,7 @@ internal class FakeCustomerSheetLoader(
financialConnectionsAvailable = financialConnectionsAvailable,
paymentMethodOrder = configuration.paymentMethodOrder,
isGooglePayReady = isGooglePayAvailable,
isPaymentMethodSetAsDefaultEnabled = isPaymentMethodSyncDefaultEnabled,
),
supportedPaymentMethods = supportedPaymentMethods,
customerPaymentMethods = customerPaymentMethods,
Expand Down

0 comments on commit 54f73e0

Please sign in to comment.