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

Add saved payment method mandates. #10163

Merged
merged 1 commit into from
Feb 13, 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 @@ -73,7 +73,7 @@ internal interface PaymentMethodVerticalLayoutInteractor {
}

internal class DefaultPaymentMethodVerticalLayoutInteractor(
paymentMethodMetadata: PaymentMethodMetadata,
private val paymentMethodMetadata: PaymentMethodMetadata,
processing: StateFlow<Boolean>,
temporarySelection: StateFlow<PaymentMethodCode?>,
selection: StateFlow<PaymentSelection?>,
Expand Down Expand Up @@ -227,19 +227,13 @@ internal class DefaultPaymentMethodVerticalLayoutInteractor(
} else {
null
}
val selectionCode = temporarySelectionCode ?: (mostRecentSelection as? PaymentSelection.New).code()
val mandate = if (selectionCode != null) {
(formTypeForCode(selectionCode) as? FormType.MandateOnly)?.mandate
} else {
null
}
PaymentMethodVerticalLayoutInteractor.State(
displayablePaymentMethods = displayablePaymentMethods,
isProcessing = isProcessing,
selection = temporarySelection ?: mostRecentSelection?.asVerticalSelection(),
displayedSavedPaymentMethod = displayedSavedPaymentMethod,
availableSavedPaymentMethodAction = action,
mandate = mandate,
mandate = getMandate(temporarySelectionCode, mostRecentSelection),
)
}

Expand Down Expand Up @@ -425,6 +419,16 @@ internal class DefaultPaymentMethodVerticalLayoutInteractor(
selectedPaymentMethodCode,
)
}

private fun getMandate(temporarySelectionCode: String?, selection: PaymentSelection?): ResolvableString? {
val selectionCode = temporarySelectionCode ?: (selection as? PaymentSelection.New).code()
return if (selectionCode != null) {
(formTypeForCode(selectionCode) as? FormType.MandateOnly)?.mandate
} else {
val savedSelection = selection as? PaymentSelection.Saved?
savedSelection?.mandateText(paymentMethodMetadata.merchantName, paymentMethodMetadata.hasIntentToSetup())
}
}
}

private fun PaymentSelection.asVerticalSelection(): PaymentMethodVerticalLayoutInteractor.Selection = when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Test
import com.stripe.android.paymentsheet.R as PaymentSheetR
import com.stripe.android.ui.core.R as StripeUiCoreR

@Suppress("LargeClass")
class DefaultPaymentMethodVerticalLayoutInteractorTest {
Expand Down Expand Up @@ -1126,6 +1127,33 @@ class DefaultPaymentMethodVerticalLayoutInteractorTest {
}
}

@Test
fun savedSelectionUpdatesMandate() {
val paymentMethodTypes = listOf("card", "sepa_debit")
val paymentMethodMetadata = PaymentMethodMetadataFactory.create(
stripeIntent = SetupIntentFixtures.SI_REQUIRES_PAYMENT_METHOD.copy(
paymentMethodTypes = paymentMethodTypes
)
)
runScenario(
paymentMethodMetadata = paymentMethodMetadata,
) {
interactor.state.test {
assertThat(awaitItem().mandate).isNull()
selectionSource.value = PaymentSelection.Saved(PaymentMethodFixtures.SEPA_DEBIT_PAYMENT_METHOD)
assertThat(awaitItem().mandate)
.isEqualTo(
resolvableString(
id = StripeUiCoreR.string.stripe_sepa_mandate,
paymentMethodMetadata.merchantName
)
)
selectionSource.value = PaymentSelection.Saved(PaymentMethodFixtures.CARD_PAYMENT_METHOD)
assertThat(awaitItem().mandate).isNull()
}
}
}

private val notImplemented: () -> Nothing = { throw AssertionError("Not implemented") }

private fun runScenario(
Expand Down
Loading