Skip to content

Commit

Permalink
Remove SetupIntent tests & move CustomerAdapter to own flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
samer-stripe committed May 24, 2024
1 parent 899250d commit 169b31e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ package com.stripe.android.lpm

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.stripe.android.BasePlaygroundTest
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.example.playground.settings.CollectAddressSettingsDefinition
import com.stripe.android.paymentsheet.example.playground.settings.CollectEmailSettingsDefinition
import com.stripe.android.paymentsheet.example.playground.settings.CollectNameSettingsDefinition
import com.stripe.android.paymentsheet.example.playground.settings.CollectPhoneSettingsDefinition
import com.stripe.android.paymentsheet.example.playground.settings.Country
import com.stripe.android.paymentsheet.example.playground.settings.CountrySettingsDefinition
import com.stripe.android.paymentsheet.example.playground.settings.CustomerSettingsDefinition
import com.stripe.android.paymentsheet.example.playground.settings.CustomerSheetPaymentMethodModeDefinition
import com.stripe.android.paymentsheet.example.playground.settings.CustomerType
import com.stripe.android.paymentsheet.example.playground.settings.DefaultBillingAddress
import com.stripe.android.paymentsheet.example.playground.settings.DefaultBillingAddressSettingsDefinition
import com.stripe.android.paymentsheet.example.playground.settings.PaymentMethodMode
import com.stripe.android.test.core.TestParameters
import org.junit.Test
Expand All @@ -35,20 +28,6 @@ internal class TestCardInCustomerSheet : BasePlaygroundTest() {
)
}

@Test
fun testCardWithSetupIntent() {
testDriver.savePaymentMethodInCustomerSheet(
TestParameters.create(paymentMethodCode = "card").copyPlaygroundSettings { settings ->
settings[CustomerSettingsDefinition] = CustomerType.NEW
settings[CountrySettingsDefinition] = Country.US
settings[CustomerSheetPaymentMethodModeDefinition] = PaymentMethodMode.SetupIntent
},
populateCustomLpmFields = {
populateCardDetails()
},
)
}

@Test
fun testCardWithNonUsMerchant() {
testDriver.savePaymentMethodInCustomerSheet(
Expand All @@ -62,70 +41,4 @@ internal class TestCardInCustomerSheet : BasePlaygroundTest() {
},
)
}

@Test
fun testCardWithSetupIntentAndNonUsMerchant() {
testDriver.savePaymentMethodInCustomerSheet(
TestParameters.create(paymentMethodCode = "card").copyPlaygroundSettings { settings ->
settings[CustomerSettingsDefinition] = CustomerType.NEW
settings[CountrySettingsDefinition] = Country.FR
settings[CustomerSheetPaymentMethodModeDefinition] = PaymentMethodMode.SetupIntent
},
populateCustomLpmFields = {
populateCardDetails()
},
)
}

@Test
fun testCardWithBillingDetailsCollection() {
testDriver.savePaymentMethodInCustomerSheet(
TestParameters.create(
paymentMethodCode = "card",
) { settings ->
settings[CustomerSettingsDefinition] = CustomerType.NEW
settings[CountrySettingsDefinition] = Country.US
settings[DefaultBillingAddressSettingsDefinition] = DefaultBillingAddress.Off
settings[CollectNameSettingsDefinition] =
PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Always
settings[CollectEmailSettingsDefinition] =
PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Always
settings[CollectPhoneSettingsDefinition] =
PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Always
settings[CollectAddressSettingsDefinition] =
PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Full
},
populateCustomLpmFields = {
populateCardDetails()
populateEmail()
populateName("Name on card")
populateAddress()
populatePhoneNumber()
},
)
}

@Test
fun testCardWithBillingDetailsCollectionWithDefaults() {
testDriver.savePaymentMethodInCustomerSheet(
TestParameters.create(
paymentMethodCode = "card",
) { settings ->
settings[CustomerSettingsDefinition] = CustomerType.NEW
settings[CountrySettingsDefinition] = Country.US
settings[DefaultBillingAddressSettingsDefinition] = DefaultBillingAddress.On
settings[CollectNameSettingsDefinition] =
PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Always
settings[CollectEmailSettingsDefinition] =
PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Always
settings[CollectPhoneSettingsDefinition] =
PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Always
settings[CollectAddressSettingsDefinition] =
PaymentSheet.BillingDetailsCollectionConfiguration.AddressCollectionMode.Full
},
populateCustomLpmFields = {
populateCardDetails()
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1139,9 +1139,6 @@ internal class PlaygroundTestDriver(

@OptIn(ExperimentalTestApi::class)
private fun waitForManageSavedPaymentMethods() {
Espresso.onIdle()
composeTestRule.waitForIdle()

composeTestRule.waitUntilAtLeastOneExists(
hasText("Manage your payment methods"),
DEFAULT_UI_TIMEOUT.inWholeMilliseconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ internal class PaymentSheetPlaygroundActivity : AppCompatActivity(), ExternalPay
val localPlaygroundSettings = playgroundSettings ?: return@setContent

val playgroundState by viewModel.state.collectAsState()
val customerAdapter by viewModel.customerAdapter.collectAsState()

val customerSheet = playgroundState?.asCustomerState()?.let { customerPlaygroundState ->
rememberCustomerSheet(
configuration = customerPlaygroundState.customerSheetConfiguration(),
customerAdapter = customerPlaygroundState.adapter,
callback = viewModel::onCustomerSheetCallback
)
customerAdapter?.let { adapter ->
rememberCustomerSheet(
configuration = customerPlaygroundState.customerSheetConfiguration(),
customerAdapter = adapter,
callback = viewModel::onCustomerSheetCallback
)
}
}

PlaygroundTheme(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import java.io.IOException

@OptIn(ExperimentalCustomerSheetApi::class)
internal class PaymentSheetPlaygroundViewModel(
application: Application,
launchUri: Uri?,
Expand All @@ -61,6 +62,7 @@ internal class PaymentSheetPlaygroundViewModel(
val state = MutableStateFlow<PlaygroundState?>(null)
val flowControllerState = MutableStateFlow<FlowControllerState?>(null)
val customerSheetState = MutableStateFlow<CustomerSheetState?>(null)
val customerAdapter = MutableStateFlow<CustomerAdapter?>(null)

init {
viewModelScope.launch(Dispatchers.IO) {
Expand All @@ -76,6 +78,7 @@ internal class PaymentSheetPlaygroundViewModel(
state.value = null
flowControllerState.value = null
customerSheetState.value = null
customerAdapter.value = null

if (playgroundSettings.configurationData.value.integrationType.isPaymentFlow()) {
prepareCheckout(playgroundSettings)
Expand Down Expand Up @@ -149,23 +152,23 @@ internal class PaymentSheetPlaygroundViewModel(

snapshot.saveToSharedPreferences(getApplication())

state.value = PlaygroundState.Customer(
snapshot = snapshot,
adapter = CustomerAdapter.create(
context = getApplication(),
customerEphemeralKeyProvider = {
fetchEphemeralKey(snapshot)
},
setupIntentClientSecretProvider = if (
snapshot[CustomerSheetPaymentMethodModeDefinition] == PaymentMethodMode.SetupIntent
) {
{ customerId -> createSetupIntentClientSecret(snapshot, customerId) }
} else {
null
}
)
val adapter = CustomerAdapter.create(
context = getApplication(),
customerEphemeralKeyProvider = {
fetchEphemeralKey(snapshot)
},
setupIntentClientSecretProvider = if (
snapshot[CustomerSheetPaymentMethodModeDefinition] == PaymentMethodMode.SetupIntent
) {
{ customerId -> createSetupIntentClientSecret(snapshot, customerId) }
} else {
null
}
)

customerSheetState.value = CustomerSheetState()
customerAdapter.value = adapter
state.value = PlaygroundState.Customer(snapshot = snapshot)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stripe.android.paymentsheet.example.playground

import androidx.compose.runtime.Stable
import com.stripe.android.customersheet.CustomerAdapter
import com.stripe.android.customersheet.CustomerSheet
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi
import com.stripe.android.paymentsheet.PaymentSheet
Expand Down Expand Up @@ -50,7 +49,6 @@ internal sealed interface PlaygroundState {
@OptIn(ExperimentalCustomerSheetApi::class)
data class Customer(
private val snapshot: PlaygroundSettings.Snapshot,
val adapter: CustomerAdapter,
) : PlaygroundState {
override val integrationType = snapshot.configurationData.integrationType
override val countryCode = snapshot[CountrySettingsDefinition]
Expand Down

0 comments on commit 169b31e

Please sign in to comment.