Skip to content

Commit

Permalink
Remove hacky payment options params in paymentsheet. (#7639)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe authored Nov 17, 2023
1 parent 33ae56f commit b614347
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ sealed class ConfirmStripeIntentParamsFactory<out T : ConfirmStripeIntentParams>
abstract fun create(
createParams: PaymentMethodCreateParams,
optionsParams: PaymentMethodOptionsParams? = null,
setupFutureUsage: ConfirmPaymentIntentParams.SetupFutureUsage? = null,
): T

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
Expand Down Expand Up @@ -78,40 +77,11 @@ internal class ConfirmPaymentIntentParamsFactory(
override fun create(
createParams: PaymentMethodCreateParams,
optionsParams: PaymentMethodOptionsParams?,
setupFutureUsage: ConfirmPaymentIntentParams.SetupFutureUsage?,
): ConfirmPaymentIntentParams {
return ConfirmPaymentIntentParams.createWithPaymentMethodCreateParams(
paymentMethodCreateParams = createParams,
clientSecret = clientSecret,

/**
Sets `payment_method_options[card][setup_future_usage]`
- Note: PaymentSheet uses this `setup_future_usage` (SFU) value very differently from the top-level one:
We read the top-level SFU to know the merchant’s desired save behavior
We write payment method options SFU to set the customer’s desired save behavior
*/
// At this time, paymentMethodOptions card and us_bank_account is the only PM that
// supports setup future usage
paymentMethodOptions = when (createParams.typeCode) {
PaymentMethod.Type.Card.code -> {
PaymentMethodOptionsParams.Card(setupFutureUsage = setupFutureUsage)
}
PaymentMethod.Type.USBankAccount.code -> {
PaymentMethodOptionsParams.USBankAccount(setupFutureUsage = setupFutureUsage)
}
PaymentMethod.Type.Blik.code -> {
optionsParams
}
PaymentMethod.Type.Konbini.code -> {
optionsParams
}
PaymentMethod.Type.Link.code -> {
null
}
else -> {
PaymentMethodOptionsParams.Card(setupFutureUsage = null)
}
},
paymentMethodOptions = optionsParams,
shipping = shipping,
)
}
Expand All @@ -133,7 +103,6 @@ internal class ConfirmSetupIntentParamsFactory(
override fun create(
createParams: PaymentMethodCreateParams,
optionsParams: PaymentMethodOptionsParams?,
setupFutureUsage: ConfirmPaymentIntentParams.SetupFutureUsage?,
): ConfirmSetupIntentParams {
return ConfirmSetupIntentParams.create(
paymentMethodCreateParams = createParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class ConfirmPaymentIntentParamsFactoryTest {
assertThat(
factory.create(
createParams = PaymentMethodCreateParamsFixtures.DEFAULT_CARD,
setupFutureUsage = ConfirmPaymentIntentParams.SetupFutureUsage.OffSession,
optionsParams = PaymentMethodOptionsParams.Card(
setupFutureUsage = ConfirmPaymentIntentParams.SetupFutureUsage.OffSession
)
)
).isEqualTo(
ConfirmPaymentIntentParams.createWithPaymentMethodCreateParams(
Expand All @@ -39,7 +41,9 @@ class ConfirmPaymentIntentParamsFactoryTest {
assertThat(
factory.create(
createParams = PaymentMethodCreateParamsFixtures.DEFAULT_CARD,
setupFutureUsage = ConfirmPaymentIntentParams.SetupFutureUsage.Blank,
optionsParams = PaymentMethodOptionsParams.Card(
setupFutureUsage = ConfirmPaymentIntentParams.SetupFutureUsage.Blank
)
)
).isEqualTo(
ConfirmPaymentIntentParams.createWithPaymentMethodCreateParams(
Expand All @@ -58,7 +62,7 @@ class ConfirmPaymentIntentParamsFactoryTest {
assertThat(
factory.create(
createParams = PaymentMethodCreateParamsFixtures.DEFAULT_CARD,
setupFutureUsage = null,
optionsParams = PaymentMethodOptionsParams.Card(),
)
).isEqualTo(
ConfirmPaymentIntentParams.createWithPaymentMethodCreateParams(
Expand Down Expand Up @@ -108,7 +112,6 @@ class ConfirmPaymentIntentParamsFactoryTest {

val result = factoryWithConfig.create(
createParams = PaymentMethodCreateParamsFixtures.DEFAULT_CARD,
setupFutureUsage = null,
)

assertThat(result.shipping).isEqualTo(shippingDetails)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.stripe.android.customersheet.CustomerAdapter.PaymentOption.Companion.
import com.stripe.android.customersheet.analytics.CustomerSheetEventReporter
import com.stripe.android.customersheet.injection.CustomerSheetViewModelScope
import com.stripe.android.customersheet.util.isUnverifiedUSBankAccount
import com.stripe.android.model.ConfirmPaymentIntentParams
import com.stripe.android.model.ConfirmSetupIntentParams
import com.stripe.android.model.ConfirmStripeIntentParams
import com.stripe.android.model.PaymentMethod
Expand Down Expand Up @@ -816,7 +815,7 @@ internal class CustomerSheetViewModel @Inject constructor(
),
paymentMethod = paymentMethod,
shippingValues = null,
setupForFutureUsage = ConfirmPaymentIntentParams.SetupFutureUsage.OffSession,
customerRequestedSave = true,
)

unconfirmedPaymentMethod = paymentMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.stripe.android.core.injection.PUBLISHABLE_KEY
import com.stripe.android.core.injection.STRIPE_ACCOUNT_ID
import com.stripe.android.core.networking.ApiRequest
import com.stripe.android.model.ConfirmPaymentIntentParams
import com.stripe.android.model.ConfirmPaymentIntentParams.SetupFutureUsage.OffSession
import com.stripe.android.model.ConfirmStripeIntentParams
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
Expand Down Expand Up @@ -64,14 +63,14 @@ internal interface IntentConfirmationInterceptor {
paymentMethodCreateParams: PaymentMethodCreateParams,
paymentMethodOptionsParams: PaymentMethodOptionsParams? = null,
shippingValues: ConfirmPaymentIntentParams.Shipping?,
setupForFutureUsage: ConfirmPaymentIntentParams.SetupFutureUsage?,
customerRequestedSave: Boolean,
): NextStep

suspend fun intercept(
initializationMode: PaymentSheet.InitializationMode,
paymentMethod: PaymentMethod,
shippingValues: ConfirmPaymentIntentParams.Shipping?,
setupForFutureUsage: ConfirmPaymentIntentParams.SetupFutureUsage?,
customerRequestedSave: Boolean,
): NextStep

companion object {
Expand Down Expand Up @@ -109,15 +108,15 @@ internal class DefaultIntentConfirmationInterceptor @Inject constructor(
paymentMethodCreateParams: PaymentMethodCreateParams,
paymentMethodOptionsParams: PaymentMethodOptionsParams?,
shippingValues: ConfirmPaymentIntentParams.Shipping?,
setupForFutureUsage: ConfirmPaymentIntentParams.SetupFutureUsage?,
customerRequestedSave: Boolean,
): NextStep {
return when (initializationMode) {
is PaymentSheet.InitializationMode.DeferredIntent -> {
handleDeferredIntent(
intentConfiguration = initializationMode.intentConfiguration,
shippingValues = shippingValues,
paymentMethodCreateParams = paymentMethodCreateParams,
setupForFutureUsage = setupForFutureUsage,
customerRequestedSave = customerRequestedSave,
)
}

Expand All @@ -127,7 +126,6 @@ internal class DefaultIntentConfirmationInterceptor @Inject constructor(
shippingValues = shippingValues,
paymentMethodCreateParams = paymentMethodCreateParams,
paymentMethodOptionsParams = paymentMethodOptionsParams,
setupForFutureUsage = setupForFutureUsage,
)
}

Expand All @@ -136,7 +134,6 @@ internal class DefaultIntentConfirmationInterceptor @Inject constructor(
clientSecret = initializationMode.clientSecret,
shippingValues = shippingValues,
paymentMethodCreateParams = paymentMethodCreateParams,
setupForFutureUsage = setupForFutureUsage,
)
}
}
Expand All @@ -146,15 +143,15 @@ internal class DefaultIntentConfirmationInterceptor @Inject constructor(
initializationMode: PaymentSheet.InitializationMode,
paymentMethod: PaymentMethod,
shippingValues: ConfirmPaymentIntentParams.Shipping?,
setupForFutureUsage: ConfirmPaymentIntentParams.SetupFutureUsage?,
customerRequestedSave: Boolean,
): NextStep {
return when (initializationMode) {
is PaymentSheet.InitializationMode.DeferredIntent -> {
handleDeferredIntent(
intentConfiguration = initializationMode.intentConfiguration,
paymentMethod = paymentMethod,
shippingValues = shippingValues,
setupForFutureUsage = setupForFutureUsage,
customerRequestedSave = customerRequestedSave,
)
}

Expand Down Expand Up @@ -182,7 +179,7 @@ internal class DefaultIntentConfirmationInterceptor @Inject constructor(
intentConfiguration: PaymentSheet.IntentConfiguration,
paymentMethodCreateParams: PaymentMethodCreateParams,
shippingValues: ConfirmPaymentIntentParams.Shipping?,
setupForFutureUsage: ConfirmPaymentIntentParams.SetupFutureUsage?,
customerRequestedSave: Boolean,
): NextStep {
val productUsage = buildSet {
addAll(paymentMethodCreateParams.attribution)
Expand All @@ -201,7 +198,7 @@ internal class DefaultIntentConfirmationInterceptor @Inject constructor(
intentConfiguration = intentConfiguration,
paymentMethod = paymentMethod,
shippingValues = shippingValues,
setupForFutureUsage = setupForFutureUsage,
customerRequestedSave = customerRequestedSave,
)
},
onFailure = { error ->
Expand All @@ -217,15 +214,15 @@ internal class DefaultIntentConfirmationInterceptor @Inject constructor(
intentConfiguration: PaymentSheet.IntentConfiguration,
paymentMethod: PaymentMethod,
shippingValues: ConfirmPaymentIntentParams.Shipping?,
setupForFutureUsage: ConfirmPaymentIntentParams.SetupFutureUsage?,
customerRequestedSave: Boolean,
): NextStep {
return when (val callback = IntentConfirmationInterceptor.createIntentCallback) {
is CreateIntentCallback -> {
handleDeferredIntentCreationFromPaymentMethod(
createIntentCallback = callback,
intentConfiguration = intentConfiguration,
paymentMethod = paymentMethod,
shouldSavePaymentMethod = setupForFutureUsage == OffSession,
shouldSavePaymentMethod = customerRequestedSave,
shippingValues = shippingValues,
)
}
Expand Down Expand Up @@ -336,7 +333,6 @@ internal class DefaultIntentConfirmationInterceptor @Inject constructor(
shippingValues: ConfirmPaymentIntentParams.Shipping?,
paymentMethodCreateParams: PaymentMethodCreateParams,
paymentMethodOptionsParams: PaymentMethodOptionsParams? = null,
setupForFutureUsage: ConfirmPaymentIntentParams.SetupFutureUsage?,
): NextStep.Confirm {
val paramsFactory = ConfirmStripeIntentParamsFactory.createFactory(
clientSecret = clientSecret,
Expand All @@ -345,7 +341,6 @@ internal class DefaultIntentConfirmationInterceptor @Inject constructor(
val confirmParams = paramsFactory.create(
paymentMethodCreateParams,
paymentMethodOptionsParams,
setupForFutureUsage
)

return NextStep.Confirm(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stripe.android.paymentsheet

import com.stripe.android.model.ConfirmPaymentIntentParams
import com.stripe.android.model.ConfirmPaymentIntentParams.SetupFutureUsage
import com.stripe.android.paymentsheet.model.PaymentSelection
import com.stripe.android.paymentsheet.model.PaymentSelection.CustomerRequestedSave

Expand All @@ -12,26 +11,20 @@ internal suspend fun IntentConfirmationInterceptor.intercept(
): IntentConfirmationInterceptor.NextStep {
return when (paymentSelection) {
is PaymentSelection.New -> {
val setupFutureUsage = when (paymentSelection.customerRequestedSave) {
CustomerRequestedSave.RequestReuse -> SetupFutureUsage.OffSession
CustomerRequestedSave.RequestNoReuse -> SetupFutureUsage.Blank
CustomerRequestedSave.NoRequest -> null
}

intercept(
initializationMode = initializationMode,
paymentMethodOptionsParams = paymentSelection.paymentMethodOptionsParams,
paymentMethodCreateParams = paymentSelection.paymentMethodCreateParams,
shippingValues = shippingValues,
setupForFutureUsage = setupFutureUsage,
customerRequestedSave = paymentSelection.customerRequestedSave == CustomerRequestedSave.RequestReuse,
)
}
is PaymentSelection.Saved -> {
intercept(
initializationMode = initializationMode,
paymentMethod = paymentSelection.paymentMethod,
shippingValues = shippingValues,
setupForFutureUsage = null,
customerRequestedSave = false,
)
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stripe.android.paymentsheet.model

import com.stripe.android.ConfirmStripeIntentParamsFactory
import com.stripe.android.model.ConfirmPaymentIntentParams
import com.stripe.android.model.ConfirmStripeIntentParams

internal fun <T : ConfirmStripeIntentParams> ConfirmStripeIntentParamsFactory<T>.create(
Expand All @@ -13,20 +12,8 @@ internal fun <T : ConfirmStripeIntentParams> ConfirmStripeIntentParamsFactory<T>
internal fun <T : ConfirmStripeIntentParams> ConfirmStripeIntentParamsFactory<T>.create(
paymentSelection: PaymentSelection.New,
): T {
val setupFutureUsage = when (paymentSelection.customerRequestedSave) {
PaymentSelection.CustomerRequestedSave.RequestReuse -> {
ConfirmPaymentIntentParams.SetupFutureUsage.OffSession
}
PaymentSelection.CustomerRequestedSave.RequestNoReuse -> {
ConfirmPaymentIntentParams.SetupFutureUsage.Blank
}
PaymentSelection.CustomerRequestedSave.NoRequest -> {
null
}
}

return create(
createParams = paymentSelection.paymentMethodCreateParams,
setupFutureUsage = setupFutureUsage,
optionsParams = paymentSelection.paymentMethodOptionsParams,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.annotation.DrawableRes
import com.stripe.android.link.LinkPaymentDetails
import com.stripe.android.model.Address
import com.stripe.android.model.CardBrand
import com.stripe.android.model.ConfirmPaymentIntentParams
import com.stripe.android.model.ConsumerPaymentDetails
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethod.Type.USBankAccount
Expand Down Expand Up @@ -107,10 +108,10 @@ internal sealed class PaymentSelection : Parcelable {
}
}

enum class CustomerRequestedSave {
RequestReuse,
RequestNoReuse,
NoRequest
enum class CustomerRequestedSave(val setupFutureUsage: ConfirmPaymentIntentParams.SetupFutureUsage?) {
RequestReuse(ConfirmPaymentIntentParams.SetupFutureUsage.OffSession),
RequestNoReuse(ConfirmPaymentIntentParams.SetupFutureUsage.Blank),
NoRequest(null)
}

sealed class New : PaymentSelection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.stripe.android.financialconnections.model.FinancialConnectionsAccount
import com.stripe.android.model.Address
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.model.PaymentMethodOptionsParams
import com.stripe.android.payments.bankaccount.CollectBankAccountConfiguration
import com.stripe.android.payments.bankaccount.CollectBankAccountLauncher
import com.stripe.android.payments.bankaccount.navigation.CollectBankAccountResultInternal
Expand Down Expand Up @@ -468,6 +469,10 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
bankName: String,
linkAccountId: String,
): PaymentSelection.New.USBankAccount {
val customerRequestedSave = customerRequestedSave(
showCheckbox = args.formArgs.showCheckbox,
saveForFutureUse = saveForFutureUse.value
)
return PaymentSelection.New.USBankAccount(
labelResource = application.getString(
R.string.stripe_paymentsheet_payment_method_item_card_number,
Expand All @@ -487,16 +492,10 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
address = address.value,
)
),
paymentMethodOptionsParams = null,
customerRequestedSave = if (args.formArgs.showCheckbox) {
if (saveForFutureUse.value) {
PaymentSelection.CustomerRequestedSave.RequestReuse
} else {
PaymentSelection.CustomerRequestedSave.RequestNoReuse
}
} else {
PaymentSelection.CustomerRequestedSave.NoRequest
},
paymentMethodOptionsParams = PaymentMethodOptionsParams.USBankAccount(
setupFutureUsage = customerRequestedSave.setupFutureUsage
),
customerRequestedSave = customerRequestedSave,
screenState = currentScreenState.value,
input = PaymentSelection.New.USBankAccount.Input(
name = name.value,
Expand Down Expand Up @@ -596,3 +595,18 @@ internal fun PaymentSheet.Address.asAddressModel() =
country = country,
postalCode = postalCode,
)

internal fun customerRequestedSave(
showCheckbox: Boolean,
saveForFutureUse: Boolean
): PaymentSelection.CustomerRequestedSave {
return if (showCheckbox) {
if (saveForFutureUse) {
PaymentSelection.CustomerRequestedSave.RequestReuse
} else {
PaymentSelection.CustomerRequestedSave.RequestNoReuse
}
} else {
PaymentSelection.CustomerRequestedSave.NoRequest
}
}
Loading

0 comments on commit b614347

Please sign in to comment.