Skip to content

Commit

Permalink
Add EmbeddedFormHelperFactory (#9990)
Browse files Browse the repository at this point in the history
* Add EmbeddedFormHelperFactory

* Refactor EmbeddedFormHelperFactory to be injectable

* Fix tests
  • Loading branch information
tjclawson-stripe authored Jan 27, 2025
1 parent 51bfffb commit f1673e9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ package com.stripe.android.paymentelement.embedded

import android.os.Parcelable
import androidx.lifecycle.SavedStateHandle
import com.stripe.android.cards.CardAccountRangeRepository
import com.stripe.android.core.injection.IOContext
import com.stripe.android.core.strings.ResolvableString
import com.stripe.android.link.LinkConfigurationCoordinator
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.paymentelement.ExperimentalEmbeddedPaymentElementApi
import com.stripe.android.paymentsheet.CustomerStateHolder
import com.stripe.android.paymentsheet.DefaultFormHelper
import com.stripe.android.paymentsheet.FormHelper
import com.stripe.android.paymentsheet.LinkInlineHandler
import com.stripe.android.paymentsheet.NewOrExternalPaymentSelection
import com.stripe.android.paymentsheet.PaymentSheet.Appearance.Embedded
import com.stripe.android.paymentsheet.SavedPaymentMethodMutator
import com.stripe.android.paymentsheet.analytics.EventReporter
Expand Down Expand Up @@ -63,15 +57,14 @@ internal interface DefaultEmbeddedContentHelperFactory : EmbeddedContentHelperFa
@OptIn(ExperimentalEmbeddedPaymentElementApi::class)
internal class DefaultEmbeddedContentHelper @AssistedInject constructor(
@Assisted private val coroutineScope: CoroutineScope,
private val cardAccountRangeRepositoryFactory: CardAccountRangeRepository.Factory,
private val savedStateHandle: SavedStateHandle,
private val eventReporter: EventReporter,
private val linkConfigurationCoordinator: LinkConfigurationCoordinator,
@IOContext private val workContext: CoroutineContext,
private val customerRepository: CustomerRepository,
private val selectionHolder: EmbeddedSelectionHolder,
private val embeddedWalletsHelper: EmbeddedWalletsHelper,
private val customerStateHolder: CustomerStateHolder,
private val embeddedFormHelperFactory: EmbeddedFormHelperFactory
) : EmbeddedContentHelper {

private val mandate: StateFlow<ResolvableString?> = savedStateHandle.getStateFlow(
Expand Down Expand Up @@ -145,9 +138,10 @@ internal class DefaultEmbeddedContentHelper @AssistedInject constructor(
val paymentMethodIncentiveInteractor = PaymentMethodIncentiveInteractor(
incentive = paymentMethodMetadata.paymentMethodIncentive,
)
val formHelper = createFormHelper(
val formHelper = embeddedFormHelperFactory.create(
coroutineScope = coroutineScope,
paymentMethodMetadata = paymentMethodMetadata,
selectionUpdater = ::setSelection
)
val savedPaymentMethodMutator = createSavedPaymentMethodMutator(
coroutineScope = coroutineScope,
Expand Down Expand Up @@ -233,33 +227,6 @@ internal class DefaultEmbeddedContentHelper @AssistedInject constructor(
)
}

private fun createFormHelper(
coroutineScope: CoroutineScope,
paymentMethodMetadata: PaymentMethodMetadata,
): FormHelper {
return DefaultFormHelper(
coroutineScope = coroutineScope,
linkInlineHandler = LinkInlineHandler.create(),
cardAccountRangeRepositoryFactory = cardAccountRangeRepositoryFactory,
paymentMethodMetadata = paymentMethodMetadata,
newPaymentSelectionProvider = {
when (val currentSelection = selectionHolder.selection.value) {
is PaymentSelection.ExternalPaymentMethod -> {
NewOrExternalPaymentSelection.External(currentSelection)
}
is PaymentSelection.New -> {
NewOrExternalPaymentSelection.New(currentSelection)
}
else -> null
}
},
selectionUpdater = {
setSelection(it)
},
linkConfigurationCoordinator = linkConfigurationCoordinator,
)
}

private fun setSelection(paymentSelection: PaymentSelection?) {
if (paymentSelection != selectionHolder.selection.value) {
savedStateHandle[MANDATE_KEY_EMBEDDED_CONTENT] = null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.stripe.android.paymentelement.embedded

import com.stripe.android.cards.CardAccountRangeRepository
import com.stripe.android.link.LinkConfigurationCoordinator
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata
import com.stripe.android.paymentsheet.DefaultFormHelper
import com.stripe.android.paymentsheet.FormHelper
import com.stripe.android.paymentsheet.LinkInlineHandler
import com.stripe.android.paymentsheet.NewOrExternalPaymentSelection
import com.stripe.android.paymentsheet.model.PaymentSelection
import kotlinx.coroutines.CoroutineScope
import javax.inject.Inject

internal class EmbeddedFormHelperFactory @Inject constructor(
private val linkConfigurationCoordinator: LinkConfigurationCoordinator,
private val embeddedSelectionHolder: EmbeddedSelectionHolder,
private val cardAccountRangeRepositoryFactory: CardAccountRangeRepository.Factory,
) {
fun create(
coroutineScope: CoroutineScope,
paymentMethodMetadata: PaymentMethodMetadata,
selectionUpdater: (PaymentSelection?) -> Unit
): FormHelper {
return DefaultFormHelper(
coroutineScope = coroutineScope,
linkInlineHandler = LinkInlineHandler.create(),
cardAccountRangeRepositoryFactory = cardAccountRangeRepositoryFactory,
paymentMethodMetadata = paymentMethodMetadata,
newPaymentSelectionProvider = {
when (val currentSelection = embeddedSelectionHolder.selection.value) {
is PaymentSelection.ExternalPaymentMethod -> {
NewOrExternalPaymentSelection.External(currentSelection)
}
is PaymentSelection.New -> {
NewOrExternalPaymentSelection.New(currentSelection)
}
else -> null
}
},
selectionUpdater = selectionUpdater,
linkConfigurationCoordinator = linkConfigurationCoordinator,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,21 @@ internal class DefaultEmbeddedContentHelperTest {
val savedStateHandle = SavedStateHandle()
savedStateHandle.setup()
val selectionHolder = EmbeddedSelectionHolder(savedStateHandle)
val embeddedFormHelperFactory = EmbeddedFormHelperFactory(
linkConfigurationCoordinator = FakeLinkConfigurationCoordinator(),
cardAccountRangeRepositoryFactory = NullCardAccountRangeRepositoryFactory,
embeddedSelectionHolder = selectionHolder
)
val embeddedContentHelper = DefaultEmbeddedContentHelper(
coroutineScope = CoroutineScope(Dispatchers.Unconfined),
cardAccountRangeRepositoryFactory = NullCardAccountRangeRepositoryFactory,
savedStateHandle = savedStateHandle,
eventReporter = mock(),
linkConfigurationCoordinator = FakeLinkConfigurationCoordinator(),
workContext = Dispatchers.Unconfined,
customerRepository = FakeCustomerRepository(),
selectionHolder = selectionHolder,
embeddedWalletsHelper = { stateFlowOf(null) },
customerStateHolder = CustomerStateHolder(savedStateHandle, selectionHolder.selection),
embeddedFormHelperFactory = embeddedFormHelperFactory
)
Scenario(
embeddedContentHelper = embeddedContentHelper,
Expand Down

0 comments on commit f1673e9

Please sign in to comment.