I am using payment gateway in my android app.I am using webview to load payment page.I have provided a redirect URL to payment gateway, into which the webview will be redirected after confirming payment. The confirmation from the Bank (success / failure) will be posted back to this URL. I can redirect my webview to this URL to show the customer that the transaction is successfull.I need to fetch the POST data which is sent to the redirect URL.I need to place the order in my app if the transaction is successful. What I am currently doing is, I am checking the redirect url , whether it is for successful transaction. I would like to know whether there is any other method I can use to check the status of my transaction? Here is my code,
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);
}
Try with this:
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);
}
}
And init the webview with:
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.addJavascriptInterface(new FormDataInterface(), "FORMOUT");
webView.setWebViewClient(new CustomWebViewClient());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With