Skip to content

Commit

Permalink
Pairing with skyler
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswoo-stripe committed Jul 6, 2022
1 parent 616f488 commit 9509ff4
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 60 deletions.
8 changes: 3 additions & 5 deletions paymentsheet/api/paymentsheet.api
Expand Up @@ -266,18 +266,16 @@ public final class com/stripe/android/paymentsheet/PaymentSheet$Configuration :
public fun <init> (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 <init> (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 <init> (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 <init> (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 <init> (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 <init> (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;
public final fun component4 ()Landroid/content/res/ColorStateList;
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
Expand Down
Expand Up @@ -131,7 +131,7 @@ class AutocompleteScreenTest {
) =
composeTestRule.setContent {
DefaultPaymentsTheme {
AutocompleteTextField(
AutocompleteScreenUI(
viewModel = AutocompleteViewModel(
args,
AddressElementNavigator(),
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Expand Up @@ -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 = "")
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -46,7 +45,7 @@ internal class AutocompleteViewModel @Inject constructor(
get() = _loading

@VisibleForTesting
val addressResult = MutableStateFlow<Result<Address?>?>(null)
val addressResult = MutableStateFlow<Result<ShippingAddress?>?>(null)

val textFieldController = SimpleTextFieldController(
SimpleTextFieldConfig(
Expand All @@ -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()
Expand Down Expand Up @@ -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()
},
Expand Down
Expand Up @@ -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
Expand All @@ -25,13 +24,13 @@ internal class InputAddressViewModel @Inject constructor(

init {
viewModelScope.launch {
navigator.getResultFlow<Address?>(ShippingAddress.KEY)?.collect {
navigator.getResultFlow<ShippingAddress?>(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,
Expand Down
Expand Up @@ -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? {
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -73,7 +72,7 @@ class AutocompleteViewModelTest {
)
)
val expectedResult = Result.success(
Address(
ShippingAddress(
city = null,
country = null,
line1 = "",
Expand Down Expand Up @@ -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 = "",
Expand Down
@@ -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
Expand Down Expand Up @@ -30,20 +29,20 @@ class InputAddressViewModelTest {

@Test
fun `no autocomplete address passed has an empty address to start`() = runTest {
val flow = MutableStateFlow<Address?>(null)
whenever(navigator.getResultFlow<Address?>(any())).thenReturn(flow)
val flow = MutableStateFlow<ShippingAddress?>(null)
whenever(navigator.getResultFlow<ShippingAddress?>(any())).thenReturn(flow)

val viewModel = createViewModel()
assertThat(viewModel.collectedAddress.value).isEqualTo(ShippingAddress())
}

@Test
fun `autocomplete address passed is collected to start`() = runTest {
val expectedAddress = Address(city = "Seattle")
val flow = MutableStateFlow<Address?>(expectedAddress)
whenever(navigator.getResultFlow<Address?>(any())).thenReturn(flow)
val expectedAddress = ShippingAddress(name = "skyler", company = "stripe")
val flow = MutableStateFlow<ShippingAddress?>(expectedAddress)
whenever(navigator.getResultFlow<ShippingAddress?>(any())).thenReturn(flow)

val viewModel = createViewModel()
assertThat(viewModel.collectedAddress.value).isEqualTo(ShippingAddress(city = "Seattle"))
assertThat(viewModel.collectedAddress.value).isEqualTo(expectedAddress)
}
}

0 comments on commit 9509ff4

Please sign in to comment.