Flutter Webview Apple Pay

Viewed 1157

In an attempt to use WebViewController.evaluateJavascript('myjs') I get this error in my debug console.

[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: PlatformException(evaluateJavaScript_failed, Failed evaluating JavaScript, JavaScript string was: 'doucment.getElementById("checkout_email").value = "Hell0";'
Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=0, WKJavaScriptExceptionMessage=Unable to run user agent scripts because this document has previously accessed Apple Pay. Documents can be prevented from accessing Apple Pay by adding a WKUserScript to the WKWebView's WKUserContentController., WKJavaScriptExceptionColumnNumber=0, NSLocalizedDescription=A JavaScript exception occurred})
#0      StandardMethodCodec.decodeEnvelope 
package:flutter/…/services/message_codecs.dart:572
#1      MethodChannel._invokeMethod 
package:flutter/…/services/platform_channel.dart:161
<asynchronous suspension>
#2      MethodChannel.invokeMethod 
package:flutter/…/services/platform_channel.dart:334
#3      MethodChannelWebViewPlatform<…>

My Dart file looks like this, On page load I do a simple check to ensure I execute the js on the correct page. If the check passes the js is executed but unfortunately returns the error above. Ive tried looking all over Google for an answer but ive found nothing similar to the issue I am having.

  final Completer<WebViewController> _controller =
      Completer<WebViewController>();

  TextEditingController _searchQueryController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: _searchBar(),
        leading: _directionButton('backward'),
        actions: [_directionButton('forward')],
      ),
      body: WebView(
        initialUrl: 'https://google.com',
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController webViewController) {
          _controller.complete(webViewController);
        },
        onPageStarted: (url) {
          _searchQueryController.text = url;
        },
        onPageFinished: (url) async {
          print('Page Finished: ' + url);
          final controller = await _controller.future;
          if (url.contains('checkouts')) {
            await controller.evaluateJavascript('doucment.getElementById("checkout_email").value = "Hello";');
          }
        },
      ),
    ));
  }
1 Answers

Try to add this into your info.plist , Replasing domain of your payment gateway

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>yourdomain.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

and Dont forget to re-install app

Related