0

I inflated a webview in my Dialog. The WebView controls the payment done by paypal.After payment WebView Loads the Page having message about payment in it. How Can I get That message?? The message can be anything like success,payment failed etc Please help!!

addJavascriptInterface not Working.., Adding Jsoup Library not working, Hitting on the url & getting html in String format not working

As it is a secure https connection

Below is an example of code that i used or tried so far......

protected void callpaynow() {

    final Dialog wbviewDialog = new Dialog(Cart.this);
    wbviewDialog.setCancelable(true);
    wbviewDialog.setCanceledOnTouchOutside(true);
    wbviewDialog.getWindow().requestFeature(getWindow().FEATURE_NO_TITLE);




    WebView wbview = new WebView(wbviewDialog.getContext());
    // webview.addJavascriptInterface(new MyJavaScriptInterface(Cart.this), "HtmlViewer");

     String postData = "order_id="+getSharedPreferences("sp", MODE_PRIVATE).getString("orderid", "")+"&tip_amount=20";
     wbview.setWebViewClient(new SSLTolerentWebViewClient());
        wbview.loadUrl(URL_PAYNOW+postData);
     wbview.setVerticalScrollBarEnabled(true);
        wbview.setHorizontalScrollBarEnabled(true);
        wbview.getSettings().setBuiltInZoomControls(true); 
        wbview.getSettings().setJavaScriptEnabled(true);


        wbview.getSettings().setUseWideViewPort(true); 
        wbview.getSettings().setLoadWithOverviewMode(true);

    wbview.setLayoutParams(new LayoutParams(
            android.view.ViewGroup.LayoutParams.FILL_PARENT,
            android.view.ViewGroup.LayoutParams.FILL_PARENT));


    wbviewDialog.setContentView(wbview);
    wbviewDialog.getWindow().setLayout(LayoutParams.FILL_PARENT,
            LayoutParams.MATCH_PARENT);

    wbviewDialog.show();



    /*WebView wbview = new WebView(Cart.this);
     String postData = "order_id="+getSharedPreferences("sp", MODE_PRIVATE).getString("orderid", "")+"&tip_amount=20";

        wbview.loadUrl(URL_PAYNOW+postData);
    //wbview.loadUrl(URL_PAYNOW+getSharedPreferences("sp", MODE_PRIVATE).getString("orderid", ""));
     wbview.setVerticalScrollBarEnabled(true);
        wbview.setHorizontalScrollBarEnabled(true);

        wbview.getSettings().setJavaScriptEnabled(true);

      //  wbview.getSettings().setDomStorageEnabled(true);

    wbview.setLayoutParams(new LayoutParams(
            android.view.ViewGroup.LayoutParams.FILL_PARENT,
            android.view.ViewGroup.LayoutParams.FILL_PARENT));

    //wbview.setWebViewClient(new SSLTolerentWebViewClient());

    */
}

/* class MyJavaScriptInterface { 

        private Context ctx;

        MyJavaScriptInterface(Context ctx) {
            this.ctx = ctx;
        } 

        public void showHTML(String html) {
            new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
                    .setPositiveButton("ok", null).setCancelable(false).create().show();
        } 

    } */

private class SSLTolerentWebViewClient extends WebViewClient {


    /*@Override
    public WebResourceResponse shouldInterceptRequest(WebView view,
            WebResourceRequest request) {
        // TODO Auto-generated method stub
        Log.e("request", request.toString());
        return super.shouldInterceptRequest(view, request);


    }*/

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);



    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub


        //doasync(view,url);


        /*try {
        fetchContent(view,url);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }*/


        Log.e("url",url);

        if(url.contains(URL.BASE_URL)){

            getString(url);

        }

        //Log.e("title", view.getTitle());
        /*
        Document doc;
        try {
            doc = Jsoup.connect(url).get();
            Elements newsHeadlines = doc.select("#mp-itn b a");
             Log.e("des",""+newsHeadlines.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/

        super.onPageFinished(view, url);





    }

    @Override 
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        super.onReceivedSslError(view, handler, error);
        handler.proceed(); // Ignore SSL certificate errors
    } 

}

public String fetchContent(WebView view, String url) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet get = new HttpGet(url);
    HttpResponse response = httpClient.execute(get);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    HttpEntity entity = response.getEntity();
    String html = EntityUtils.toString(entity); // assume html for simplicity
    view.loadDataWithBaseURL(url, html, "text/html", "utf-8", url); // todo: get mime, charset from entity
    if (statusCode != 200) {
        // handle fail 
    } 
    return html;
} 

public void doasync(final WebView view, final String url) {


    webview = view;
if(webview == view){
task.execute(url);
}
}



// Remember, never run network processes within UI threads
// ...so we use asynctask instead
AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {


  @Override
  protected void onPostExecute(String result) {
    // We are done and back to the UI thread
    // Show status or a screen here

      Log.e("response", result);

  }

@Override
protected String doInBackground(String... params) {
    try {
        HttpClient httpClient = new DefaultHttpClient();
    HttpGet get = new HttpGet(params[0]);
    HttpResponse response = httpClient.execute(get);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    HttpEntity entity = response.getEntity();
    String html = EntityUtils.toString(entity); // assume html for simplicity
    webview.loadDataWithBaseURL(params[0], html, "text/html", "utf-8", params[0]); // todo: get mime, charset from entity
    if (statusCode != 200) {
        // handle fail 
    } 
    return html;

    }
    catch (IOException e) {
      // Log.e(TAG, e.getMessage());
    }

    return "";
  }
};
TUSHAR
  • 327
  • 1
  • 10
  • Possible duplicate of http://stackoverflow.com/questions/8200945/how-to-get-html-content-from-a-webview – Rohit5k2 Aug 20 '15 at 08:00
  • You can get data but parsing that is up to you and the content it has. Implement the solution give in this question and fetch the html content. Once its done, we can help you parsing the message in a new question. – Rohit5k2 Aug 20 '15 at 08:00
  • @Rohit5k2: Please see my updated question – TUSHAR Aug 20 '15 at 08:05
  • You mean to say `webView.getSettings().setJavaScriptEnabled(true);` isn't working? – Rohit5k2 Aug 20 '15 at 08:07
  • webview.loadUrl("javascript:window.HtmlViewer.showHTML" + "(''+document.getElementsByTagName('html')[0].innerHTML+'');"); not working – TUSHAR Aug 20 '15 at 08:09
  • Can you put your code? – Rohit5k2 Aug 20 '15 at 08:13

1 Answers1

0

I'm too late for this answer, but wanted to warn you about handling payments based on messages on WebView or any other app. It is completely insecure, because anyone can manipulate and fake the payment on the webview, there are lots of options for fraud in such app.

If you want to implement any payment method including Google Pay or Paypal or whatever, you must use their own api separately for each one of them.

For example for Paypal you must use their Android SDK.

M D P
  • 4,525
  • 2
  • 23
  • 35