From 94a1657c222d28806ed838be0cddf65b0fc63542 Mon Sep 17 00:00:00 2001 From: Chen Cen <79880926+ccen-stripe@users.noreply.github.com> Date: Thu, 9 Dec 2021 09:02:47 -0800 Subject: [PATCH] allow non-terminal state for PaymentSheet (#4438) * allow non-terminal state for PaymentSheet * update test name * update error message * update test --- .../model/StripeIntentValidator.kt | 15 ++++++------- .../paymentsheet/PaymentSheetViewModelTest.kt | 3 +-- .../model/StripeIntentValidatorTest.kt | 22 +++++++++++++++++-- 3 files changed, 28 insertions(+), 12 deletions(-) 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 30156955293..0f9ca91f4c6 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 @@ -26,26 +26,25 @@ internal class StripeIntentValidator @Inject constructor() { } stripeIntent is PaymentIntent && ( - (stripeIntent.status != StripeIntent.Status.RequiresPaymentMethod) && - (stripeIntent.status != StripeIntent.Status.RequiresAction) + (stripeIntent.status == StripeIntent.Status.Canceled) || + (stripeIntent.status == StripeIntent.Status.Succeeded) || + (stripeIntent.status == StripeIntent.Status.RequiresCapture) ) -> { error( """ - A PaymentIntent with status='requires_payment_method' or 'requires_action` is required. - The current PaymentIntent has status '${stripeIntent.status}'. + PaymentSheet cannot set up a PaymentIntent in status '${stripeIntent.status}'. See https://stripe.com/docs/api/payment_intents/object#payment_intent_object-status. """.trimIndent() ) } stripeIntent is SetupIntent && ( - (stripeIntent.status != StripeIntent.Status.RequiresPaymentMethod) && - (stripeIntent.status != StripeIntent.Status.RequiresAction) + (stripeIntent.status == StripeIntent.Status.Canceled) || + (stripeIntent.status == StripeIntent.Status.Succeeded) ) -> { error( """ - A SetupIntent with status='requires_payment_method' or 'requires_action` is required. - The current SetupIntent has status '${stripeIntent.status}'. + PaymentSheet cannot set up a SetupIntent in status '${stripeIntent.status}'. See https://stripe.com/docs/api/setup_intents/object#setup_intent_object-status """.trimIndent() ) diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetViewModelTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetViewModelTest.kt index 4a5c38e1ee7..6203e10cc5a 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetViewModelTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/PaymentSheetViewModelTest.kt @@ -638,8 +638,7 @@ internal class PaymentSheetViewModelTest { viewModel.maybeFetchStripeIntent() assertThat((result as? PaymentSheetResult.Failed)?.error?.message) .isEqualTo( - "A PaymentIntent with status='requires_payment_method' or 'requires_action` is required.\n" + - "The current PaymentIntent has status 'succeeded'.\n" + + "PaymentSheet cannot set up a PaymentIntent in status 'succeeded'.\n" + "See https://stripe.com/docs/api/payment_intents/object#payment_intent_object-status." ) } diff --git a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/model/StripeIntentValidatorTest.kt b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/model/StripeIntentValidatorTest.kt index 695700fb87e..79da2dfbb00 100644 --- a/paymentsheet/src/test/java/com/stripe/android/paymentsheet/model/StripeIntentValidatorTest.kt +++ b/paymentsheet/src/test/java/com/stripe/android/paymentsheet/model/StripeIntentValidatorTest.kt @@ -34,11 +34,11 @@ class StripeIntentValidatorTest { } @Test - fun `requireValid() processing is not valid`() { + fun `requireValid() Succeeded is not valid`() { assertFails { validator.requireValid( PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD.copy( - status = StripeIntent.Status.Processing + status = StripeIntent.Status.Succeeded ) ) } @@ -71,4 +71,22 @@ class StripeIntentValidatorTest { SetupIntentFixtures.SI_NEXT_ACTION_REDIRECT ) } + + @Test + fun `PaymentIntent requireValid() allows status = RequiresConfirmation`() { + validator.requireValid( + PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD.copy( + status = StripeIntent.Status.RequiresConfirmation + ) + ) + } + + @Test + fun `PaymentIntent requireValid() allows status = Processing`() { + validator.requireValid( + PaymentIntentFixtures.PI_REQUIRES_PAYMENT_METHOD.copy( + status = StripeIntent.Status.Processing + ) + ) + } }