2014-04-02 115 views
7

我在我的android應用中使用支付網關。我使用webview加載付款頁面。我提供了一個重定向網址到付款網關,在確認付款後,webview將被重定向到該網站。來自銀行的確認(成功/失敗)將被髮回到此URL。我可以將我的webview重定向到此URL以顯示客戶交易成功。我需要獲取發送到重定向URL的POST數據。如果交易成功,我需要將訂單放入我的應用中。我目前正在做的是,我正在檢查重定向網址,無論是成功的交易。我想知道是否有其他方法可以用來檢查我的交易狀態?這裏是我的代碼,從Android Webview獲取POST數據

mWebview = (WebView)findViewById(R.id.webView1); 

       mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript 
       mWebview.getSettings().setAppCacheEnabled(false); 
       mWebview.getSettings().setLoadWithOverviewMode(true); 
       mWebview.getSettings().setUseWideViewPort(true); 
       mWebview.getSettings().setBuiltInZoomControls(true); 


       mWebview.setWebViewClient(new WebViewClient() { 
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
         Toast.makeText(activity, description, Toast.LENGTH_SHORT).show(); 
        } 

        @Override 
        public void onPageStarted(WebView view, String url, Bitmap favicon) 
        { 
         pd.show(); 
        } 


        @Override 
        public void onPageFinished(WebView view, String url) { 
         pd.dismiss(); 


         String webUrl = mWebview.getUrl(); 


         Log.i("RETURN URL", "RETURN URL IS "+webUrl); 


          if(url.equals("http://www.mydomain.in/trxn_complete")) //This is my method.But I think its ugly one 
         { 
           AlertDialog alertDialog = new AlertDialog.Builder(OnlinePaymentActivity.this).create();    

           alertDialog.setMessage("Transaction successful.Press OK to continue"); 
           alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog, int which) { 
              // TODO Add your code for the button here. 


               //Transaction success, So place order 
               new Orderitems(OnlinePaymentActivity.this).execute(); 




             } 
            }); 
           alertDialog.show(); 
         } 




} 

}); 


     mWebview .loadUrl("http://263.134.260.173/gateway/epi/fts?ttype="+type+"&tempTxnId="+tempTxnId+"&token="+token+"&txnStage="+txnStage); 




     } 
+0

你已經解決問題了嗎? –

回答

0

試試這個:

private static final String paymentReturnUrl="http:/yourUrl"; 

private class FormDataInterface { 

     @JavascriptInterface 
     public void processFormData(String url,String formData) { 
      Log.d(DEBUG_TAG,"Url:"+url+" form data "+formData); 
      if(url.equals(paymentReturnUrl)){ 
       HashMap<String,String> map=new HashMap<>(); 
       String[] values = formData.split("&"); 
       for(String pair :values){ 
        String[] nameValue=pair.split("="); 
        if(nameValue.length==2){ 
         Log.d(DEBUG_TAG,"Name:"+nameValue[0]+" value:"+nameValue[1]); 
         map.put(nameValue[0],nameValue[1]); 
        } 
       } 

       return; 
      } 
     } 
    } 

    private class CustomWebViewClient extends WebViewClient{ 
     private final String jsCode ="" + "function parseForm(form){"+ 
      "var values='';"+ 
      "for(var i=0 ; i< form.elements.length; i++){"+ 
      " values+=form.elements[i].name+'='+form.elements[i].value+'&'"+ 
      "}"+ 
      "var url=form.action;"+ 
       "console.log('parse form fired');"+ 
       "window.FORMOUT.processFormData(url,values);"+ 
    " }"+ 
     "for(var i=0 ; i< document.forms.length ; i++){"+ 
     " parseForm(document.forms[i]);"+ 
       "};"; 



     private static final String DEBUG_TAG = "CustomWebClient"; 

     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      if(url.equals(paymentReturnUrl)){ 
       Log.d(DEBUG_TAG,"return url cancelling"); 
       view.stopLoading(); 
       return; 
      } 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      Log.d(DEBUG_TAG, "Url: "+url); 
      if(url.equals(paymentReturnUrl)){ 
       return; 
      } 
      view.loadUrl("javascript:(function() { " + jsCode + "})()"); 

      super.onPageFinished(view, url); 
     } 

    } 

和Init與web視圖:

webView.getSettings().setJavaScriptEnabled(true); 
webView.getSettings().setDomStorageEnabled(true); 
webView.addJavascriptInterface(new FormDataInterface(), "FORMOUT"); 
webView.setWebViewClient(new CustomWebViewClient());