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

Tolu/intent conf proto #9729

Closed
wants to merge 19 commits into from
Closed
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 @@ -21,13 +21,16 @@ import com.stripe.android.link.model.LinkAccount
import com.stripe.android.link.ui.LinkAppBarState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject

internal class LinkActivityViewModel @Inject constructor(
val activityRetainedComponent: NativeLinkComponent,
private val linkAccountManager: LinkAccountManager
private val linkAccountManager: LinkAccountManager,
private val linkIntentConfirmationHandler: LinkIntentConfirmationHandler
) : ViewModel(), DefaultLifecycleObserver {
private val _linkState = MutableStateFlow(
value = LinkAppBarState(
Expand All @@ -45,6 +48,16 @@ internal class LinkActivityViewModel @Inject constructor(
var navController: NavHostController? = null
var dismissWithResult: ((LinkActivityResult) -> Unit)? = null

fun listenForSuccessfulPayment() {
viewModelScope.launch {
linkIntentConfirmationHandler.state
.filterIsInstance<LinkIntentConfirmationHandler.State.Success>()
.collect {
// dismissWithResult?.invoke(LinkActivityResult.Completed())
}
}
}

fun handleViewAction(action: LinkAction) {
when (action) {
LinkAction.BackPressed -> handleBackPressed()
Expand Down Expand Up @@ -102,10 +115,11 @@ internal class LinkActivityViewModel @Inject constructor(
val handle: SavedStateHandle = savedStateHandle ?: createSavedStateHandle()
val app = this[APPLICATION_KEY] as Application
val args: NativeLinkArgs = getArgs(handle) ?: throw NoArgsException()

val linkIntentConfirmationHandler = LinkIntentConfirmation.handler ?: throw NoArgsException()
DaggerNativeLinkComponent
.builder()
.configuration(args.configuration)
.linkIntentConfirmationHandler(linkIntentConfirmationHandler)
.publishableKeyProvider { args.publishableKey }
.stripeAccountIdProvider { args.stripeAccountId }
.context(app)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.stripe.android.link

import com.stripe.android.core.strings.ResolvableString
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.model.StripeIntent
import kotlinx.coroutines.flow.Flow

interface LinkIntentConfirmationHandler {
val state: Flow<State>

suspend fun confirmIntent(
intent: StripeIntent,
params: PaymentMethodCreateParams
)

sealed interface State {
data class Failed(
val cause: Throwable,
val message: ResolvableString,
) : State

data object Cancelled : State

data object Idle : State

data object Success : State
}
}

object LinkIntentConfirmation {
var handler: LinkIntentConfirmationHandler? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class LinkPaymentLauncher @Inject internal constructor(
fun unregister() {
linkActivityResultLauncher?.unregister()
linkActivityResultLauncher = null
LinkIntentConfirmation.handler = null
}

/**
Expand All @@ -68,7 +69,9 @@ class LinkPaymentLauncher @Inject internal constructor(
*/
fun present(
configuration: LinkConfiguration,
linkIntentConfirmationHandler: LinkIntentConfirmationHandler? = null
) {
LinkIntentConfirmation.handler = linkIntentConfirmationHandler
val args = LinkActivityContract.Args(
configuration,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.stripe.android.core.injection.PUBLISHABLE_KEY
import com.stripe.android.core.injection.STRIPE_ACCOUNT_ID
import com.stripe.android.link.LinkActivityViewModel
import com.stripe.android.link.LinkConfiguration
import com.stripe.android.link.LinkIntentConfirmationHandler
import com.stripe.android.link.account.LinkAccountManager
import com.stripe.android.link.analytics.LinkEventsReporter
import dagger.BindsInstance
Expand All @@ -29,12 +30,16 @@ internal interface NativeLinkComponent {
val linkEventsReporter: LinkEventsReporter
val logger: Logger
val viewModel: LinkActivityViewModel
val linkIntentConfirmationHandler: LinkIntentConfirmationHandler

@Component.Builder
interface Builder {
@BindsInstance
fun configuration(configuration: LinkConfiguration): Builder

@BindsInstance
fun linkIntentConfirmationHandler(linkIntentConfirmationHandler: LinkIntentConfirmationHandler): Builder

@BindsInstance
fun publishableKeyProvider(@Named(PUBLISHABLE_KEY) publishableKeyProvider: () -> String): Builder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ internal class WalletScreenTest {
composeTestRule.setContent {
WalletScreen(viewModel)
}
composeTestRule.waitForIdle()

onWalletCollapsedHeader().assertIsDisplayed()
onWalletCollapsedChevron().assertIsDisplayed()
Expand All @@ -56,12 +55,8 @@ internal class WalletScreenTest {
WalletScreen(viewModel)
}

composeTestRule.waitForIdle()

onCollapsedWalletRow().performClick()

composeTestRule.waitForIdle()

onWalletAddPaymentMethodRow().assertIsDisplayed().assertHasClickAction()
onExpandedWalletHeader().assertIsDisplayed()
onPaymentMethodList().assertCountEquals(3)
Expand All @@ -77,8 +72,6 @@ internal class WalletScreenTest {
WalletScreen(viewModel)
}

composeTestRule.waitForIdle()

onLoader().assertIsDisplayed()
onPaymentMethodList().assertCountEquals(0)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.stripe.android.paymentelement.confirmation

import com.stripe.android.link.LinkIntentConfirmationHandler
import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.model.SetupIntent
import com.stripe.android.model.StripeIntent
import com.stripe.android.paymentsheet.state.PaymentElementLoader
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

internal class DefaultLinkIntentConfirmationHandler @Inject constructor(
private val confirmationHandler: ConfirmationHandler
) : LinkIntentConfirmationHandler {

override val state: Flow<LinkIntentConfirmationHandler.State>
get() {
return confirmationHandler.state.map(::transformConfirmationHandlerState)
}

override suspend fun confirmIntent(
intent: StripeIntent,
params: PaymentMethodCreateParams
) {
confirmationHandler.start(
arguments = ConfirmationHandler.Args(
intent = intent,
confirmationOption = PaymentMethodConfirmationOption.New(
initializationMode = intent.initializationMode(),
shippingDetails = null,
createParams = params,
optionsParams = null,
shouldSave = false
)
)
)
}

private fun transformConfirmationHandlerState(
confirmationHandlerState: ConfirmationHandler.State
): LinkIntentConfirmationHandler.State {
return when (confirmationHandlerState) {
is ConfirmationHandler.State.Complete -> {
when (val result = confirmationHandlerState.result) {
is ConfirmationHandler.Result.Canceled -> {
LinkIntentConfirmationHandler.State.Cancelled
}
is ConfirmationHandler.Result.Failed -> {
LinkIntentConfirmationHandler.State.Failed(
cause = result.cause,
message = result.message
)
}
is ConfirmationHandler.Result.Succeeded -> {
LinkIntentConfirmationHandler.State.Success
}
}
}
is ConfirmationHandler.State.Confirming -> {
LinkIntentConfirmationHandler.State.Idle
}
ConfirmationHandler.State.Idle -> {
LinkIntentConfirmationHandler.State.Idle
}
}
}

private fun StripeIntent.initializationMode(): PaymentElementLoader.InitializationMode {
return when (this) {
is PaymentIntent -> {
PaymentElementLoader.InitializationMode.PaymentIntent(
clientSecret = clientSecret ?: ""
)
}
is SetupIntent -> PaymentElementLoader.InitializationMode.SetupIntent(
clientSecret = clientSecret ?: ""
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.SavedStateHandle
import com.stripe.android.link.LinkActivityResult
import com.stripe.android.link.LinkConfiguration
import com.stripe.android.link.LinkConfigurationCoordinator
import com.stripe.android.link.LinkIntentConfirmationHandler
import com.stripe.android.link.LinkPaymentDetails
import com.stripe.android.link.LinkPaymentLauncher
import com.stripe.android.link.account.LinkStore
Expand Down Expand Up @@ -37,6 +38,7 @@ internal class LinkHandler @Inject constructor(
val linkConfigurationCoordinator: LinkConfigurationCoordinator,
private val savedStateHandle: SavedStateHandle,
private val linkStore: LinkStore,
private val linkIntentConfirmationHandler: LinkIntentConfirmationHandler,
linkAnalyticsComponentBuilder: LinkAnalyticsComponent.Builder,
) {
sealed class ProcessingState {
Expand Down Expand Up @@ -203,6 +205,7 @@ internal class LinkHandler @Inject constructor(

linkLauncher.present(
config,
linkIntentConfirmationHandler
)

_processingState.tryEmit(ProcessingState.Launched)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.stripe.android.common.exception.stripeErrorMessage
import com.stripe.android.core.exception.StripeException
import com.stripe.android.core.injection.ENABLE_LOGGING
import com.stripe.android.link.LinkActivityResult
import com.stripe.android.link.LinkIntentConfirmationHandler
import com.stripe.android.link.LinkPaymentLauncher
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodOptionsParams
Expand Down Expand Up @@ -85,7 +86,8 @@ internal class DefaultFlowController @Inject internal constructor(
private val configurationHandler: FlowControllerConfigurationHandler,
private val errorReporter: ErrorReporter,
@InitializedViaCompose private val initializedViaCompose: Boolean,
private val cvcRecollectionHandler: CvcRecollectionHandler
private val cvcRecollectionHandler: CvcRecollectionHandler,
private val linkIntentConfirmationHandler: LinkIntentConfirmationHandler
) : PaymentSheet.FlowController {
private val paymentOptionActivityLauncher: ActivityResultLauncher<PaymentOptionContract.Args>
private val sepaMandateActivityLauncher: ActivityResultLauncher<SepaMandateContract.Args>
Expand Down Expand Up @@ -662,7 +664,10 @@ internal class DefaultFlowController @Inject internal constructor(

if (paymentSelection is PaymentSelection.Link) {
// User selected Link as the payment method, not inline
linkLauncher.present(linkConfig)
linkLauncher.present(
linkConfig,
linkIntentConfirmationHandler
)
} else {
// New user paying inline, complete without launching Link
confirmPaymentSelection(paymentSelection, state, appearance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import android.content.Context
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.stripe.android.core.injection.IOContext
import com.stripe.android.link.LinkIntentConfirmationHandler
import com.stripe.android.paymentelement.confirmation.ALLOWS_MANUAL_CONFIRMATION
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler
import com.stripe.android.paymentelement.confirmation.DefaultLinkIntentConfirmationHandler
import com.stripe.android.payments.core.injection.PRODUCT_USAGE
import com.stripe.android.paymentsheet.analytics.EventReporter
import com.stripe.android.paymentsheet.injection.PaymentOptionsViewModelSubcomponent
Expand Down Expand Up @@ -64,6 +66,16 @@ internal object FlowControllerModule {
)
}

@Provides
@Singleton
fun providesLinkIntentConfirmationHandler(
confirmationHandler: ConfirmationHandler,
): LinkIntentConfirmationHandler {
return DefaultLinkIntentConfirmationHandler(
confirmationHandler = confirmationHandler
)
}

@Provides
@Singleton
fun provideStripeImageLoader(context: Context): StripeImageLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.stripe.android.paymentsheet.injection
import android.content.Context
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.PRODUCT_USAGE
import com.stripe.android.payments.core.injection.StripeRepositoryModule
import com.stripe.android.ui.core.forms.resources.injection.ResourceRepositoryModule
Expand All @@ -19,7 +21,9 @@ import javax.inject.Singleton
PaymentOptionsViewModelModule::class,
CoroutineContextModule::class,
CoreCommonModule::class,
ResourceRepositoryModule::class
ResourceRepositoryModule::class,
ConfirmationModule::class,
GooglePayLauncherModule::class,
]
)
internal interface PaymentOptionsViewModelFactoryComponent {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.stripe.android.paymentsheet.injection

import com.stripe.android.paymentelement.confirmation.ALLOWS_MANUAL_CONFIRMATION
//import com.stripe.android.paymentelement.confirmation.STATUS_BAR_COLOR_PROVIDER
import com.stripe.android.paymentsheet.analytics.EventReporter
import dagger.Module
import dagger.Provides
import javax.inject.Named
import javax.inject.Singleton

@Module(
Expand All @@ -15,4 +18,14 @@ internal class PaymentOptionsViewModelModule {
@Provides
@Singleton
fun provideEventReporterMode(): EventReporter.Mode = EventReporter.Mode.Custom

// @Provides
// @Named(STATUS_BAR_COLOR_PROVIDER)
// fun providesStatusBarColor(): () -> Int? {
// return { null }
// }

@Provides
@Named(ALLOWS_MANUAL_CONFIRMATION)
fun provideAllowsManualConfirmation() = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package com.stripe.android.paymentsheet.injection

import android.app.Application
import androidx.lifecycle.SavedStateHandle
import com.stripe.android.paymentelement.confirmation.ConfirmationModule
import com.stripe.android.paymentsheet.PaymentOptionContract
import com.stripe.android.paymentsheet.PaymentOptionsViewModel
import dagger.BindsInstance
import dagger.Subcomponent

@Subcomponent
@Subcomponent(
modules = [ConfirmationModule::class]
)
internal interface PaymentOptionsViewModelSubcomponent {
val viewModel: PaymentOptionsViewModel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import java.util.Objects
import kotlin.test.assertFailsWith

@RunWith(RobolectricTestRunner::class)
class DefaultIntentConfirmationInterceptorTest {
class DefaultLinkIntentConfirmationInterceptorTest {

@get:Rule
val intentConfirmationInterceptorTestRule = IntentConfirmationInterceptorTestRule()
Expand Down
Loading
Loading