Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add polling for Cash App Pay flow result processing #6680

Merged
merged 4 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ internal sealed class PaymentFlowResultProcessor<T : StripeIntent, out S : Strip
stripeIntent.paymentMethod?.type == PaymentMethod.Type.Card &&
stripeIntent.nextActionType == StripeIntent.NextActionType.UseStripeSdk

return succeededMaybeRefresh || cancelledMaybeRefresh || actionNotProcessedMaybeRefresh
// For Cash App Pay, the intent status can still be `requires_action` by the time the user
// gets back to the merchant app. We poll until it's succeeded.
val shouldRefreshForCashApp = stripeIntent.requiresAction() &&
stripeIntent.paymentMethod?.type == PaymentMethod.Type.CashAppPay

return succeededMaybeRefresh || cancelledMaybeRefresh ||
actionNotProcessedMaybeRefresh || shouldRefreshForCashApp
}

private fun determineFlowOutcome(intent: StripeIntent, originalFlowOutcome: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.PaymentIntentFixtures
import com.stripe.android.model.StripeIntent
import com.stripe.android.networking.StripeRepository
import com.stripe.android.testing.PaymentMethodFactory
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Test
Expand Down Expand Up @@ -340,6 +341,41 @@ internal class PaymentIntentFlowResultProcessorTest {
assertThat(result).isEqualTo(expectedResult)
}

@Test
fun `Keeps refreshing when encountering a CashAppPay payment that still requires action`() =
runTest(testDispatcher) {
val requiresActionIntent = PaymentIntentFixtures.PI_SUCCEEDED.copy(
status = StripeIntent.Status.RequiresAction,
paymentMethod = PaymentMethodFactory.cashAppPay(),
paymentMethodTypes = listOf("card", "cashapp"),
)

val succeededIntent = requiresActionIntent.copy(status = StripeIntent.Status.Succeeded)

whenever(mockStripeRepository.retrievePaymentIntent(any(), any(), any())).thenReturn(
requiresActionIntent,
requiresActionIntent,
succeededIntent,
)

val clientSecret = requireNotNull(requiresActionIntent.clientSecret)

val result = processor.processResult(
PaymentFlowResult.Unvalidated(
clientSecret = clientSecret,
flowOutcome = StripeIntentResult.Outcome.UNKNOWN,
)
).getOrThrow()

val expectedResult = PaymentIntentResult(
intent = succeededIntent,
outcomeFromFlow = StripeIntentResult.Outcome.SUCCEEDED,
failureMessage = null,
)

assertThat(result).isEqualTo(expectedResult)
}

private suspend fun runCanceledFlow(
initialIntent: PaymentIntent,
refreshedIntent: PaymentIntent = initialIntent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.stripe.android.core.networking.ApiRequest
import com.stripe.android.model.SetupIntentFixtures
import com.stripe.android.model.StripeIntent
import com.stripe.android.networking.StripeRepository
import com.stripe.android.testing.PaymentMethodFactory
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Test
Expand Down Expand Up @@ -174,4 +175,39 @@ internal class SetupIntentFlowResultProcessorTest {
)
)
}

@Test
fun `Keeps refreshing when encountering a CashAppPay setup that still requires action`() =
runTest(testDispatcher) {
val requiresActionIntent = SetupIntentFixtures.SI_SUCCEEDED.copy(
status = StripeIntent.Status.RequiresAction,
paymentMethod = PaymentMethodFactory.cashAppPay(),
paymentMethodTypes = listOf("card", "cashapp"),
)

val succeededIntent = requiresActionIntent.copy(status = StripeIntent.Status.Succeeded)

whenever(mockStripeRepository.retrieveSetupIntent(any(), any(), any())).thenReturn(
requiresActionIntent,
requiresActionIntent,
succeededIntent,
)

val clientSecret = requireNotNull(requiresActionIntent.clientSecret)

val result = processor.processResult(
PaymentFlowResult.Unvalidated(
clientSecret = clientSecret,
flowOutcome = StripeIntentResult.Outcome.UNKNOWN,
)
).getOrThrow()

val expectedResult = SetupIntentResult(
intent = succeededIntent,
outcomeFromFlow = StripeIntentResult.Outcome.SUCCEEDED,
failureMessage = null,
)

assertThat(result).isEqualTo(expectedResult)
}
}