Skip to content

Commit

Permalink
Move confirmation injection to a separate module (#9636)
Browse files Browse the repository at this point in the history
  • Loading branch information
samer-stripe authored Nov 15, 2024
1 parent bb348ae commit 29ba1ce
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import com.stripe.android.model.PaymentMethodUpdateParams
import com.stripe.android.model.StripeIntent
import com.stripe.android.networking.StripeRepository
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler
import com.stripe.android.paymentelement.confirmation.DefaultConfirmationHandler
import com.stripe.android.payments.bankaccount.CollectBankAccountLauncher
import com.stripe.android.payments.core.analytics.ErrorReporter
import com.stripe.android.paymentsheet.R
Expand Down Expand Up @@ -99,7 +98,7 @@ internal class CustomerSheetViewModel(
private val eventReporter: CustomerSheetEventReporter,
private val workContext: CoroutineContext = Dispatchers.IO,
@Named(IS_LIVE_MODE) private val isLiveModeProvider: () -> Boolean,
confirmationHandlerFactory: DefaultConfirmationHandler.Factory,
confirmationHandlerFactory: ConfirmationHandler.Factory,
private val customerSheetLoader: CustomerSheetLoader,
private val editInteractorFactory: ModifiableEditPaymentMethodViewInteractor.Factory,
private val errorReporter: ErrorReporter,
Expand All @@ -116,7 +115,7 @@ internal class CustomerSheetViewModel(
eventReporter: CustomerSheetEventReporter,
workContext: CoroutineContext = Dispatchers.IO,
@Named(IS_LIVE_MODE) isLiveModeProvider: () -> Boolean,
confirmationHandlerFactory: DefaultConfirmationHandler.Factory,
confirmationHandlerFactory: ConfirmationHandler.Factory,
customerSheetLoader: CustomerSheetLoader,
editInteractorFactory: ModifiableEditPaymentMethodViewInteractor.Factory,
errorReporter: ErrorReporter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import com.stripe.android.customersheet.CustomerSheet
import com.stripe.android.customersheet.CustomerSheetIntegration
import com.stripe.android.customersheet.CustomerSheetViewModel
import com.stripe.android.googlepaylauncher.injection.GooglePayLauncherModule
import com.stripe.android.paymentelement.confirmation.ConfirmationModule
import com.stripe.android.payments.core.injection.StripeRepositoryModule
import dagger.BindsInstance
import dagger.Component

@CustomerSheetViewModelScope
@Component(
modules = [
ConfirmationModule::class,
CustomerSheetViewModelModule::class,
StripeRepositoryModule::class,
GooglePayLauncherModule::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.Application
import android.content.Context
import android.content.res.Resources
import androidx.core.os.LocaleListCompat
import androidx.lifecycle.SavedStateHandle
import com.stripe.android.BuildConfig
import com.stripe.android.PaymentConfiguration
import com.stripe.android.core.Logger
Expand All @@ -18,23 +17,19 @@ import com.stripe.android.core.networking.AnalyticsRequestExecutor
import com.stripe.android.core.networking.AnalyticsRequestFactory
import com.stripe.android.core.networking.NetworkTypeDetector
import com.stripe.android.core.utils.ContextUtils.packageInfo
import com.stripe.android.core.utils.UserFacingLogger
import com.stripe.android.customersheet.CustomerSheetLoader
import com.stripe.android.customersheet.DefaultCustomerSheetLoader
import com.stripe.android.customersheet.analytics.CustomerSheetEventReporter
import com.stripe.android.customersheet.analytics.DefaultCustomerSheetEventReporter
import com.stripe.android.paymentelement.confirmation.DefaultConfirmationHandler
import com.stripe.android.paymentelement.confirmation.DefaultIntentConfirmationInterceptor
import com.stripe.android.paymentelement.confirmation.IntentConfirmationInterceptor
import com.stripe.android.paymentelement.confirmation.ALLOWS_MANUAL_CONFIRMATION
import com.stripe.android.paymentelement.confirmation.STATUS_BAR_COLOR_PROVIDER
import com.stripe.android.payments.core.analytics.ErrorReporter
import com.stripe.android.payments.core.analytics.RealErrorReporter
import com.stripe.android.payments.core.injection.PRODUCT_USAGE
import com.stripe.android.payments.financialconnections.DefaultIsFinancialConnectionsAvailable
import com.stripe.android.payments.financialconnections.IsFinancialConnectionsAvailable
import com.stripe.android.payments.paymentlauncher.StripePaymentLauncherAssistedFactory
import com.stripe.android.paymentsheet.injection.ALLOWS_MANUAL_CONFIRMATION
import com.stripe.android.paymentsheet.model.PaymentSelection
import com.stripe.android.paymentsheet.paymentdatacollection.bacs.BacsMandateConfirmationLauncherFactory
import com.stripe.android.paymentsheet.paymentdatacollection.bacs.DefaultBacsMandateConfirmationLauncherFactory
import com.stripe.android.paymentsheet.repositories.ElementsSessionRepository
import com.stripe.android.paymentsheet.repositories.RealElementsSessionRepository
import com.stripe.android.paymentsheet.ui.DefaultEditPaymentMethodViewInteractor
Expand All @@ -49,12 +44,6 @@ import kotlin.coroutines.CoroutineContext

@Module
internal interface CustomerSheetViewModelModule {

@Binds
fun bindsIntentConfirmationInterceptor(
impl: DefaultIntentConfirmationInterceptor,
): IntentConfirmationInterceptor

@Binds
fun bindsCustomerSheetEventReporter(
impl: DefaultCustomerSheetEventReporter
Expand Down Expand Up @@ -113,6 +102,13 @@ internal interface CustomerSheetViewModelModule {
paymentConfiguration: Provider<PaymentConfiguration>
): () -> Boolean = { paymentConfiguration.get().publishableKey.startsWith("pk_live") }

@Provides
@Named(STATUS_BAR_COLOR_PROVIDER)
fun providesStatusBarColor(statusBarColor: Int?): () -> Int? = { statusBarColor }

@Provides
fun providesUserFacingLogger(): UserFacingLogger? = null

@Provides
internal fun provideAnalyticsRequestFactory(
application: Application,
Expand All @@ -134,33 +130,6 @@ internal interface CustomerSheetViewModelModule {
analyticsRequestExecutor = analyticsRequestExecutor,
)

@Provides
fun providesBacsMandateConfirmationLauncherFactory(): BacsMandateConfirmationLauncherFactory =
DefaultBacsMandateConfirmationLauncherFactory

@Provides
fun providesIntentConfirmationHandlerFactory(
savedStateHandle: SavedStateHandle,
paymentConfigurationProvider: Provider<PaymentConfiguration>,
bacsMandateConfirmationLauncherFactory: BacsMandateConfirmationLauncherFactory,
stripePaymentLauncherAssistedFactory: StripePaymentLauncherAssistedFactory,
statusBarColor: Int?,
intentConfirmationInterceptor: IntentConfirmationInterceptor,
errorReporter: ErrorReporter
): DefaultConfirmationHandler.Factory {
return DefaultConfirmationHandler.Factory(
intentConfirmationInterceptor = intentConfirmationInterceptor,
paymentConfigurationProvider = paymentConfigurationProvider,
stripePaymentLauncherAssistedFactory = stripePaymentLauncherAssistedFactory,
googlePayPaymentMethodLauncherFactory = null,
bacsMandateConfirmationLauncherFactory = bacsMandateConfirmationLauncherFactory,
statusBarColor = { statusBarColor },
savedStateHandle = savedStateHandle,
errorReporter = errorReporter,
logger = null
)
}

@Provides
fun resources(application: Application): Resources {
return application.resources
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.stripe.android.paymentelement.confirmation

/**
* Whether the integration allows for manual confirmation
*/
internal const val ALLOWS_MANUAL_CONFIRMATION = "ALLOWS_MANUAL_CONFIRMATION"

/**
* Color of the status bar on the user's device
*/
internal const val STATUS_BAR_COLOR_PROVIDER = "STATUS_BAR_COLOR_PROVIDER"
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.stripe.android.model.StripeIntent
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.addresselement.AddressDetails
import com.stripe.android.paymentsheet.state.PaymentElementLoader
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.StateFlow
import kotlinx.parcelize.Parcelize
import com.stripe.android.model.PaymentMethod as PaymentMethodModel
Expand All @@ -20,6 +21,12 @@ import com.stripe.android.model.PaymentMethod as PaymentMethodModel
* intended to run only one confirmation process at a time.
*/
internal interface ConfirmationHandler {
/**
* Indicates if this handler has been reloaded from process death. This occurs if the handler was confirming
* an intent before did not complete the process before process death.
*/
val hasReloadedFromProcessDeath: Boolean

/**
* An observable indicating the current confirmation state of the handler.
*/
Expand Down Expand Up @@ -47,6 +54,14 @@ internal interface ConfirmationHandler {
*/
suspend fun awaitIntentResult(): Result?

/**
* A factory for creating a [ConfirmationHandler] instance using a provided [CoroutineScope]. This scope is
* used to launch confirmation tasks.
*/
interface Factory {
fun create(scope: CoroutineScope): ConfirmationHandler
}

/**
* Defines the set of arguments requires for beginning the confirmation process
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.stripe.android.paymentelement.confirmation

import com.stripe.android.paymentsheet.paymentdatacollection.bacs.BacsMandateConfirmationLauncherFactory
import com.stripe.android.paymentsheet.paymentdatacollection.bacs.DefaultBacsMandateConfirmationLauncherFactory
import dagger.Binds
import dagger.Module
import dagger.Provides

@Module
internal interface ConfirmationModule {
@Binds
fun bindsConfirmationHandlerFactory(
defaultConfirmationHandlerFactory: DefaultConfirmationHandler.Factory
): ConfirmationHandler.Factory

@Binds
fun bindsIntentConfirmationInterceptor(
defaultConfirmationHandlerFactory: DefaultIntentConfirmationInterceptor
): IntentConfirmationInterceptor

companion object {
@Provides
fun providesBacsMandateConfirmationLauncherFactory(): BacsMandateConfirmationLauncherFactory =
DefaultBacsMandateConfirmationLauncherFactory
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Provider
import kotlin.time.Duration.Companion.seconds

Expand Down Expand Up @@ -89,7 +91,7 @@ internal class DefaultConfirmationHandler(
* Indicates if this handler has been reloaded from process death. This occurs if the handler was confirming
* an intent before did not complete the process before process death.
*/
val hasReloadedFromProcessDeath = hasReloadedWhileAwaitingPreConfirm || hasReloadedWhileAwaitingConfirm
override val hasReloadedFromProcessDeath = hasReloadedWhileAwaitingPreConfirm || hasReloadedWhileAwaitingConfirm

private val _state = MutableStateFlow(
if (hasReloadedWhileAwaitingPreConfirm) {
Expand Down Expand Up @@ -626,18 +628,18 @@ internal class DefaultConfirmationHandler(
}
}

class Factory(
class Factory @Inject constructor(
private val intentConfirmationInterceptor: IntentConfirmationInterceptor,
private val paymentConfigurationProvider: Provider<PaymentConfiguration>,
private val bacsMandateConfirmationLauncherFactory: BacsMandateConfirmationLauncherFactory,
private val stripePaymentLauncherAssistedFactory: StripePaymentLauncherAssistedFactory,
private val googlePayPaymentMethodLauncherFactory: GooglePayPaymentMethodLauncherFactory?,
private val savedStateHandle: SavedStateHandle,
private val statusBarColor: () -> Int?,
@Named(STATUS_BAR_COLOR_PROVIDER) private val statusBarColor: () -> Int?,
private val errorReporter: ErrorReporter,
private val logger: UserFacingLogger?
) {
fun create(scope: CoroutineScope): DefaultConfirmationHandler {
) : ConfirmationHandler.Factory {
override fun create(scope: CoroutineScope): ConfirmationHandler {
return DefaultConfirmationHandler(
bacsMandateConfirmationLauncherFactory = bacsMandateConfirmationLauncherFactory,
googlePayPaymentMethodLauncherFactory = googlePayPaymentMethodLauncherFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.stripe.android.paymentsheet.CreateIntentResult
import com.stripe.android.paymentsheet.DeferredIntentValidator
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.R
import com.stripe.android.paymentsheet.injection.ALLOWS_MANUAL_CONFIRMATION
import com.stripe.android.paymentsheet.state.PaymentElementLoader
import javax.inject.Inject
import javax.inject.Named
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.stripe.android.model.PaymentMethodOptionsParams
import com.stripe.android.model.SetupIntent
import com.stripe.android.model.StripeIntent
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler
import com.stripe.android.paymentelement.confirmation.DefaultConfirmationHandler
import com.stripe.android.paymentelement.confirmation.DeferredIntentConfirmationType
import com.stripe.android.paymentelement.confirmation.toConfirmationOption
import com.stripe.android.payments.core.analytics.ErrorReporter
Expand Down Expand Up @@ -85,7 +84,7 @@ internal class PaymentSheetViewModel @Inject internal constructor(
@IOContext workContext: CoroutineContext,
savedStateHandle: SavedStateHandle,
linkHandler: LinkHandler,
confirmationHandlerFactory: DefaultConfirmationHandler.Factory,
confirmationHandlerFactory: ConfirmationHandler.Factory,
cardAccountRangeRepositoryFactory: CardAccountRangeRepository.Factory,
editInteractorFactory: ModifiableEditPaymentMethodViewInteractor.Factory,
private val errorReporter: ErrorReporter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.stripe.android.paymentsheet.flowcontroller
import android.app.Application
import android.content.Context
import androidx.lifecycle.viewModelScope
import com.stripe.android.paymentelement.confirmation.ALLOWS_MANUAL_CONFIRMATION
import com.stripe.android.payments.core.injection.PRODUCT_USAGE
import com.stripe.android.paymentsheet.analytics.EventReporter
import com.stripe.android.paymentsheet.injection.ALLOWS_MANUAL_CONFIRMATION
import com.stripe.android.paymentsheet.injection.PaymentOptionsViewModelSubcomponent
import com.stripe.android.uicore.image.StripeImageLoader
import dagger.Module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import com.stripe.android.core.injection.CoreCommonModule
import com.stripe.android.core.injection.CoroutineContextModule
import com.stripe.android.googlepaylauncher.injection.GooglePayLauncherModule
import com.stripe.android.paymentelement.confirmation.ConfirmationModule
import com.stripe.android.payments.core.injection.StripeRepositoryModule
import com.stripe.android.paymentsheet.PaymentOptionsViewModel
import com.stripe.android.paymentsheet.injection.PaymentSheetCommonModule
Expand All @@ -16,6 +17,7 @@ import javax.inject.Singleton
@Component(
modules = [
StripeRepositoryModule::class,
ConfirmationModule::class,
PaymentSheetCommonModule::class,
FlowControllerModule::class,
GooglePayLauncherModule::class,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import com.stripe.android.link.LinkConfigurationCoordinator
import com.stripe.android.link.RealLinkConfigurationCoordinator
import com.stripe.android.link.injection.LinkAnalyticsComponent
import com.stripe.android.link.injection.LinkComponent
import com.stripe.android.paymentelement.confirmation.DefaultIntentConfirmationInterceptor
import com.stripe.android.paymentelement.confirmation.IntentConfirmationInterceptor
import com.stripe.android.payments.core.analytics.ErrorReporter
import com.stripe.android.payments.core.analytics.RealErrorReporter
import com.stripe.android.paymentsheet.BuildConfig
Expand All @@ -33,8 +31,6 @@ import com.stripe.android.paymentsheet.cvcrecollection.CvcRecollectionHandler
import com.stripe.android.paymentsheet.cvcrecollection.CvcRecollectionHandlerImpl
import com.stripe.android.paymentsheet.flowcontroller.DefaultPaymentSelectionUpdater
import com.stripe.android.paymentsheet.flowcontroller.PaymentSelectionUpdater
import com.stripe.android.paymentsheet.paymentdatacollection.bacs.BacsMandateConfirmationLauncherFactory
import com.stripe.android.paymentsheet.paymentdatacollection.bacs.DefaultBacsMandateConfirmationLauncherFactory
import com.stripe.android.paymentsheet.paymentdatacollection.cvcrecollection.CvcRecollectionInteractor
import com.stripe.android.paymentsheet.paymentdatacollection.cvcrecollection.CvcRecollectionLauncherFactory
import com.stripe.android.paymentsheet.paymentdatacollection.cvcrecollection.DefaultCvcRecollectionInteractor
Expand Down Expand Up @@ -91,11 +87,6 @@ internal abstract class PaymentSheetCommonModule {
impl: DefaultLinkAccountStatusProvider,
): LinkAccountStatusProvider

@Binds
abstract fun bindsIntentConfirmationInterceptor(
impl: DefaultIntentConfirmationInterceptor,
): IntentConfirmationInterceptor

@Binds
abstract fun bindsPaymentSheetUpdater(
impl: DefaultPaymentSelectionUpdater,
Expand Down Expand Up @@ -154,12 +145,6 @@ internal abstract class PaymentSheetCommonModule {
)
}

@Provides
@Singleton
fun provideBacsMandateConfirmationLauncherFactory(): BacsMandateConfirmationLauncherFactory {
return DefaultBacsMandateConfirmationLauncherFactory
}

@Provides
@Singleton
fun provideCvcRecollectionLauncherFactory(): CvcRecollectionLauncherFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.paymentsheet.injection

import android.app.Application
import android.content.Context
import com.stripe.android.paymentelement.confirmation.ALLOWS_MANUAL_CONFIRMATION
import com.stripe.android.payments.core.injection.PRODUCT_USAGE
import com.stripe.android.paymentsheet.analytics.EventReporter
import dagger.Binds
Expand Down
Loading

0 comments on commit 29ba1ce

Please sign in to comment.