diff --git a/connect/src/main/java/com/stripe/android/connect/webview/StripeConnectWebViewContainer.kt b/connect/src/main/java/com/stripe/android/connect/webview/StripeConnectWebViewContainer.kt index 9e928c917b2..67d3f384596 100644 --- a/connect/src/main/java/com/stripe/android/connect/webview/StripeConnectWebViewContainer.kt +++ b/connect/src/main/java/com/stripe/android/connect/webview/StripeConnectWebViewContainer.kt @@ -357,7 +357,10 @@ internal class StripeConnectWebViewContainerImpl( @JavascriptInterface fun onSetterFunctionCalled(message: String) { - val parsed = ConnectJson.decodeFromString(message) + val parsed = tryDeserializeWebMessage( + webFunctionName = "onSetterFunctionCalled", + message = message, + ) ?: return logger.debug("Setter function called: $parsed") controller?.onReceivedSetterFunctionCalled(parsed) @@ -365,13 +368,19 @@ internal class StripeConnectWebViewContainerImpl( @JavascriptInterface fun openSecureWebView(message: String) { - val secureWebViewData = ConnectJson.decodeFromString(message) + val secureWebViewData = tryDeserializeWebMessage( + webFunctionName = "openSecureWebView", + message = message, + ) logger.debug("Open secure web view with data: $secureWebViewData") } @JavascriptInterface fun pageDidLoad(message: String) { - val pageLoadMessage = ConnectJson.decodeFromString(message) + val pageLoadMessage = tryDeserializeWebMessage( + webFunctionName = "pageDidLoad", + message = message, + ) ?: return logger.debug("Page did load: $pageLoadMessage") controller?.onReceivedPageDidLoad(pageLoadMessage.pageViewId) @@ -379,7 +388,10 @@ internal class StripeConnectWebViewContainerImpl( @JavascriptInterface fun accountSessionClaimed(message: String) { - val accountSessionClaimedMessage = ConnectJson.decodeFromString(message) + val accountSessionClaimedMessage = tryDeserializeWebMessage( + webFunctionName = "accountSessionClaimed", + message = message, + ) ?: return logger.debug("Account session claimed: $accountSessionClaimedMessage") controller?.onMerchantIdChanged(accountSessionClaimedMessage.merchantId) @@ -393,6 +405,22 @@ internal class StripeConnectWebViewContainerImpl( } } + private inline fun tryDeserializeWebMessage( + webFunctionName: String, + message: String, + ): T? { + return try { + ConnectJson.decodeFromString(message) + } catch (e: IllegalArgumentException) { + controller?.onErrorDeserializingWebMessage( + webMessage = message, + error = "Unable to deserialize message from $webFunctionName", + errorMessage = e.message, + ) + null + } + } + private fun WebView.evaluateSdkJs(function: String, payload: JsonObject) { val command = "$ANDROID_JS_INTERFACE.$function($payload)" post { diff --git a/connect/src/main/java/com/stripe/android/connect/webview/StripeConnectWebViewContainerController.kt b/connect/src/main/java/com/stripe/android/connect/webview/StripeConnectWebViewContainerController.kt index e9eb9e088e2..a27aa86601e 100644 --- a/connect/src/main/java/com/stripe/android/connect/webview/StripeConnectWebViewContainerController.kt +++ b/connect/src/main/java/com/stripe/android/connect/webview/StripeConnectWebViewContainerController.kt @@ -127,6 +127,19 @@ internal class StripeConnectWebViewContainerController