diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentElementLoader.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentElementLoader.kt index b15f2361857..0374ec9b8ee 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentElementLoader.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentElementLoader.kt @@ -194,7 +194,11 @@ internal class DefaultPaymentElementLoader @Inject constructor( } val initialPaymentSelection = async { - retrieveInitialPaymentSelection(savedSelection, customer) + retrieveInitialPaymentSelection( + savedSelection = savedSelection, + customer = customer, + isGooglePayReady = isGooglePayReady, + ) } val stripeIntent = elementsSession.stripeIntent @@ -534,7 +538,8 @@ internal class DefaultPaymentElementLoader @Inject constructor( private suspend fun retrieveInitialPaymentSelection( savedSelection: Deferred, - customer: Deferred + customer: Deferred, + isGooglePayReady: Boolean, ): PaymentSelection? { return when (val selection = savedSelection.await()) { is SavedSelection.GooglePay -> PaymentSelection.GooglePay @@ -544,6 +549,7 @@ internal class DefaultPaymentElementLoader @Inject constructor( } is SavedSelection.None -> null } ?: customer.await()?.paymentMethods?.firstOrNull()?.toPaymentSelection() + ?: PaymentSelection.GooglePay.takeIf { isGooglePayReady } } private suspend fun retrieveSavedSelection( diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/state/DefaultPaymentElementLoaderTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/state/DefaultPaymentElementLoaderTest.kt index ce4895ea34e..2fc27dd7467 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/state/DefaultPaymentElementLoaderTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/state/DefaultPaymentElementLoaderTest.kt @@ -234,6 +234,72 @@ internal class DefaultPaymentElementLoaderTest { assertThat(result.paymentSelection).isEqualTo(PaymentSelection.GooglePay) } + @Test + fun `Should default to google if customer has no payment methods and no last used payment method`() = + runTest { + prefsRepository.savePaymentSelection(null) + + val loader = createPaymentElementLoader( + stripeIntent = PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD, + isGooglePayReady = true, + customerRepo = FakeCustomerRepository(paymentMethods = emptyList()), + ) + + val result = loader.load( + initializationMode = PaymentElementLoader.InitializationMode.PaymentIntent( + clientSecret = PaymentSheetFixtures.PAYMENT_INTENT_CLIENT_SECRET.value, + ), + paymentSheetConfiguration = PaymentSheetFixtures.CONFIG_CUSTOMER_WITH_GOOGLEPAY, + initializedViaCompose = false, + ).getOrThrow() + + assertThat(result.paymentSelection).isEqualTo(PaymentSelection.GooglePay) + } + + @Test + fun `Should default to no payment method google pay is not ready`() = + runTest { + prefsRepository.savePaymentSelection(null) + + val loader = createPaymentElementLoader( + stripeIntent = PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD, + isGooglePayReady = false, + customerRepo = FakeCustomerRepository(paymentMethods = emptyList()), + ) + + val result = loader.load( + initializationMode = PaymentElementLoader.InitializationMode.PaymentIntent( + clientSecret = PaymentSheetFixtures.PAYMENT_INTENT_CLIENT_SECRET.value, + ), + paymentSheetConfiguration = PaymentSheetFixtures.CONFIG_CUSTOMER_WITH_GOOGLEPAY, + initializedViaCompose = false, + ).getOrThrow() + + assertThat(result.paymentSelection).isNull() + } + + @Test + fun `Should default to no payment method google pay is not configured`() = + runTest { + prefsRepository.savePaymentSelection(null) + + val loader = createPaymentElementLoader( + stripeIntent = PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD, + isGooglePayReady = true, + customerRepo = FakeCustomerRepository(paymentMethods = emptyList()), + ) + + val result = loader.load( + initializationMode = PaymentElementLoader.InitializationMode.PaymentIntent( + clientSecret = PaymentSheetFixtures.PAYMENT_INTENT_CLIENT_SECRET.value, + ), + paymentSheetConfiguration = PaymentSheetFixtures.CONFIG_CUSTOMER, + initializedViaCompose = false, + ).getOrThrow() + + assertThat(result.paymentSelection).isNull() + } + @Test fun `Should default to no payment method if customer has no payment methods and no last used payment method`() = runTest { @@ -243,6 +309,7 @@ internal class DefaultPaymentElementLoaderTest { stripeIntent = PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD_WITHOUT_LINK, isGooglePayReady = true, customerRepo = FakeCustomerRepository(paymentMethods = emptyList()), + isGooglePayEnabledFromBackend = false, ) val result = loader.load(