Skip to content

Commit

Permalink
simplify confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
toluo-stripe committed Jan 20, 2025
1 parent a7c98c5 commit 24cf02f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.link.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ReadOnlyComposable
Expand All @@ -27,8 +28,13 @@ internal fun DefaultLinkTheme(
colors = colors.materialColors,
typography = Typography,
shapes = MaterialTheme.shapes,
content = content
)
) {
Surface(
color = MaterialTheme.colors.background
) {
content()
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ internal fun PaymentSelection.toConfirmationOption(
}
}

internal fun LinkAccount.toConfirmationOption(
linkConfiguration: LinkConfiguration
): ConfirmationHandler.Option? {
return toLinkConfirmationOption(
linkConfiguration = linkConfiguration,
linkAccount = this
)
}

private fun toLinkConfirmationOption(
linkConfiguration: LinkConfiguration?,
linkAccount: LinkAccount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal class LinkHandler @Inject constructor(
coroutineScope: CoroutineScope,
state: LinkState?,
launchEagerly: Boolean = false,
launchLink: (LinkAccount) -> Unit = {},
launchLink: suspend (LinkAccount) -> Unit = {},
) {
setupLink(state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,11 @@ internal class PaymentSheetViewModel @Inject internal constructor(
state = state.paymentMethodMetadata.linkState,
launchEagerly = FeatureFlags.nativeLinkEnabled.isEnabled,
launchLink = { linkAccount ->
checkoutWithLinkExpress(linkAccount)
val configuration = state.paymentMethodMetadata.linkState?.configuration ?: return@setupLinkLaunchEagerly
val confirmationOption = linkAccount.toConfirmationOption(configuration)
confirmationOption?.let {
confirmWithConfirmationOption(it)
}
}
)

Expand Down Expand Up @@ -491,42 +495,46 @@ internal class PaymentSheetViewModel @Inject internal constructor(
linkConfiguration = linkHandler.linkConfiguration.value,
)

confirmationOption?.let { option ->
val stripeIntent = awaitStripeIntent()

confirmationHandler.start(
arguments = ConfirmationHandler.Args(
intent = stripeIntent,
confirmationOption = option,
initializationMode = args.initializationMode,
appearance = config.appearance,
shippingDetails = config.shippingDetails,
),
)
} ?: run {
val message = paymentSelection?.let {
"Cannot confirm using a ${it::class.qualifiedName} payment selection!"
} ?: "Cannot confirm without a payment selection!"
if (confirmationOption != null) {
return@launch confirmWithConfirmationOption(confirmationOption)
}

val exception = IllegalStateException(message)
val message = paymentSelection?.let {
"Cannot confirm using a ${it::class.qualifiedName} payment selection!"
} ?: "Cannot confirm without a payment selection!"

val event = paymentSelection?.let {
ErrorReporter.UnexpectedErrorEvent.PAYMENT_SHEET_INVALID_PAYMENT_SELECTION_ON_CHECKOUT
} ?: ErrorReporter.UnexpectedErrorEvent.PAYMENT_SHEET_NO_PAYMENT_SELECTION_ON_CHECKOUT
val exception = IllegalStateException(message)

errorReporter.report(event, StripeException.create(exception))
val event = paymentSelection?.let {
ErrorReporter.UnexpectedErrorEvent.PAYMENT_SHEET_INVALID_PAYMENT_SELECTION_ON_CHECKOUT
} ?: ErrorReporter.UnexpectedErrorEvent.PAYMENT_SHEET_NO_PAYMENT_SELECTION_ON_CHECKOUT

processIntentResult(
ConfirmationHandler.Result.Failed(
cause = exception,
message = exception.stripeErrorMessage(),
type = ConfirmationHandler.Result.Failed.ErrorType.Internal,
)
errorReporter.report(event, StripeException.create(exception))

processIntentResult(
ConfirmationHandler.Result.Failed(
cause = exception,
message = exception.stripeErrorMessage(),
type = ConfirmationHandler.Result.Failed.ErrorType.Internal,
)
}
)
}
}

private suspend fun confirmWithConfirmationOption(confirmationOption: ConfirmationHandler.Option) {
val stripeIntent = awaitStripeIntent()

confirmationHandler.start(
arguments = ConfirmationHandler.Args(
intent = stripeIntent,
confirmationOption = confirmationOption,
initializationMode = args.initializationMode,
appearance = config.appearance,
shippingDetails = config.shippingDetails,
),
)
}

override fun onPaymentResult(paymentResult: PaymentResult) {
viewModelScope.launch(workContext) {
val stripeIntent = awaitStripeIntent()
Expand Down

0 comments on commit 24cf02f

Please sign in to comment.