Skip to content

Commit

Permalink
Remove update PM screen feature flag in CustomerSheet (#9778)
Browse files Browse the repository at this point in the history
* Remove update PM screen feature flag in CustomerSheet

* Fix tests

* Remove unused imports

* Fix another test
  • Loading branch information
amk-stripe authored Dec 13, 2024
1 parent e1b1316 commit cfd44e6
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.stripe.android.core.networking.ApiRequest
import com.stripe.android.core.strings.ResolvableString
import com.stripe.android.core.strings.orEmpty
import com.stripe.android.core.strings.resolvableString
import com.stripe.android.core.utils.FeatureFlags
import com.stripe.android.core.utils.requireApplication
import com.stripe.android.customersheet.analytics.CustomerSheetEventReporter
import com.stripe.android.customersheet.data.CustomerSheetDataResult
Expand Down Expand Up @@ -63,8 +62,6 @@ import com.stripe.android.paymentsheet.parseAppearance
import com.stripe.android.paymentsheet.paymentdatacollection.ach.USBankAccountFormArguments
import com.stripe.android.paymentsheet.state.PaymentElementLoader
import com.stripe.android.paymentsheet.ui.DefaultUpdatePaymentMethodInteractor
import com.stripe.android.paymentsheet.ui.EditPaymentMethodViewInteractor
import com.stripe.android.paymentsheet.ui.ModifiableEditPaymentMethodViewInteractor
import com.stripe.android.paymentsheet.ui.PaymentMethodRemovalDelayMillis
import com.stripe.android.paymentsheet.ui.PrimaryButton
import com.stripe.android.paymentsheet.ui.transformToPaymentMethodCreateParams
Expand Down Expand Up @@ -104,7 +101,6 @@ internal class CustomerSheetViewModel(
@Named(IS_LIVE_MODE) private val isLiveModeProvider: () -> Boolean,
confirmationHandlerFactory: ConfirmationHandler.Factory,
private val customerSheetLoader: CustomerSheetLoader,
private val editInteractorFactory: ModifiableEditPaymentMethodViewInteractor.Factory,
private val errorReporter: ErrorReporter,
) : ViewModel() {

Expand All @@ -121,7 +117,6 @@ internal class CustomerSheetViewModel(
@Named(IS_LIVE_MODE) isLiveModeProvider: () -> Boolean,
confirmationHandlerFactory: ConfirmationHandler.Factory,
customerSheetLoader: CustomerSheetLoader,
editInteractorFactory: ModifiableEditPaymentMethodViewInteractor.Factory,
errorReporter: ErrorReporter,
) : this(
application = application,
Expand All @@ -139,7 +134,6 @@ internal class CustomerSheetViewModel(
isLiveModeProvider = isLiveModeProvider,
confirmationHandlerFactory = confirmationHandlerFactory,
customerSheetLoader = customerSheetLoader,
editInteractorFactory = editInteractorFactory,
errorReporter = errorReporter,
)

Expand Down Expand Up @@ -563,68 +557,35 @@ internal class CustomerSheetViewModel(
}
}

@Suppress("LongMethod") // Suppressing because this is only due to a temporary feature flag.
private fun onModifyItem(paymentMethod: DisplayableSavedPaymentMethod) {
val customerState = customerState.value

if (FeatureFlags.useNewUpdateCardScreen.isEnabled) {
transition(
to = CustomerSheetViewState.UpdatePaymentMethod(
updatePaymentMethodInteractor = DefaultUpdatePaymentMethodInteractor(
isLiveMode = isLiveModeProvider(),
canRemove = customerState.canRemove,
displayableSavedPaymentMethod = paymentMethod,
cardBrandFilter = PaymentSheetCardBrandFilter(customerState.configuration.cardBrandAcceptance),
removeExecutor = ::removeExecutor,
onBrandChoiceOptionsShown = {
eventReporter.onShowPaymentOptionBrands(
source = CustomerSheetEventReporter.CardBrandChoiceEventSource.Edit,
selectedBrand = it
)
},
onBrandChoiceOptionsDismissed = {
eventReporter.onHidePaymentOptionBrands(
source = CustomerSheetEventReporter.CardBrandChoiceEventSource.Edit,
selectedBrand = it
)
},
updateExecutor = ::updateExecutor,
),
isLiveMode = isLiveModeProvider(),
)
)
} else {
transition(
to = CustomerSheetViewState.EditPaymentMethod(
editPaymentMethodInteractor = editInteractorFactory.create(
initialPaymentMethod = paymentMethod.paymentMethod,
eventHandler = { event ->
when (event) {
is EditPaymentMethodViewInteractor.Event.ShowBrands -> {
eventReporter.onShowPaymentOptionBrands(
source = CustomerSheetEventReporter.CardBrandChoiceEventSource.Edit,
selectedBrand = event.brand
)
}
is EditPaymentMethodViewInteractor.Event.HideBrands -> {
eventReporter.onHidePaymentOptionBrands(
source = CustomerSheetEventReporter.CardBrandChoiceEventSource.Edit,
selectedBrand = event.brand
)
}
}
},
displayName = providePaymentMethodName(paymentMethod.paymentMethod.type?.code),
removeExecutor = ::removeExecutor,
updateExecutor = ::updateExecutor,
canRemove = customerState.canRemove,
isLiveMode = requireNotNull(customerState.metadata).stripeIntent.isLiveMode,
cardBrandFilter = PaymentSheetCardBrandFilter(customerState.configuration.cardBrandAcceptance)
),
transition(
to = CustomerSheetViewState.UpdatePaymentMethod(
updatePaymentMethodInteractor = DefaultUpdatePaymentMethodInteractor(
isLiveMode = isLiveModeProvider(),
)
canRemove = customerState.canRemove,
displayableSavedPaymentMethod = paymentMethod,
cardBrandFilter = PaymentSheetCardBrandFilter(customerState.configuration.cardBrandAcceptance),
removeExecutor = ::removeExecutor,
onBrandChoiceOptionsShown = {
eventReporter.onShowPaymentOptionBrands(
source = CustomerSheetEventReporter.CardBrandChoiceEventSource.Edit,
selectedBrand = it
)
},
onBrandChoiceOptionsDismissed = {
eventReporter.onHidePaymentOptionBrands(
source = CustomerSheetEventReporter.CardBrandChoiceEventSource.Edit,
selectedBrand = it
)
},
updateExecutor = ::updateExecutor,
workContext = workContext,
),
isLiveMode = isLiveModeProvider(),
)
}
)
}

private suspend fun removeExecutor(paymentMethod: PaymentMethod): Throwable? {
Expand Down Expand Up @@ -1222,7 +1183,6 @@ internal class CustomerSheetViewModel(
eventReporter.onScreenPresented(CustomerSheetEventReporter.Screen.AddPaymentMethod)
is CustomerSheetViewState.SelectPaymentMethod ->
eventReporter.onScreenPresented(CustomerSheetEventReporter.Screen.SelectPaymentMethod)
is CustomerSheetViewState.EditPaymentMethod,
is CustomerSheetViewState.UpdatePaymentMethod ->
eventReporter.onScreenPresented(CustomerSheetEventReporter.Screen.EditPaymentMethod)
else -> { }
Expand Down Expand Up @@ -1261,7 +1221,6 @@ internal class CustomerSheetViewModel(
get() = when (this) {
is CustomerSheetViewState.AddPaymentMethod -> CustomerSheetEventReporter.Screen.AddPaymentMethod
is CustomerSheetViewState.SelectPaymentMethod -> CustomerSheetEventReporter.Screen.SelectPaymentMethod
is CustomerSheetViewState.EditPaymentMethod,
is CustomerSheetViewState.UpdatePaymentMethod -> CustomerSheetEventReporter.Screen.EditPaymentMethod
else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.stripe.android.paymentsheet.forms.FormFieldValues
import com.stripe.android.paymentsheet.model.PaymentSelection
import com.stripe.android.paymentsheet.paymentdatacollection.FormArguments
import com.stripe.android.paymentsheet.paymentdatacollection.ach.USBankAccountFormArguments
import com.stripe.android.paymentsheet.ui.ModifiableEditPaymentMethodViewInteractor
import com.stripe.android.paymentsheet.ui.PaymentSheetTopBarState
import com.stripe.android.paymentsheet.ui.PaymentSheetTopBarStateFactory
import com.stripe.android.paymentsheet.ui.PrimaryButton
Expand All @@ -29,7 +28,6 @@ internal sealed class CustomerSheetViewState(
fun shouldDisplayDismissConfirmationModal(): Boolean {
return when (this) {
is Loading,
is EditPaymentMethod,
is UpdatePaymentMethod,
is SelectPaymentMethod -> {
false
Expand Down Expand Up @@ -128,23 +126,6 @@ internal sealed class CustomerSheetViewState(
}
}

data class EditPaymentMethod(
val editPaymentMethodInteractor: ModifiableEditPaymentMethodViewInteractor,
override val isLiveMode: Boolean,
) : CustomerSheetViewState(
isLiveMode = isLiveMode,
isProcessing = false,
canNavigateBack = true,
) {
override fun topBarState(onEditIconPressed: () -> Unit): PaymentSheetTopBarState {
return PaymentSheetTopBarStateFactory.create(
hasBackStack = canNavigateBack,
isLiveMode = isLiveMode,
editable = PaymentSheetTopBarState.Editable.Never,
)
}
}

data class UpdatePaymentMethod(
val updatePaymentMethodInteractor: UpdatePaymentMethodInteractor,
override val isLiveMode: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.stripe.android.model.CardBrand
import com.stripe.android.model.PaymentMethodCode
import com.stripe.android.paymentsheet.PaymentOptionsStateFactory
import com.stripe.android.paymentsheet.R
import com.stripe.android.paymentsheet.ui.EditPaymentMethod
import com.stripe.android.paymentsheet.ui.ErrorMessage
import com.stripe.android.paymentsheet.ui.Mandate
import com.stripe.android.paymentsheet.ui.PaymentElement
Expand Down Expand Up @@ -101,12 +100,6 @@ internal fun CustomerSheetScreen(
)
PaymentSheetContentPadding()
}
is CustomerSheetViewState.EditPaymentMethod -> {
EditPaymentMethod(
viewState = viewState,
)
PaymentSheetContentPadding()
}
is CustomerSheetViewState.UpdatePaymentMethod -> {
UpdatePaymentMethod(
viewState = viewState,
Expand Down Expand Up @@ -311,28 +304,6 @@ internal fun AddPaymentMethod(
}
}

@Composable
private fun EditPaymentMethod(
viewState: CustomerSheetViewState.EditPaymentMethod,
modifier: Modifier = Modifier,
) {
val horizontalPadding = dimensionResource(R.dimen.stripe_paymentsheet_outer_spacing_horizontal)

Column(modifier) {
H4Text(
text = stringResource(PaymentsCoreR.string.stripe_title_update_card),
modifier = Modifier
.padding(bottom = 20.dp)
.padding(horizontal = horizontalPadding)
)

EditPaymentMethod(
interactor = viewState.editPaymentMethodInteractor,
modifier = modifier,
)
}
}

@Composable
private fun UpdatePaymentMethod(
viewState: CustomerSheetViewState.UpdatePaymentMethod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ import com.stripe.android.paymentsheet.forms.FormFieldValues
import com.stripe.android.paymentsheet.model.PaymentSelection
import com.stripe.android.paymentsheet.paymentdatacollection.FormArguments
import com.stripe.android.paymentsheet.paymentdatacollection.ach.USBankAccountFormArguments
import com.stripe.android.paymentsheet.ui.DefaultEditPaymentMethodViewInteractor
import com.stripe.android.paymentsheet.ui.DefaultUpdatePaymentMethodInteractor
import com.stripe.android.paymentsheet.utils.ViewModelStoreOwnerContext
import com.stripe.android.screenshottesting.FontSize
import com.stripe.android.screenshottesting.PaparazziRule
import com.stripe.android.screenshottesting.SystemAppearance
import com.stripe.android.testing.CoroutineTestRule
import com.stripe.android.testing.FakeErrorReporter
import com.stripe.android.testing.PaymentMethodFactory
import com.stripe.android.testing.SetupIntentFactory
import com.stripe.android.ui.core.cbc.CardBrandChoiceEligibility
import com.stripe.android.utils.NullCardAccountRangeRepositoryFactory
Expand Down Expand Up @@ -315,92 +313,40 @@ internal class CustomerSheetScreenshotTest {
}

@Test
fun testEditScreen() {
val paymentMethod = PaymentMethodFactory.card().copy(
card = PaymentMethod.Card(
last4 = "1001",
networks = PaymentMethod.Card.Networks(
available = setOf(CardBrand.CartesBancaires.code, CardBrand.Visa.code),
),
)
)

val editPaymentMethod = CustomerSheetViewState.EditPaymentMethod(
editPaymentMethodInteractor = DefaultEditPaymentMethodViewInteractor(
initialPaymentMethod = paymentMethod,
displayName = "Card".resolvableString,
removeExecutor = { null },
updateExecutor = { pm, _ -> Result.success(pm) },
eventHandler = {},
canRemove = true,
isLiveMode = true,
cardBrandFilter = DefaultCardBrandFilter
),
isLiveMode = true,
)

fun testUpdatePaymentMethodScreen() {
paparazzi.snapshot {
CustomerSheetScreen(
viewState = editPaymentMethod,
viewState = createUpdatePaymentMethodViewState(canRemove = true),
paymentMethodNameProvider = { it!!.resolvableString },
)
}
}

@Test
fun testEditScreenWithoutRemove() {
val paymentMethod = PaymentMethodFactory.card().copy(
card = PaymentMethod.Card(
last4 = "1001",
networks = PaymentMethod.Card.Networks(
available = setOf(CardBrand.CartesBancaires.code, CardBrand.Visa.code),
),
)
)

val editPaymentMethod = CustomerSheetViewState.EditPaymentMethod(
editPaymentMethodInteractor = DefaultEditPaymentMethodViewInteractor(
initialPaymentMethod = paymentMethod,
displayName = "Card".resolvableString,
removeExecutor = { null },
updateExecutor = { pm, _ -> Result.success(pm) },
eventHandler = {},
canRemove = false,
isLiveMode = true,
cardBrandFilter = DefaultCardBrandFilter
),
isLiveMode = true,
)

fun testUpdatePaymentMethodScreen_withoutRemove() {
paparazzi.snapshot {
CustomerSheetScreen(
viewState = editPaymentMethod,
viewState = createUpdatePaymentMethodViewState(canRemove = false),
paymentMethodNameProvider = { it!!.resolvableString },
)
}
}

@Test
fun testUpdatePaymentMethodScreen() {
val updatePaymentMethod = CustomerSheetViewState.UpdatePaymentMethod(
private fun createUpdatePaymentMethodViewState(
canRemove: Boolean,
): CustomerSheetViewState {
return CustomerSheetViewState.UpdatePaymentMethod(
updatePaymentMethodInteractor = DefaultUpdatePaymentMethodInteractor(
displayableSavedPaymentMethod = PaymentMethodFixtures.displayableCard(),
removeExecutor = { null },
updateExecutor = { paymentMethod, _ -> Result.success(paymentMethod) },
canRemove = true,
canRemove = canRemove,
isLiveMode = true,
cardBrandFilter = DefaultCardBrandFilter,
onBrandChoiceOptionsDismissed = {},
onBrandChoiceOptionsShown = {},
),
isLiveMode = true,
)

paparazzi.snapshot {
CustomerSheetScreen(
viewState = updatePaymentMethod,
paymentMethodNameProvider = { it!!.resolvableString },
)
}
}
}
Loading

0 comments on commit cfd44e6

Please sign in to comment.