PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 11, 2022

[FIXED] How to do an action when flutter app lunch url close

 November 11, 2022     flutter, payment-gateway     No comments   

Issue

I am building a flutter app that integrates with payment gateway through API when I do HTTP Post request to the API, the gateway reply with Iframe (URL), where the user should use to enter his credit card details and complete payment.

I am using Lunch_url to open the Iframe recevied like this.

 Future CompetePaymntProcess(
   String paymentid, Map<String, dynamic> transactionresult) async {
   String iframe = transactionresult["data"]["iframe_url"];
   await launchUrlString(iframe);
}

Now I would like to do some actions based on the result of the user payment when he closes the web browser opened by LaunchURLString.

These actions are to send one more request to the API to get te payment result and do update my records.

How can I trigger an action when this web Browser is closed?

I did some research and found on Flutter web there is the below code:

 html.window.onBeforeUnload.listen((event) async {}

Is there anything similar in flutter app? or what is the best practice in my case?

Edit - Adding my tried code for Webview_Flutter Widget

 class _WebViewPageState extends State<WebViewPage> {
    @override
    Widget build(BuildContext context) {
    return WebView(
    initialUrl: paymentiframe,
    javascriptMode: JavascriptMode.unrestricted,
    navigationDelegate: (NavigationRequest request) {
     print("Navigation is detected");
     if (request.url.startsWith('https://valleysys.net')) {
      print('blocking navigation to $request}');
    }
    return NavigationDecision.prevent;
    },
    onPageStarted: (String url) {
    print("Started $url");
   },
  );
 }

Problem: Navigation Delegate never gets called, OnPageStarted only get called once when the Iframe is opened, but doesn't sense any navigation happening.

Code for Flutter_webview_plugin

 @override
 void initState() {
 final flutterWebviewPlugin = new FlutterWebviewPlugin();
 flutterWebviewPlugin.launch(paymentiframe);

 flutterWebviewPlugin.onUrlChanged.listen((String url) {
  print("Url Change is Detected");
  print(url);
  if (url == "https://valleysys.net") {
    print("Navigation to return URL");
    Navigator.of(context).pop();
  }
});

super.initState();

}

same problem is here, it detects the url only once when the first Iframe is opened, but doesn't get called again when the url is changed.


Solution

You can use webview which has a navigation delegate. Once the user enters the details navigate the user to another page which webview can capture

WebView(
        initialUrl: 'https://flutter.dev',
        javascriptMode: JavascriptMode.unrestricted,
        navigationDelegate: (NavigationRequest request) {
          if (request.url.startsWith('https://www.youtube.com/')) {
            print('blocking navigation to $request}');
//You can add your logic here
            return NavigationDecision.prevent;

          }
          
        },
      ),

https://pub.dev/packages/webview_flutter/example



Answered By - Kaushik Chandru
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing