diff --git a/payments-core/src/main/java/com/stripe/android/challenge/confirmation/IntentConfirmationChallengeActivity.kt b/payments-core/src/main/java/com/stripe/android/challenge/confirmation/IntentConfirmationChallengeActivity.kt index c41e09b2c87..7879c98dfd7 100644 --- a/payments-core/src/main/java/com/stripe/android/challenge/confirmation/IntentConfirmationChallengeActivity.kt +++ b/payments-core/src/main/java/com/stripe/android/challenge/confirmation/IntentConfirmationChallengeActivity.kt @@ -49,6 +49,10 @@ internal class IntentConfirmationChallengeActivity : AppCompatActivity() { hostUrl = HOST_URL, errorHandler = { error -> viewModel.handleWebViewError(error) + }, + openUri = { uri -> + val browserIntent = Intent(Intent.ACTION_VIEW, uri) + startActivity(browserIntent) } ) } diff --git a/payments-core/src/main/java/com/stripe/android/challenge/confirmation/IntentConfirmationWebViewClient.kt b/payments-core/src/main/java/com/stripe/android/challenge/confirmation/IntentConfirmationWebViewClient.kt index 2438bbc8b77..ae2b2eb1e3d 100644 --- a/payments-core/src/main/java/com/stripe/android/challenge/confirmation/IntentConfirmationWebViewClient.kt +++ b/payments-core/src/main/java/com/stripe/android/challenge/confirmation/IntentConfirmationWebViewClient.kt @@ -14,9 +14,16 @@ import androidx.annotation.RequiresApi internal class IntentConfirmationWebViewClient( private val hostUrl: String, - private val errorHandler: WebViewErrorHandler + private val errorHandler: WebViewErrorHandler, + private val openUri: (Uri) -> Unit ) : WebViewClient() { + override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { + val host = request?.url ?: return super.shouldOverrideUrlLoading(view, request) + openUri(host) + return true + } + @RequiresApi(Build.VERSION_CODES.M) override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) { super.onReceivedError(view, request, error) diff --git a/payments-core/src/test/java/com/stripe/android/challenge/confirmation/IntentConfirmationWebViewClientTest.kt b/payments-core/src/test/java/com/stripe/android/challenge/confirmation/IntentConfirmationWebViewClientTest.kt index 2aa91a57e23..48394d19a38 100644 --- a/payments-core/src/test/java/com/stripe/android/challenge/confirmation/IntentConfirmationWebViewClientTest.kt +++ b/payments-core/src/test/java/com/stripe/android/challenge/confirmation/IntentConfirmationWebViewClientTest.kt @@ -232,6 +232,26 @@ internal class IntentConfirmationWebViewClientTest { assertThat(errors[0].webViewErrorType).isEqualTo("render_process_gone") } + @Test + fun `shouldOverrideUrlLoading calls openUri with correct URI`() { + val capturedUris = mutableListOf() + val openUri: (Uri) -> Unit = { uri -> capturedUris.add(uri) } + val client = IntentConfirmationWebViewClient( + hostUrl = HOST_URL, + errorHandler = { }, + openUri = openUri + ) + val testUrl = "https://example.com/terms" + val request = createRequest(testUrl) + val webView = WebView(ApplicationProvider.getApplicationContext()) + + val result = client.shouldOverrideUrlLoading(webView, request) + + assertThat(result).isTrue() + assertThat(capturedUris).hasSize(1) + assertThat(capturedUris[0].toString()).isEqualTo(testUrl) + } + // Helper methods private fun testWithSetup( hostUrl: String = HOST_URL, @@ -239,7 +259,13 @@ internal class IntentConfirmationWebViewClientTest { ) { val capturedErrors = mutableListOf() val errorHandler = WebViewErrorHandler { error -> capturedErrors.add(error) } - val client = IntentConfirmationWebViewClient(hostUrl, errorHandler = errorHandler) + val capturedUris = mutableListOf() + val openUri: (Uri) -> Unit = { uri -> capturedUris.add(uri) } + val client = IntentConfirmationWebViewClient( + hostUrl = hostUrl, + errorHandler = errorHandler, + openUri = openUri + ) val webView = WebView(ApplicationProvider.getApplicationContext()) block(client, capturedErrors, webView)