Skip to content

Commit

Permalink
[MOBILESDK-2681] [MOBILESDK-2682] Default Payment Method Label Featur…
Browse files Browse the repository at this point in the history
…e Flag (#9844)

* Feature flag

* add feature flag check when getting the defaultPaymentMethodId

* use defaultPaymentMethodId directly

* break apart the scenarios for testing

* shorter method names for lint

* Update FeatureFlags.kt
  • Loading branch information
tianzhao-stripe authored Jan 7, 2025
1 parent e954f9b commit ff488bc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ internal class PlaygroundSettings private constructor(
LayoutSettingsDefinition,
CardBrandAcceptanceSettingsDefinition,
FeatureFlagSettingsDefinition(FeatureFlags.instantDebitsIncentives),
FeatureFlagSettingsDefinition(FeatureFlags.enableDefaultPaymentMethods),
)

private val nonUiSettingDefinitions: List<PlaygroundSettingDefinition<*>> = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.stripe.android.paymentsheet.state

import android.os.Parcelable
import com.stripe.android.common.model.CommonConfiguration
import com.stripe.android.core.utils.FeatureFlags
import com.stripe.android.model.ElementsSession
import com.stripe.android.model.PaymentMethod
import com.stripe.android.paymentsheet.PaymentSheet
Expand Down Expand Up @@ -66,7 +67,11 @@ internal data class CustomerState(
// Should always remove duplicates when using `customer_session`
canRemoveDuplicates = true,
),
defaultPaymentMethodId = customer.defaultPaymentMethod
defaultPaymentMethodId = if (FeatureFlags.enableDefaultPaymentMethods.isEnabled) {
customer.defaultPaymentMethod
} else {
null
}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@ package com.stripe.android.paymentsheet.state
import com.google.common.truth.Truth.assertThat
import com.stripe.android.common.model.CommonConfiguration
import com.stripe.android.common.model.asCommonConfiguration
import com.stripe.android.core.utils.FeatureFlags
import com.stripe.android.model.ElementsSession
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodFixtures
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.PaymentSheetFixtures
import com.stripe.android.testing.FeatureFlagTestRule
import com.stripe.android.testing.PaymentMethodFactory
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test

class CustomerStateTest {

@get:Rule
val enableDefaultPaymentMethods = FeatureFlagTestRule(
featureFlag = FeatureFlags.enableDefaultPaymentMethods,
isEnabled = true,
)

@Test
fun `Should create 'CustomerState' for customer session properly with permissions disabled`() {
val paymentMethods = PaymentMethodFactory.cards(4)
Expand Down Expand Up @@ -125,18 +135,17 @@ class CustomerStateTest {
)
}

@Test
fun `Should create 'CustomerState' for customer session properly with nonnull defaultPaymentMethodId`() {
private fun createCustomerSessionForTestingDefaultPaymentMethod(defaultPaymentMethodId: String?): CustomerState {
val customerId = "cus_3"
val ephemeralKeySecret = "ek_3"
val paymentMethods = PaymentMethodFactory.cards(3)

val mobilePaymentElementComponent = ElementsSession.Customer.Components.MobilePaymentElement.Enabled(
isPaymentMethodSaveEnabled = false,
isPaymentMethodRemoveEnabled = false,
canRemoveLastPaymentMethod = false,
allowRedisplayOverride = null,
)
val defaultPaymentMethodId = "aaa111"
val customer = createElementsSessionCustomer(
customerId = customerId,
ephemeralKeySecret = ephemeralKeySecret,
Expand All @@ -152,20 +161,57 @@ class CustomerStateTest {
customerSessionClientSecret = "cuss_123",
)

assertThat(customerState.id).isEqualTo(customerId)
assertThat(customerState.ephemeralKeySecret).isEqualTo(ephemeralKeySecret)
assertThat(customerState.paymentMethods).isEqualTo(paymentMethods)
assertThat(customerState.permissions).isEqualTo(
CustomerState.Permissions(
canRemovePaymentMethods = false,
canRemoveLastPaymentMethod = false,
// Always true for `customer_session`
canRemoveDuplicates = true,
)
return customerState
}

@Test
fun `Create 'CustomerState' for customer session with nonnull defaultPaymentMethodId & flag on`() {
val defaultPaymentMethodId = "aaa111"

enableDefaultPaymentMethods.setEnabled(true)
val customerState = createCustomerSessionForTestingDefaultPaymentMethod(
defaultPaymentMethodId = defaultPaymentMethodId
)

assertThat(customerState.defaultPaymentMethodId).isEqualTo(defaultPaymentMethodId)
}

@Test
fun `Create 'CustomerState' for customer session with nonnull defaultPaymentMethodId & flag off`() {
val defaultPaymentMethodId = "aaa111"

enableDefaultPaymentMethods.setEnabled(false)
val customerState = createCustomerSessionForTestingDefaultPaymentMethod(
defaultPaymentMethodId = defaultPaymentMethodId
)

assertThat(customerState.defaultPaymentMethodId).isNull()
}

@Test
fun `Create 'CustomerState' for customer session with null defaultPaymentMethodId & flag on`() {
val defaultPaymentMethodId = null

enableDefaultPaymentMethods.setEnabled(true)
val customerState = createCustomerSessionForTestingDefaultPaymentMethod(
defaultPaymentMethodId = defaultPaymentMethodId
)

assertThat(customerState.defaultPaymentMethodId).isNull()
}

@Test
fun `Create 'CustomerState' for customer session with null defaultPaymentMethodId & flag off`() {
val defaultPaymentMethodId = null

enableDefaultPaymentMethods.setEnabled(false)
val customerState = createCustomerSessionForTestingDefaultPaymentMethod(
defaultPaymentMethodId = defaultPaymentMethodId
)

assertThat(customerState.defaultPaymentMethodId).isNull()
}

@Test
fun `Should create 'CustomerState' for legacy ephemeral keys properly`() {
val paymentMethods = PaymentMethodFactory.cards(7)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object FeatureFlags {
// Add any feature flags here
val nativeLinkEnabled = FeatureFlag("Native Link")
val instantDebitsIncentives = FeatureFlag("Instant Bank Payments Incentives")
val enableDefaultPaymentMethods = FeatureFlag("Enable Default Payment Methods")
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
Expand Down

0 comments on commit ff488bc

Please sign in to comment.