Skip to content

Commit

Permalink
Move LinkState into PaymentMethodMetadata. (#9915)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe authored Jan 15, 2025
1 parent 6a4c71c commit e71c106
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.stripe.android.paymentsheet.addresselement.AddressDetails
import com.stripe.android.paymentsheet.model.PaymentMethodIncentive
import com.stripe.android.paymentsheet.model.PaymentSelection
import com.stripe.android.paymentsheet.model.toPaymentMethodIncentive
import com.stripe.android.paymentsheet.state.LinkState
import com.stripe.android.ui.core.Amount
import com.stripe.android.ui.core.cbc.CardBrandChoiceEligibility
import com.stripe.android.ui.core.elements.ExternalPaymentMethodSpec
Expand Down Expand Up @@ -54,6 +55,7 @@ internal data class PaymentMethodMetadata(
val linkInlineConfiguration: LinkInlineConfiguration?,
val paymentMethodSaveConsentBehavior: PaymentMethodSaveConsentBehavior,
val linkMode: LinkMode?,
val linkState: LinkState?,
val paymentMethodIncentive: PaymentMethodIncentive?,
val financialConnectionsAvailable: Boolean = DefaultIsFinancialConnectionsAvailable(),
val cardBrandFilter: CardBrandFilter,
Expand Down Expand Up @@ -237,6 +239,7 @@ internal data class PaymentMethodMetadata(
externalPaymentMethodSpecs: List<ExternalPaymentMethodSpec>,
isGooglePayReady: Boolean,
linkInlineConfiguration: LinkInlineConfiguration?,
linkState: LinkState?,
): PaymentMethodMetadata {
val linkSettings = elementsSession.linkSettings
return PaymentMethodMetadata(
Expand All @@ -259,6 +262,7 @@ internal data class PaymentMethodMetadata(
paymentMethodSaveConsentBehavior = elementsSession.toPaymentSheetSaveConsentBehavior(),
linkInlineConfiguration = linkInlineConfiguration,
linkMode = linkSettings?.linkMode,
linkState = linkState,
paymentMethodIncentive = linkSettings?.linkConsumerIncentive?.toPaymentMethodIncentive(),
isGooglePayReady = isGooglePayReady,
cardBrandFilter = PaymentSheetCardBrandFilter(configuration.cardBrandAcceptance)
Expand Down Expand Up @@ -293,6 +297,7 @@ internal data class PaymentMethodMetadata(
financialConnectionsAvailable = isFinancialConnectionsAvailable(),
paymentMethodSaveConsentBehavior = paymentMethodSaveConsentBehavior,
linkMode = elementsSession.linkSettings?.linkMode,
linkState = null,
paymentMethodIncentive = null,
externalPaymentMethodSpecs = emptyList(),
cardBrandFilter = PaymentSheetCardBrandFilter(configuration.cardBrandAcceptance)
Expand Down Expand Up @@ -322,6 +327,7 @@ internal data class PaymentMethodMetadata(
paymentMethodSaveConsentBehavior = PaymentMethodSaveConsentBehavior.Disabled(null),
linkInlineConfiguration = null,
linkMode = null,
linkState = null,
paymentMethodIncentive = null,
isGooglePayReady = false,
cardBrandFilter = PaymentSheetCardBrandFilter(PaymentSheet.CardBrandAcceptance.all())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal class PaymentOptionsViewModel @Inject constructor(
// This is bad, but I don't think there's a better option
PaymentSheet.FlowController.linkHandler = linkHandler

linkHandler.setupLink(args.state.linkState)
linkHandler.setupLink(args.state.paymentMethodMetadata.linkState)

// After recovering from don't keep activities the paymentMethodMetadata will be saved,
// calling setPaymentMethodMetadata would require the repository be initialized, which
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ internal class PaymentSheetViewModel @Inject internal constructor(

setPaymentMethodMetadata(state.paymentMethodMetadata)

linkHandler.setupLink(state.linkState)
linkHandler.setupLink(state.paymentMethodMetadata.linkState)

val pendingFailedPaymentResult = confirmationHandler.awaitResult()
as? ConfirmationHandler.Result.Failed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ internal class DefaultFlowController @Inject internal constructor(
viewModelScope.launch {
val confirmationOption = paymentSelection?.toConfirmationOption(
configuration = state.config,
linkConfiguration = state.linkState?.configuration,
linkConfiguration = state.paymentMethodMetadata.linkState?.configuration,
)

confirmationOption?.let { option ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class DefaultPaymentSelectionUpdater @Inject constructor() : PaymentSel
state.paymentMethodMetadata.isGooglePayReady
}
is PaymentSelection.Link -> {
state.linkState != null
state.paymentMethodMetadata.linkState != null
}
is PaymentSelection.ExternalPaymentMethod -> {
state.paymentMethodMetadata.isExternalPaymentMethod(selection.type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ internal interface PaymentElementLoader {
data class State(
val config: CommonConfiguration,
val customer: CustomerState?,
val linkState: LinkState?,
val paymentSelection: PaymentSelection?,
val validationError: PaymentSheetLoadingException?,
val paymentMethodMetadata: PaymentMethodMetadata,
Expand Down Expand Up @@ -214,7 +213,6 @@ internal class DefaultPaymentElementLoader @Inject constructor(
val state = PaymentElementLoader.State(
config = configuration,
customer = customer.await(),
linkState = linkState.await(),
paymentSelection = initialPaymentSelection.await(),
validationError = stripeIntent.validate(),
paymentMethodMetadata = paymentMethodMetadata,
Expand Down Expand Up @@ -276,6 +274,7 @@ internal class DefaultPaymentElementLoader @Inject constructor(
externalPaymentMethodSpecs = externalPaymentMethodSpecs,
isGooglePayReady = isGooglePayReady,
linkInlineConfiguration = createLinkInlineConfiguration(linkState),
linkState = linkState,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ internal sealed interface PaymentSheetState : Parcelable {
data class Full(
val config: CommonConfiguration,
val customer: CustomerState?,
val linkState: LinkState?,
val paymentSelection: PaymentSelection?,
val validationError: PaymentSheetLoadingException?,
val paymentMethodMetadata: PaymentMethodMetadata,
) : PaymentSheetState {
constructor(state: PaymentElementLoader.State) : this(
config = state.config,
customer = state.customer,
linkState = state.linkState,
paymentSelection = state.paymentSelection,
validationError = state.validationError,
paymentMethodMetadata = state.paymentMethodMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.PaymentSheetFixtures
import com.stripe.android.paymentsheet.addresselement.AddressDetails
import com.stripe.android.paymentsheet.model.PaymentMethodIncentive
import com.stripe.android.paymentsheet.state.LinkState
import com.stripe.android.ui.core.cbc.CardBrandChoiceEligibility
import com.stripe.android.ui.core.elements.ExternalPaymentMethodSpec
import com.stripe.android.ui.core.elements.LpmSerializer
Expand All @@ -33,6 +34,7 @@ internal object PaymentMethodMetadataFactory {
paymentMethodSaveConsentBehavior: PaymentMethodSaveConsentBehavior = PaymentMethodSaveConsentBehavior.Legacy,
linkInlineConfiguration: LinkInlineConfiguration? = null,
linkMode: LinkMode? = LinkMode.LinkPaymentMethod,
linkState: LinkState? = null,
cardBrandFilter: CardBrandFilter = DefaultCardBrandFilter,
defaultBillingDetails: PaymentSheet.BillingDetails = PaymentSheet.BillingDetails(),
paymentMethodIncentive: PaymentMethodIncentive? = null,
Expand All @@ -55,6 +57,7 @@ internal object PaymentMethodMetadataFactory {
isGooglePayReady = isGooglePayReady,
linkInlineConfiguration = linkInlineConfiguration,
linkMode = linkMode,
linkState = linkState,
cardBrandFilter = cardBrandFilter,
paymentMethodIncentive = paymentMethodIncentive,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ internal class PaymentMethodMetadataTest {
externalPaymentMethodSpecs = externalPaymentMethodSpecs,
isGooglePayReady = false,
linkInlineConfiguration = linkInlineConfiguration,
linkState = null,
)

val expectedMetadata = PaymentMethodMetadata(
Expand All @@ -789,6 +790,7 @@ internal class PaymentMethodMetadataTest {
isGooglePayReady = false,
linkInlineConfiguration = linkInlineConfiguration,
linkMode = null,
linkState = null,
cardBrandFilter = PaymentSheetCardBrandFilter(cardBrandAcceptance),
paymentMethodIncentive = null,
)
Expand Down Expand Up @@ -854,6 +856,7 @@ internal class PaymentMethodMetadataTest {
financialConnectionsAvailable = false,
linkInlineConfiguration = null,
linkMode = null,
linkState = null,
cardBrandFilter = PaymentSheetCardBrandFilter(cardBrandAcceptance),
paymentMethodIncentive = null,
)
Expand Down Expand Up @@ -924,6 +927,7 @@ internal class PaymentMethodMetadataTest {
externalPaymentMethodSpecs = listOf(),
isGooglePayReady = false,
linkInlineConfiguration = null,
linkState = null,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ internal class DefaultEmbeddedConfigurationHandlerTest {
PaymentElementLoader.State(
config = configuration,
customer = null,
linkState = null,
paymentSelection = null,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ internal class SharedPaymentElementViewModelTest {
PaymentElementLoader.State(
config = configuration.asCommonConfiguration(),
customer = null,
linkState = null,
paymentSelection = null,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(
Expand Down Expand Up @@ -78,7 +77,6 @@ internal class SharedPaymentElementViewModelTest {
PaymentElementLoader.State(
config = configuration.asCommonConfiguration(),
customer = null,
linkState = null,
paymentSelection = PaymentSelection.GooglePay,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(
Expand Down Expand Up @@ -120,7 +118,6 @@ internal class SharedPaymentElementViewModelTest {
PaymentElementLoader.State(
config = configuration.asCommonConfiguration(),
customer = null,
linkState = null,
paymentSelection = null,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(
Expand Down Expand Up @@ -159,7 +156,6 @@ internal class SharedPaymentElementViewModelTest {
PaymentElementLoader.State(
config = configuration.asCommonConfiguration(),
customer = null,
linkState = null,
paymentSelection = PaymentSelection.GooglePay,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(
Expand Down Expand Up @@ -211,7 +207,6 @@ internal class SharedPaymentElementViewModelTest {
PaymentElementLoader.State(
config = configuration.asCommonConfiguration(),
customer = null,
linkState = null,
paymentSelection = PaymentSelection.GooglePay,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(
Expand Down Expand Up @@ -264,7 +259,6 @@ internal class SharedPaymentElementViewModelTest {
PaymentElementLoader.State(
config = configuration.asCommonConfiguration(),
customer = null,
linkState = null,
paymentSelection = PaymentSelection.GooglePay,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -777,11 +777,17 @@ internal class PaymentOptionsViewModelTest {

private fun createViewModel(
args: PaymentOptionContract.Args = PAYMENT_OPTION_CONTRACT_ARGS,
linkState: LinkState? = args.state.linkState,
linkState: LinkState? = args.state.paymentMethodMetadata.linkState,
linkConfigurationCoordinator: LinkConfigurationCoordinator = FakeLinkConfigurationCoordinator()
) = TestViewModelFactory.create(linkConfigurationCoordinator) { linkHandler, savedStateHandle ->
PaymentOptionsViewModel(
args = args.copy(state = args.state.copy(linkState = linkState)),
args = args.copy(
state = args.state.copy(
paymentMethodMetadata = args.state.paymentMethodMetadata.copy(
linkState = linkState
)
)
),
eventReporter = eventReporter,
customerRepository = customerRepository,
workContext = testDispatcher,
Expand Down Expand Up @@ -832,7 +838,6 @@ internal class PaymentOptionsViewModelTest {
customer = PaymentSheetFixtures.EMPTY_CUSTOMER_STATE,
config = PaymentSheetFixtures.CONFIG_CUSTOMER_WITH_GOOGLEPAY.asCommonConfiguration(),
paymentSelection = null,
linkState = null,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(
stripeIntent = PAYMENT_INTENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ internal object PaymentSheetFixtures {
customer = EMPTY_CUSTOMER_STATE,
config = CONFIG_GOOGLEPAY.asCommonConfiguration(),
paymentSelection = null,
linkState = null,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(),
),
Expand All @@ -152,7 +151,7 @@ internal object PaymentSheetFixtures {
stripeIntent: StripeIntent = state.stripeIntent,
config: PaymentSheet.Configuration = configuration,
paymentSelection: PaymentSelection? = state.paymentSelection,
linkState: LinkState? = state.linkState,
linkState: LinkState? = state.paymentMethodMetadata.linkState,
): PaymentOptionContract.Args {
return copy(
state = state.copy(
Expand All @@ -170,10 +169,10 @@ internal object PaymentSheetFixtures {
),
config = config.asCommonConfiguration(),
paymentSelection = paymentSelection,
linkState = linkState,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(
stripeIntent = stripeIntent,
isGooglePayReady = isGooglePayReady,
linkState = linkState,
),
),
configuration = config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ internal class DefaultFlowControllerTest {
customer = PaymentSheetFixtures.EMPTY_CUSTOMER_STATE,
config = PaymentSheet.Configuration("com.stripe.android.paymentsheet.test").asCommonConfiguration(),
paymentSelection = null,
linkState = null,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(allowsDelayedPaymentMethods = false),
),
Expand Down Expand Up @@ -665,7 +664,6 @@ internal class DefaultFlowControllerTest {
customer = PaymentSheetFixtures.EMPTY_CUSTOMER_STATE.copy(
paymentMethods = PAYMENT_METHODS
),
linkState = null,
paymentSelection = initialSelection,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(),
Expand Down Expand Up @@ -706,7 +704,6 @@ internal class DefaultFlowControllerTest {
customer = PaymentSheetFixtures.EMPTY_CUSTOMER_STATE.copy(
paymentMethods = PAYMENT_METHODS
),
linkState = null,
paymentSelection = initialSelection,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(),
Expand Down Expand Up @@ -750,7 +747,6 @@ internal class DefaultFlowControllerTest {
customer = PaymentSheetFixtures.EMPTY_CUSTOMER_STATE.copy(
paymentMethods = PAYMENT_METHODS
),
linkState = null,
paymentSelection = initialSelection,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(),
Expand Down Expand Up @@ -2400,7 +2396,6 @@ internal class DefaultFlowControllerTest {
customer = PaymentSheetFixtures.EMPTY_CUSTOMER_STATE.copy(
paymentMethods = PAYMENT_METHODS
),
linkState = null,
paymentSelection = null,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ class PaymentSelectionUpdaterTest {
customer = PaymentSheetFixtures.EMPTY_CUSTOMER_STATE.copy(
paymentMethods = customerPaymentMethods
),
linkState = null,
paymentSelection = paymentSelection,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(
Expand Down Expand Up @@ -392,7 +391,6 @@ class PaymentSelectionUpdaterTest {
customer = PaymentSheetFixtures.EMPTY_CUSTOMER_STATE.copy(
paymentMethods = customerPaymentMethods
),
linkState = null,
paymentSelection = paymentSelection,
validationError = null,
paymentMethodMetadata = PaymentMethodMetadataFactory.create(
Expand Down
Loading

0 comments on commit e71c106

Please sign in to comment.