From 67cfa84f461859c130e97659a2abb7f2adfe3933 Mon Sep 17 00:00:00 2001 From: Till Hellmund Date: Tue, 5 Dec 2023 10:02:58 -0500 Subject: [PATCH] Fix tests and make code tweaks --- .../paymentsheet/PaymentSheetViewModel.kt | 2 +- .../model/StripeIntentValidator.kt | 4 ++-- .../paymentsheet/state/PaymentSheetLoader.kt | 6 +----- .../state/PaymentSheetLoadingException.kt | 19 ++++++++++--------- .../state/DefaultPaymentSheetLoaderTest.kt | 14 +++++++++----- 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/PaymentSheetViewModel.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/PaymentSheetViewModel.kt index 87068c5e885..2194a4b105f 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/PaymentSheetViewModel.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/PaymentSheetViewModel.kt @@ -332,7 +332,7 @@ internal class PaymentSheetViewModel @Inject internal constructor( if (pendingResult is InternalPaymentResult.Completed) { // If we just received a transaction result after process death, we don't error. Instead, we dismiss // PaymentSheet and return a `Completed` result to the caller. - val usedPaymentMethod = error.asPaymentSheetLoadingException.paymentMethod + val usedPaymentMethod = error.asPaymentSheetLoadingException.usedPaymentMethod handlePaymentCompleted(usedPaymentMethod, finishImmediately = true) } else { setStripeIntent(null) diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/model/StripeIntentValidator.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/model/StripeIntentValidator.kt index 124cc6662f3..88fa4654e8f 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/model/StripeIntentValidator.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/model/StripeIntentValidator.kt @@ -27,13 +27,13 @@ internal object StripeIntentValidator { val exception = when { stripeIntent is PaymentIntent && stripeIntent.confirmationMethod != Automatic -> { - PaymentSheetLoadingException.InvalidConfirmationMethod(paymentMethod, stripeIntent.confirmationMethod) + PaymentSheetLoadingException.InvalidConfirmationMethod(stripeIntent.confirmationMethod) } stripeIntent is PaymentIntent && stripeIntent.isInTerminalState -> { PaymentSheetLoadingException.PaymentIntentInTerminalState(paymentMethod, stripeIntent.status) } stripeIntent is PaymentIntent && (stripeIntent.amount == null || stripeIntent.currency == null) -> { - PaymentSheetLoadingException.MissingAmountOrCurrency(paymentMethod) + PaymentSheetLoadingException.MissingAmountOrCurrency } stripeIntent is SetupIntent && stripeIntent.isInTerminalState -> { PaymentSheetLoadingException.SetupIntentInTerminalState(paymentMethod, stripeIntent.status) diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentSheetLoader.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentSheetLoader.kt index 244ad8bfe95..e701b33d620 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentSheetLoader.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentSheetLoader.kt @@ -186,11 +186,7 @@ internal class DefaultPaymentSheetLoader @Inject constructor( val requested = stripeIntent.paymentMethodTypes.joinToString(separator = ", ") val supported = lpmRepository.values().joinToString(separator = ", ") { it.code } - throw PaymentSheetLoadingException.NoPaymentMethodTypesAvailable( - paymentMethod = stripeIntent.paymentMethod, - requested = requested, - supported = supported, - ) + throw PaymentSheetLoadingException.NoPaymentMethodTypesAvailable(requested, supported) } } diff --git a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentSheetLoadingException.kt b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentSheetLoadingException.kt index 1ac3bfabea9..ca569f1dfea 100644 --- a/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentSheetLoadingException.kt +++ b/paymentsheet/src/main/java/com/stripe/android/paymentsheet/state/PaymentSheetLoadingException.kt @@ -10,13 +10,14 @@ import com.stripe.android.paymentsheet.state.PaymentSheetLoadingException.Unknow internal sealed class PaymentSheetLoadingException : Throwable() { abstract val type: String - abstract val paymentMethod: PaymentMethod? + abstract val usedPaymentMethod: PaymentMethod? data class InvalidConfirmationMethod( - override val paymentMethod: PaymentMethod?, private val confirmationMethod: PaymentIntent.ConfirmationMethod, ) : PaymentSheetLoadingException() { + override val usedPaymentMethod: PaymentMethod? = null + override val type: String = "invalidConfirmationMethod" override val message: String = """ @@ -27,11 +28,12 @@ internal sealed class PaymentSheetLoadingException : Throwable() { } data class NoPaymentMethodTypesAvailable( - override val paymentMethod: PaymentMethod?, private val requested: String, private val supported: String, ) : PaymentSheetLoadingException() { + override val usedPaymentMethod: PaymentMethod? = null + override val type: String = "noPaymentMethodTypesAvailable" override val message: String @@ -40,7 +42,7 @@ internal sealed class PaymentSheetLoadingException : Throwable() { } data class PaymentIntentInTerminalState( - override val paymentMethod: PaymentMethod?, + override val usedPaymentMethod: PaymentMethod?, private val status: StripeIntent.Status?, ) : PaymentSheetLoadingException() { @@ -54,7 +56,7 @@ internal sealed class PaymentSheetLoadingException : Throwable() { } data class SetupIntentInTerminalState( - override val paymentMethod: PaymentMethod?, + override val usedPaymentMethod: PaymentMethod?, private val status: StripeIntent.Status?, ) : PaymentSheetLoadingException() { @@ -67,9 +69,8 @@ internal sealed class PaymentSheetLoadingException : Throwable() { """.trimIndent() } - data class MissingAmountOrCurrency( - override val paymentMethod: PaymentMethod?, - ) : PaymentSheetLoadingException() { + object MissingAmountOrCurrency : PaymentSheetLoadingException() { + override val usedPaymentMethod: PaymentMethod? = null override val type: String = "missingAmountOrCurrency" override val message: String = "PaymentIntent must contain amount and currency." } @@ -83,7 +84,7 @@ internal sealed class PaymentSheetLoadingException : Throwable() { override val message: String? = cause.message - override val paymentMethod: PaymentMethod? = null + override val usedPaymentMethod: PaymentMethod? = null } } diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/state/DefaultPaymentSheetLoaderTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/state/DefaultPaymentSheetLoaderTest.kt index 2d297fe6b4b..ae8370ea804 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/state/DefaultPaymentSheetLoaderTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/state/DefaultPaymentSheetLoaderTest.kt @@ -27,6 +27,7 @@ import com.stripe.android.paymentsheet.model.PaymentSelection import com.stripe.android.paymentsheet.repositories.CustomerRepository import com.stripe.android.testing.FeatureFlagTestRule import com.stripe.android.testing.PaymentIntentFactory +import com.stripe.android.testing.PaymentMethodFactory import com.stripe.android.ui.core.forms.resources.LpmRepository import com.stripe.android.utils.FakeCustomerRepository import com.stripe.android.utils.FakeElementsSessionRepository @@ -392,10 +393,13 @@ internal class DefaultPaymentSheetLoaderTest { @Test fun `load() when PaymentIntent has invalid status should return null`() = runTest { + val paymentIntent = PaymentIntentFixtures.PI_SUCCEEDED.copy( + paymentMethod = PaymentMethodFactory.card(), + ) + val paymentMethod = paymentIntent.paymentMethod!! + val result = createPaymentSheetLoader( - stripeIntent = PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD.copy( - status = Succeeded, - ), + stripeIntent = paymentIntent, ).load( initializationMode = PaymentSheet.InitializationMode.PaymentIntent( clientSecret = PaymentSheetFixtures.PAYMENT_INTENT_CLIENT_SECRET.value, @@ -403,7 +407,7 @@ internal class DefaultPaymentSheetLoaderTest { PaymentSheetFixtures.CONFIG_CUSTOMER_WITH_GOOGLEPAY ).exceptionOrNull() - assertThat(result).isEqualTo(PaymentSheetLoadingException.PaymentIntentInTerminalState(Succeeded)) + assertThat(result).isEqualTo(PaymentSheetLoadingException.PaymentIntentInTerminalState(paymentMethod, Succeeded)) } @Test @@ -703,7 +707,7 @@ internal class DefaultPaymentSheetLoaderTest { @Test fun `Emits correct events when loading fails for deferred intent`() = runTest { - val error = PaymentSheetLoadingException.PaymentIntentInTerminalState(status = Canceled) + val error = PaymentSheetLoadingException.PaymentIntentInTerminalState(usedPaymentMethod = null, status = Canceled) val loader = createPaymentSheetLoader(error = error) loader.load(