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

In update card screen, only update card brand when the card brand has changed #10239

Merged
merged 4 commits into from
Feb 20, 2025
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 @@ -166,23 +166,44 @@ internal class DefaultUpdatePaymentMethodInteractor(

private fun savePaymentMethod() {
coroutineScope.launch {
val newCardBrand = cardBrandChoice.value.brand

error.emit(getInitialError())
status.emit(UpdatePaymentMethodInteractor.Status.Updating)

val updateCardBrandResult = updateCardBrandExecutor(
displayableSavedPaymentMethod.paymentMethod,
newCardBrand
val updateCardBrandResult = maybeUpdateCardBrand()

val errorMessage = getErrorMessageForUpdates(
updateCardBrandResult = updateCardBrandResult,
)

updateCardBrandResult.onSuccess {
if (errorMessage != null) {
error.emit(errorMessage)
}

status.emit(UpdatePaymentMethodInteractor.Status.Idle)
}
}

private suspend fun maybeUpdateCardBrand(): Result<PaymentMethod>? {
val newCardBrand = cardBrandChoice.value.brand
return if (cardBrandHasBeenChanged.value) {
updateCardBrandExecutor(
displayableSavedPaymentMethod.paymentMethod,
newCardBrand
).onSuccess {
savedCardBrand.emit(CardBrandChoice(brand = newCardBrand, enabled = true))
cardBrandHasBeenChanged.emit(false)
}.onFailure {
error.emit(it.stripeErrorMessage())
}
status.emit(UpdatePaymentMethodInteractor.Status.Idle)
} else {
null
}
}

private fun getErrorMessageForUpdates(
updateCardBrandResult: Result<PaymentMethod>?,
): ResolvableString? {
return when {
updateCardBrandResult?.isFailure == true -> updateCardBrandResult.exceptionOrNull()?.stripeErrorMessage()
else -> null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,28 @@ class DefaultUpdatePaymentMethodInteractorTest {
}
}

@Test
fun updatingCardBrand_cardBrandHasNotChanged_doesNotAttemptUpdate() {
val initialPaymentMethod = PaymentMethodFixtures.CARD_WITH_NETWORKS_PAYMENT_METHOD
var cardBrandUpdateCalled = false

@Suppress("UnusedParameter")
fun updateCardBrandExecutor(paymentMethod: PaymentMethod, brand: CardBrand): Result<PaymentMethod> {
cardBrandUpdateCalled = true

return Result.success(paymentMethod)
}

runScenario(
displayableSavedPaymentMethod = initialPaymentMethod.toDisplayableSavedPaymentMethod(),
onUpdateCardBrand = ::updateCardBrandExecutor,
) {
interactor.handleViewAction(UpdatePaymentMethodInteractor.ViewAction.SaveButtonPressed)

assertThat(cardBrandUpdateCalled).isFalse()
}
}

@Test
fun saveButtonClick_failure_displaysError() {
val updateException = IllegalStateException("Not allowed.")
Expand Down
Loading