From 9509ff42e871bdebfbef77084e15e91ad750969e Mon Sep 17 00:00:00 2001 From: jameswoo-stripe <99316447+jameswoo-stripe@users.noreply.github.com> Date: Wed, 6 Jul 2022 11:37:56 -0700 Subject: [PATCH] Pairing with skyler --- paymentsheet/api/paymentsheet.api | 8 ++-- .../addresselement/AutocompleteScreenTest.kt | 2 +- .../android/paymentsheet/PaymentSheet.kt | 9 +--- .../addresselement/AutocompleteScreen.kt | 4 +- .../addresselement/AutocompleteViewModel.kt | 21 +++++--- .../addresselement/InputAddressViewModel.kt | 9 ++-- .../flowcontroller/DefaultFlowController.kt | 48 ++++++++++--------- .../AutocompleteViewModelTest.kt | 5 +- .../InputAddressViewModelTest.kt | 13 +++-- 9 files changed, 59 insertions(+), 60 deletions(-) diff --git a/paymentsheet/api/paymentsheet.api b/paymentsheet/api/paymentsheet.api index 51ffa7952e2..072dddb49a3 100644 --- a/paymentsheet/api/paymentsheet.api +++ b/paymentsheet/api/paymentsheet.api @@ -266,8 +266,7 @@ public final class com/stripe/android/paymentsheet/PaymentSheet$Configuration : public fun (Ljava/lang/String;Lcom/stripe/android/paymentsheet/PaymentSheet$CustomerConfiguration;Lcom/stripe/android/paymentsheet/PaymentSheet$GooglePayConfiguration;Landroid/content/res/ColorStateList;Lcom/stripe/android/paymentsheet/PaymentSheet$BillingDetails;)V public fun (Ljava/lang/String;Lcom/stripe/android/paymentsheet/PaymentSheet$CustomerConfiguration;Lcom/stripe/android/paymentsheet/PaymentSheet$GooglePayConfiguration;Landroid/content/res/ColorStateList;Lcom/stripe/android/paymentsheet/PaymentSheet$BillingDetails;Z)V public fun (Ljava/lang/String;Lcom/stripe/android/paymentsheet/PaymentSheet$CustomerConfiguration;Lcom/stripe/android/paymentsheet/PaymentSheet$GooglePayConfiguration;Landroid/content/res/ColorStateList;Lcom/stripe/android/paymentsheet/PaymentSheet$BillingDetails;ZLcom/stripe/android/paymentsheet/PaymentSheet$Appearance;)V - public fun (Ljava/lang/String;Lcom/stripe/android/paymentsheet/PaymentSheet$CustomerConfiguration;Lcom/stripe/android/paymentsheet/PaymentSheet$GooglePayConfiguration;Landroid/content/res/ColorStateList;Lcom/stripe/android/paymentsheet/PaymentSheet$BillingDetails;ZLcom/stripe/android/paymentsheet/PaymentSheet$Appearance;Ljava/lang/String;)V - public synthetic fun (Ljava/lang/String;Lcom/stripe/android/paymentsheet/PaymentSheet$CustomerConfiguration;Lcom/stripe/android/paymentsheet/PaymentSheet$GooglePayConfiguration;Landroid/content/res/ColorStateList;Lcom/stripe/android/paymentsheet/PaymentSheet$BillingDetails;ZLcom/stripe/android/paymentsheet/PaymentSheet$Appearance;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public synthetic fun (Ljava/lang/String;Lcom/stripe/android/paymentsheet/PaymentSheet$CustomerConfiguration;Lcom/stripe/android/paymentsheet/PaymentSheet$GooglePayConfiguration;Landroid/content/res/ColorStateList;Lcom/stripe/android/paymentsheet/PaymentSheet$BillingDetails;ZLcom/stripe/android/paymentsheet/PaymentSheet$Appearance;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun component1 ()Ljava/lang/String; public final fun component2 ()Lcom/stripe/android/paymentsheet/PaymentSheet$CustomerConfiguration; public final fun component3 ()Lcom/stripe/android/paymentsheet/PaymentSheet$GooglePayConfiguration; @@ -275,9 +274,8 @@ public final class com/stripe/android/paymentsheet/PaymentSheet$Configuration : public final fun component5 ()Lcom/stripe/android/paymentsheet/PaymentSheet$BillingDetails; public final fun component6 ()Z public final fun component7 ()Lcom/stripe/android/paymentsheet/PaymentSheet$Appearance; - public final fun component8 ()Ljava/lang/String; - public final fun copy (Ljava/lang/String;Lcom/stripe/android/paymentsheet/PaymentSheet$CustomerConfiguration;Lcom/stripe/android/paymentsheet/PaymentSheet$GooglePayConfiguration;Landroid/content/res/ColorStateList;Lcom/stripe/android/paymentsheet/PaymentSheet$BillingDetails;ZLcom/stripe/android/paymentsheet/PaymentSheet$Appearance;Ljava/lang/String;)Lcom/stripe/android/paymentsheet/PaymentSheet$Configuration; - public static synthetic fun copy$default (Lcom/stripe/android/paymentsheet/PaymentSheet$Configuration;Ljava/lang/String;Lcom/stripe/android/paymentsheet/PaymentSheet$CustomerConfiguration;Lcom/stripe/android/paymentsheet/PaymentSheet$GooglePayConfiguration;Landroid/content/res/ColorStateList;Lcom/stripe/android/paymentsheet/PaymentSheet$BillingDetails;ZLcom/stripe/android/paymentsheet/PaymentSheet$Appearance;Ljava/lang/String;ILjava/lang/Object;)Lcom/stripe/android/paymentsheet/PaymentSheet$Configuration; + public final fun copy (Ljava/lang/String;Lcom/stripe/android/paymentsheet/PaymentSheet$CustomerConfiguration;Lcom/stripe/android/paymentsheet/PaymentSheet$GooglePayConfiguration;Landroid/content/res/ColorStateList;Lcom/stripe/android/paymentsheet/PaymentSheet$BillingDetails;ZLcom/stripe/android/paymentsheet/PaymentSheet$Appearance;)Lcom/stripe/android/paymentsheet/PaymentSheet$Configuration; + public static synthetic fun copy$default (Lcom/stripe/android/paymentsheet/PaymentSheet$Configuration;Ljava/lang/String;Lcom/stripe/android/paymentsheet/PaymentSheet$CustomerConfiguration;Lcom/stripe/android/paymentsheet/PaymentSheet$GooglePayConfiguration;Landroid/content/res/ColorStateList;Lcom/stripe/android/paymentsheet/PaymentSheet$BillingDetails;ZLcom/stripe/android/paymentsheet/PaymentSheet$Appearance;ILjava/lang/Object;)Lcom/stripe/android/paymentsheet/PaymentSheet$Configuration; public fun describeContents ()I public fun equals (Ljava/lang/Object;)Z public final fun getAllowsDelayedPaymentMethods ()Z diff --git a/paymentsheet/src/androidTest/java/com/stripe/android/paymentsheet/addresselement/AutocompleteScreenTest.kt b/paymentsheet/src/androidTest/java/com/stripe/android/paymentsheet/addresselement/AutocompleteScreenTest.kt index 9e9196f7706..98ca258ef08 100644 --- a/paymentsheet/src/androidTest/java/com/stripe/android/paymentsheet/addresselement/AutocompleteScreenTest.kt +++ b/paymentsheet/src/androidTest/java/com/stripe/android/paymentsheet/addresselement/AutocompleteScreenTest.kt @@ -131,7 +131,7 @@ class AutocompleteScreenTest { ) = composeTestRule.setContent { DefaultPaymentsTheme { - AutocompleteTextField( + AutocompleteScreenUI( viewModel = AutocompleteViewModel( args, AddressElementNavigator(), diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/PaymentSheet.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/PaymentSheet.kt index 3cf0e5f75e0..ba8ec99d2f0 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/PaymentSheet.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/PaymentSheet.kt @@ -6,7 +6,6 @@ import android.os.Parcelable import androidx.activity.ComponentActivity import androidx.annotation.ColorInt import androidx.annotation.FontRes -import androidx.annotation.RestrictTo import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.toArgb import androidx.fragment.app.Fragment @@ -142,13 +141,7 @@ class PaymentSheet internal constructor( /** * Describes the appearance of Payment Sheet. */ - val appearance: Appearance = Appearance(), - - /** - * Google Places API key used for autocomplete addresses. - */ - @get:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) - val googlePlacesApiKey: String? = null + val appearance: Appearance = Appearance() ) : Parcelable { /** * [Configuration] builder for cleaner object creation from Java. diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/AutocompleteScreen.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/AutocompleteScreen.kt index 4853f4cd623..2cd243af00c 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/AutocompleteScreen.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/AutocompleteScreen.kt @@ -60,11 +60,11 @@ internal fun AutocompleteScreen(injector: NonFallbackInjector) { it.initialize() } - AutocompleteTextField(viewModel = viewModel) + AutocompleteScreenUI(viewModel = viewModel) } @Composable -internal fun AutocompleteTextField(viewModel: AutocompleteViewModel) { +internal fun AutocompleteScreenUI(viewModel: AutocompleteViewModel) { val predictions by viewModel.predictions.collectAsState() val loading by viewModel.loading.collectAsState(initial = false) val query = viewModel.textFieldController.fieldValue.collectAsState(initial = "") diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/AutocompleteViewModel.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/AutocompleteViewModel.kt index 46f64d24526..f6d5fcab1cf 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/AutocompleteViewModel.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/AutocompleteViewModel.kt @@ -6,7 +6,6 @@ import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope -import com.stripe.android.model.Address import com.stripe.android.paymentsheet.R import com.stripe.android.ui.core.injection.NonFallbackInjectable import com.stripe.android.paymentsheet.injection.AutoCompleteViewModelSubcomponent @@ -46,7 +45,7 @@ internal class AutocompleteViewModel @Inject constructor( get() = _loading @VisibleForTesting - val addressResult = MutableStateFlow?>(null) + val addressResult = MutableStateFlow?>(null) val textFieldController = SimpleTextFieldController( SimpleTextFieldConfig( @@ -69,9 +68,11 @@ internal class AutocompleteViewModel @Inject constructor( fun initialize( clientProvider: () -> PlacesClientProxy? = { - args.config?.googlePlacesApiKey?.let { - PlacesClientProxy.create(getApplication(), it) - } + // TODO: Update the PaymentSheet Configuration to include api key +// args.config?.googlePlacesApiKey?.let { +// PlacesClientProxy.create(getApplication(), it) +// } + PlacesClientProxy.create(getApplication(), "AIzaSyBajorPDiua9tcfEqa1HXFl74b-a6bWnXM") } ) { client = clientProvider() @@ -107,8 +108,16 @@ internal class AutocompleteViewModel @Inject constructor( )?.fold( onSuccess = { _loading.value = false + val address = it.place.transformGoogleToStripeAddress(getApplication()) addressResult.value = Result.success( - it.place.transformGoogleToStripeAddress(getApplication()) + ShippingAddress( + city = address.city, + country = address.country, + line1 = address.line1, + line2 = address.line2, + postalCode = address.postalCode, + state = address.state + ) ) setResultAndGoBack() }, diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/InputAddressViewModel.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/InputAddressViewModel.kt index 38c80668659..a38ca2d376c 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/InputAddressViewModel.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/addresselement/InputAddressViewModel.kt @@ -3,7 +3,6 @@ package com.stripe.android.paymentsheet.addresselement import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope -import com.stripe.android.model.Address import com.stripe.android.ui.core.injection.NonFallbackInjectable import com.stripe.android.paymentsheet.injection.InputAddressViewModelSubcomponent import com.stripe.android.ui.core.injection.FormControllerSubcomponent @@ -25,13 +24,13 @@ internal class InputAddressViewModel @Inject constructor( init { viewModelScope.launch { - navigator.getResultFlow(ShippingAddress.KEY)?.collect { + navigator.getResultFlow(ShippingAddress.KEY)?.collect { val oldShippingAddress = _collectedAddress.value _collectedAddress.emit( ShippingAddress( - name = oldShippingAddress?.name, - company = oldShippingAddress?.company, - phoneNumber = oldShippingAddress?.phoneNumber, + name = oldShippingAddress?.name ?: it?.name, + company = oldShippingAddress?.company ?: it?.company, + phoneNumber = oldShippingAddress?.phoneNumber ?: it?.phoneNumber, city = it?.city, country = it?.country, line1 = it?.line1, diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt index 28de50ae9cb..2ec06055c1e 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/flowcontroller/DefaultFlowController.kt @@ -227,29 +227,31 @@ internal class DefaultFlowController @Inject internal constructor( } override fun presentPaymentOptions() { - val initData = runCatching { - viewModel.initData - }.getOrElse { - error( - "FlowController must be successfully initialized using " + - "configureWithPaymentIntent() or configureWithSetupIntent() " + - "before calling presentPaymentOptions()" - ) - } - - paymentOptionActivityLauncher.launch( - PaymentOptionContract.Args( - stripeIntent = initData.stripeIntent, - paymentMethods = initData.paymentMethods, - config = initData.config, - isGooglePayReady = initData.isGooglePayReady, - newLpm = viewModel.paymentSelection as? PaymentSelection.New, - statusBarColor = statusBarColor(), - injectorKey = injectorKey, - enableLogging = enableLogging, - productUsage = productUsage - ) - ) +// val initData = runCatching { +// viewModel.initData +// }.getOrElse { +// error( +// "FlowController must be successfully initialized using " + +// "configureWithPaymentIntent() or configureWithSetupIntent() " + +// "before calling presentPaymentOptions()" +// ) +// } +// +// paymentOptionActivityLauncher.launch( +// PaymentOptionContract.Args( +// stripeIntent = initData.stripeIntent, +// paymentMethods = initData.paymentMethods, +// config = initData.config, +// isGooglePayReady = initData.isGooglePayReady, +// newLpm = viewModel.paymentSelection as? PaymentSelection.New, +// statusBarColor = statusBarColor(), +// injectorKey = injectorKey, +// enableLogging = enableLogging, +// productUsage = productUsage +// ) +// ) + + presentShippingAddress() } private fun getShippingAddress(): ShippingAddress? { diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/addresselement/AutocompleteViewModelTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/addresselement/AutocompleteViewModelTest.kt index 0fff29ecae9..cf6ad5620b9 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/addresselement/AutocompleteViewModelTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/addresselement/AutocompleteViewModelTest.kt @@ -5,7 +5,6 @@ import android.text.SpannableString import androidx.lifecycle.viewModelScope import androidx.test.core.app.ApplicationProvider import com.google.common.truth.Truth.assertThat -import com.stripe.android.model.Address import com.stripe.android.ui.core.elements.TextFieldIcon import com.stripe.android.ui.core.elements.autocomplete.PlacesClientProxy import com.stripe.android.ui.core.elements.autocomplete.model.AutocompletePrediction @@ -73,7 +72,7 @@ class AutocompleteViewModelTest { ) ) val expectedResult = Result.success( - Address( + ShippingAddress( city = null, country = null, line1 = "", @@ -130,7 +129,7 @@ class AutocompleteViewModelTest { fun `onEnterAddressManually sets the current address and navigates back`() = runTest(UnconfinedTestDispatcher()) { val viewModel = createViewModel() val expectedResult = Result.success( - Address( + ShippingAddress( city = "city", country = null, line1 = "", diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/addresselement/InputAddressViewModelTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/addresselement/InputAddressViewModelTest.kt index c24f894a18d..e6949ea2e96 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/addresselement/InputAddressViewModelTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/addresselement/InputAddressViewModelTest.kt @@ -1,7 +1,6 @@ package com.stripe.android.paymentsheet.addresselement import com.google.common.truth.Truth.assertThat -import com.stripe.android.model.Address import com.stripe.android.ui.core.injection.FormControllerSubcomponent import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableStateFlow @@ -30,8 +29,8 @@ class InputAddressViewModelTest { @Test fun `no autocomplete address passed has an empty address to start`() = runTest { - val flow = MutableStateFlow(null) - whenever(navigator.getResultFlow(any())).thenReturn(flow) + val flow = MutableStateFlow(null) + whenever(navigator.getResultFlow(any())).thenReturn(flow) val viewModel = createViewModel() assertThat(viewModel.collectedAddress.value).isEqualTo(ShippingAddress()) @@ -39,11 +38,11 @@ class InputAddressViewModelTest { @Test fun `autocomplete address passed is collected to start`() = runTest { - val expectedAddress = Address(city = "Seattle") - val flow = MutableStateFlow(expectedAddress) - whenever(navigator.getResultFlow(any())).thenReturn(flow) + val expectedAddress = ShippingAddress(name = "skyler", company = "stripe") + val flow = MutableStateFlow(expectedAddress) + whenever(navigator.getResultFlow(any())).thenReturn(flow) val viewModel = createViewModel() - assertThat(viewModel.collectedAddress.value).isEqualTo(ShippingAddress(city = "Seattle")) + assertThat(viewModel.collectedAddress.value).isEqualTo(expectedAddress) } }