Skip to content

Commit

Permalink
Default to google pay if configured. (#9868)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe authored Jan 9, 2025
1 parent 1b50c6d commit 950041e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -534,7 +538,8 @@ internal class DefaultPaymentElementLoader @Inject constructor(

private suspend fun retrieveInitialPaymentSelection(
savedSelection: Deferred<SavedSelection>,
customer: Deferred<CustomerState?>
customer: Deferred<CustomerState?>,
isGooglePayReady: Boolean,
): PaymentSelection? {
return when (val selection = savedSelection.await()) {
is SavedSelection.GooglePay -> PaymentSelection.GooglePay
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down

0 comments on commit 950041e

Please sign in to comment.