Saurabh Jain
Lurker
I am new here. I am facing this issue last 3 days. we have Web application and Android application.
In Web application we receive requests (POST method) from clients and redirect to a success or failure URL depending on the outcome.
Now problem with Android application: Using Webview I am sending same request to server. my code is executing but I don't know few things.
Note: In this code JavaScript code there I don't know how to use. I refer somewhere else. PayUJavaScriptInterface, success(), and popup() method not using. If I have to use for getting response then tell me how to use.
Here is my code for my activity
public classPayMentGateWayextends Activity{
privateArrayList<String> post_val =newArrayList<String>();
privateString post_Data="";
WebView webView ;
finalActivity activity =this;
privateString tag ="PayMentGateWay";
privateString hash,hashSequence;
String merchant_key="JBZaLc";
String salt="GQs7yium";
String action1 ="";
String base_url="https://test.payu.in";
int error=0;
String hashString="";
Map<String,String> params;
String txnid ="";
Handler mHandler =newHandler();
@SuppressLint("JavascriptInterface")
@override
protectedvoid onCreate(Bundle savedInstanceState){
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
finalProgressDialog progressDialog =newProgressDialog(activity);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
webView =newWebView(this);
setContentView(webView);
post_val.add("key");
post_val.add("JBZaLc");
post_val.add("txnid");
post_val.add("5d59dae66618a14f5020");
post_val.add("amount");
post_val.add("100.00");
post_val.add("productinfo");
post_val.add("{paymentParts:[{name:abc,description:abcd,value:500,isRequired:true,settlementEvent:EmailConfirmation}], paymentIdentifiers:[{field:CompletionDate, value:25/06/2015}]}");
post_val.add("firstname");
post_val.add("pqrs");
post_val.add("email");
post_val.add("xxxx@xxxmail.com");
post_val.add("phone");
post_val.add("xxxxxxxxxx");
post_val.add("surl");
post_val.add("https://www.google.com");
post_val.add("furl");
post_val.add("https://www.gmail.com");
post_val.add("hash");
post_val.add("");
post_val.add("provider");
post_val.add("payu_paisa");
Log.d(tag,"post_val: "+post_val);
params=newHashMap<String,String>();
for(int i =0;i<post_val.size()
{
params.put(post_val.get(i), post_val.get(i+1));
i+=2;
}
if(empty(params.get("txnid"))){
Random rand =newRandom();
String rndm =Integer.toString(rand.nextInt())+(System.currentTimeMillis()/1000L);
txnid=hashCal("SHA-256",rndm).substring(0,20);
popup("txnid : "+ txnid);
System.out.println("......txnid...... "+ txnid);
}else{
txnid=params.get("txnid");
System.out.println("....else.......txnid...... "+ txnid);
}
//String udf2 = txnid;
String txn="abcd";
hash="";
String hashSequence ="key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";
if(empty(params.get("hash"))&& params.size()>0){
if( empty(params.get("key"))
|| empty(params.get("txnid"))
|| empty(params.get("amount"))
|| empty(params.get("firstname"))
|| empty(params.get("email"))
|| empty(params.get("phone"))
|| empty(params.get("productinfo"))
|| empty(params.get("surl"))
|| empty(params.get("furl"))
|| empty(params.get("service_provider"))
){
String[] hashVarSeq = hashSequence.split("\\|");
for(int i =0; i < hashVarSeq.length; i++){
System.out.println("<<<<<>>>>>>>> "+ hashVarSeq);
}
for(String part : hashVarSeq)
{
hashString=(empty(params.get(part)))? hashString.concat(""): hashString.concat(params.get(part));
hashString=hashString.concat("|");
System.out.println("??????? "+ hashString);
}
hashString=hashString.concat(salt);
hash=hashCal("SHA-512",hashString);
System.out.println(hashString +" /////~~~~~~ "+ hash);
action1=base_url.concat("/_payment");
}
}
elseif(!empty(params.get("hash")))
{
hash=params.get("hash");
action1=base_url.concat("/_payment");
System.out.println("!!!!!!!!HASHHHHHHH!!!!!! "+ hash);
}
webView.setWebViewClient(newWebViewClient(){
@override
publicvoid onReceivedError(WebView view,int errorCode,
String description,String failingUrl){
// TODO Auto-generated method stub
System.out.println(">>>>>>>>>>>>>>onReceivedError>>>>>>>>>>>>>>>>>>");
Toast.makeText(activity,"Oh no! "+ description,Toast.LENGTH_SHORT).show();
}
@override
publicvoid onReceivedSslError(WebView view,
SslErrorHandler handler,SslError error){
// TODO Auto-generated method stub
System.out.println(">>>>>>>>>>>>>>onReceivedSslError>>>>>>>>>>>>>>>>>>");
Toast.makeText(activity,"SslError! "+ error,Toast.LENGTH_SHORT).show();
handler.proceed();
}
@override
publicboolean shouldOverrideUrlLoading(WebView view,String url){
// TODO Auto-generated method stub
System.out.println(">>>>>>>>>>>>>>shouldOverrideUrlLoading>>>>>>>>>>>>>>>>>>");
returnsuper.shouldOverrideUrlLoading(view, url);
}
@override
publicvoid onPageFinished(WebView view,String url){
// TODO Auto-generated method stub
super.onPageFinished(view, url);
System.out.println(">>>>>>>>>>>>>>onPageFinished>>>>>>>>>>>>>>>>>>");
}
});
webView.setVisibility(0);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setCacheMode(2);
webView.getSettings().setDomStorageEnabled(true);
webView.clearHistory();
webView.clearCache(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(false);
webView.getSettings().setLoadWithOverviewMode(false);
webView.addJavascriptInterface(newPayUJavaScriptInterface(activity),"PayUMoney");
Map<String,String> mapParams =newHashMap<String,String>();
mapParams.put("key",merchant_key);
mapParams.put("hash",PayMentGateWay.this.hash);
mapParams.put("txnid",(empty(PayMentGateWay.this.params.get("txnid")))?""
ayMentGateWay.this.params.get("txnid"));
Log.d(tag,"txnid: "+PayMentGateWay.this.params.get("txnid"));
mapParams.put("service_provider","payu_paisa");
mapParams.put("amount",(empty(PayMentGateWay.this.params.get("amount")))?""
ayMentGateWay.this.params.get("amount"));
mapParams.put("firstname",(empty(PayMentGateWay.this.params.get("firstname")))?""
ayMentGateWay.this.params.get("firstname"));
mapParams.put("email",(empty(PayMentGateWay.this.params.get("email")))?""
ayMentGateWay.this.params.get("email"));
mapParams.put("phone",(empty(PayMentGateWay.this.params.get("phone")))?""
ayMentGateWay.this.params.get("phone"));
mapParams.put("productinfo",(empty(PayMentGateWay.this.params.get("productinfo")))?""
ayMentGateWay.this.params.get("productinfo"));
mapParams.put("surl",(empty(PayMentGateWay.this.params.get("surl")))?""
ayMentGateWay.this.params.get("surl"));
mapParams.put("furl",(empty(PayMentGateWay.this.params.get("furl")))?""
ayMentGateWay.this.params.get("furl"));
mapParams.put("lastname",(empty(PayMentGateWay.this.params.get("lastname")))?""
ayMentGateWay.this.params.get("lastname"));
mapParams.put("address1",(empty(PayMentGateWay.this.params.get("address1")))?""
ayMentGateWay.this.params.get("address1"));
mapParams.put("address2",(empty(PayMentGateWay.this.params.get("address2")))?""
ayMentGateWay.this.params.get("address2"));
mapParams.put("city",(empty(PayMentGateWay.this.params.get("city")))?""
ayMentGateWay.this.params.get("city"));
mapParams.put("state",(empty(PayMentGateWay.this.params.get("state")))?""
ayMentGateWay.this.params.get("state"));
mapParams.put("country",(empty(PayMentGateWay.this.params.get("country")))?""
ayMentGateWay.this.params.get("country"));
mapParams.put("zipcode",(empty(PayMentGateWay.this.params.get("zipcode")))?""
ayMentGateWay.this.params.get("zipcode"));
mapParams.put("udf1",(empty(PayMentGateWay.this.params.get("udf1")))?""
ayMentGateWay.this.params.get("udf1"));
mapParams.put("udf2",(empty(PayMentGateWay.this.params.get("udf2")))?""
ayMentGateWay.this.params.get("udf2"));
mapParams.put("udf3",(empty(PayMentGateWay.this.params.get("udf3")))?""
ayMentGateWay.this.params.get("udf3"));
mapParams.put("udf4",(empty(PayMentGateWay.this.params.get("udf4")))?""
ayMentGateWay.this.params.get("udf4"));
mapParams.put("udf5",(empty(PayMentGateWay.this.params.get("udf5")))?""
ayMentGateWay.this.params.get("udf5"));
mapParams.put("pg",(empty(PayMentGateWay.this.params.get("pg")))?""
ayMentGateWay.this.params.get("pg"));
webview_ClientPost(webView, action1, mapParams.entrySet());
}
publicclassPayUJavaScriptInterface{
Context mContext;
/** Instantiate the interface and set the context */
PayUJavaScriptInterface(Context c){
mContext = c;
}
publicvoid success(long id,finalString paymentId){
mHandler.post(newRunnable(){
publicvoid run(){
mHandler =null;
Intent intent =newIntent(PayMentGateWay.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("result","success");
intent.putExtra("paymentId", paymentId);
startActivity(intent);
finish();
}
});
}
}
publicvoid webview_ClientPost(WebView webView,String url,Collection<Map.Entry<String,String>> postData){
StringBuilder sb =newStringBuilder();
sb.append("<html><head></head>");
sb.append("<body onload='form1.submit()'>");
sb.append(String.format("<form id='form1' action='%s' method='%s'>", url,"post"));
for(Map.Entry<String,String> item : postData){
sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue()));
}
sb.append("</form></body></html>");
Log.d(tag,"webview_ClientPost called");
webView.loadData(sb.toString(),"text/html","utf-8");
}
publicvoid success(long id,finalString paymentId){
mHandler.post(newRunnable(){
publicvoid run(){
mHandler =null;
Intent intent =newIntent(PayMentGateWay.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(Constants.ACCOUNT_NAME,"success");
intent.putExtra(Constants._ID, paymentId);
startActivity(intent);
finish();
}
});
}
publicboolean empty(String s)
{
if(s==null|| s.trim().equals(""))
returntrue;
else
returnfalse;
}
publicString hashCal(String type,String str){
byte[] hashseq=str.getBytes();
StringBuffer hexString =newStringBuffer();
try{
MessageDigest algorithm =MessageDigest.getInstance(type);
algorithm.reset();
algorithm.update(hashseq);
byte messageDigest[]= algorithm.digest();
for(int i=0;i<messageDigest.length;i++){
String hex=Integer.toHexString(0xFF& messageDigest);
if(hex.length()==1) hexString.append("0");
hexString.append(hex);
}
}catch(NoSuchAlgorithmException nsae){}
return hexString.toString();
}
publicvoid popup(String msg){
Toast.makeText(PayMentGateWay.this,""+ msg,Toast.LENGTH_LONG).show();
}
}
In Web application we receive requests (POST method) from clients and redirect to a success or failure URL depending on the outcome.
Now problem with Android application: Using Webview I am sending same request to server. my code is executing but I don't know few things.
- what will be the success and failure URL for Android?
- how to get response from client side and get back to the activity?
Note: In this code JavaScript code there I don't know how to use. I refer somewhere else. PayUJavaScriptInterface, success(), and popup() method not using. If I have to use for getting response then tell me how to use.
Here is my code for my activity
public classPayMentGateWayextends Activity{
privateArrayList<String> post_val =newArrayList<String>();
privateString post_Data="";
WebView webView ;
finalActivity activity =this;
privateString tag ="PayMentGateWay";
privateString hash,hashSequence;
String merchant_key="JBZaLc";
String salt="GQs7yium";
String action1 ="";
String base_url="https://test.payu.in";
int error=0;
String hashString="";
Map<String,String> params;
String txnid ="";
Handler mHandler =newHandler();
@SuppressLint("JavascriptInterface")
@override
protectedvoid onCreate(Bundle savedInstanceState){
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
finalProgressDialog progressDialog =newProgressDialog(activity);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
webView =newWebView(this);
setContentView(webView);
post_val.add("key");
post_val.add("JBZaLc");
post_val.add("txnid");
post_val.add("5d59dae66618a14f5020");
post_val.add("amount");
post_val.add("100.00");
post_val.add("productinfo");
post_val.add("{paymentParts:[{name:abc,description:abcd,value:500,isRequired:true,settlementEvent:EmailConfirmation}], paymentIdentifiers:[{field:CompletionDate, value:25/06/2015}]}");
post_val.add("firstname");
post_val.add("pqrs");
post_val.add("email");
post_val.add("xxxx@xxxmail.com");
post_val.add("phone");
post_val.add("xxxxxxxxxx");
post_val.add("surl");
post_val.add("https://www.google.com");
post_val.add("furl");
post_val.add("https://www.gmail.com");
post_val.add("hash");
post_val.add("");
post_val.add("provider");
post_val.add("payu_paisa");
Log.d(tag,"post_val: "+post_val);
params=newHashMap<String,String>();
for(int i =0;i<post_val.size()

params.put(post_val.get(i), post_val.get(i+1));
i+=2;
}
if(empty(params.get("txnid"))){
Random rand =newRandom();
String rndm =Integer.toString(rand.nextInt())+(System.currentTimeMillis()/1000L);
txnid=hashCal("SHA-256",rndm).substring(0,20);
popup("txnid : "+ txnid);
System.out.println("......txnid...... "+ txnid);
}else{
txnid=params.get("txnid");
System.out.println("....else.......txnid...... "+ txnid);
}
//String udf2 = txnid;
String txn="abcd";
hash="";
String hashSequence ="key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";
if(empty(params.get("hash"))&& params.size()>0){
if( empty(params.get("key"))
|| empty(params.get("txnid"))
|| empty(params.get("amount"))
|| empty(params.get("firstname"))
|| empty(params.get("email"))
|| empty(params.get("phone"))
|| empty(params.get("productinfo"))
|| empty(params.get("surl"))
|| empty(params.get("furl"))
|| empty(params.get("service_provider"))
){
String[] hashVarSeq = hashSequence.split("\\|");
for(int i =0; i < hashVarSeq.length; i++){
System.out.println("<<<<<>>>>>>>> "+ hashVarSeq);
}
for(String part : hashVarSeq)
{
hashString=(empty(params.get(part)))? hashString.concat(""): hashString.concat(params.get(part));
hashString=hashString.concat("|");
System.out.println("??????? "+ hashString);
}
hashString=hashString.concat(salt);
hash=hashCal("SHA-512",hashString);
System.out.println(hashString +" /////~~~~~~ "+ hash);
action1=base_url.concat("/_payment");
}
}
elseif(!empty(params.get("hash")))
{
hash=params.get("hash");
action1=base_url.concat("/_payment");
System.out.println("!!!!!!!!HASHHHHHHH!!!!!! "+ hash);
}
webView.setWebViewClient(newWebViewClient(){
@override
publicvoid onReceivedError(WebView view,int errorCode,
String description,String failingUrl){
// TODO Auto-generated method stub
System.out.println(">>>>>>>>>>>>>>onReceivedError>>>>>>>>>>>>>>>>>>");
Toast.makeText(activity,"Oh no! "+ description,Toast.LENGTH_SHORT).show();
}
@override
publicvoid onReceivedSslError(WebView view,
SslErrorHandler handler,SslError error){
// TODO Auto-generated method stub
System.out.println(">>>>>>>>>>>>>>onReceivedSslError>>>>>>>>>>>>>>>>>>");
Toast.makeText(activity,"SslError! "+ error,Toast.LENGTH_SHORT).show();
handler.proceed();
}
@override
publicboolean shouldOverrideUrlLoading(WebView view,String url){
// TODO Auto-generated method stub
System.out.println(">>>>>>>>>>>>>>shouldOverrideUrlLoading>>>>>>>>>>>>>>>>>>");
returnsuper.shouldOverrideUrlLoading(view, url);
}
@override
publicvoid onPageFinished(WebView view,String url){
// TODO Auto-generated method stub
super.onPageFinished(view, url);
System.out.println(">>>>>>>>>>>>>>onPageFinished>>>>>>>>>>>>>>>>>>");
}
});
webView.setVisibility(0);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setCacheMode(2);
webView.getSettings().setDomStorageEnabled(true);
webView.clearHistory();
webView.clearCache(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(false);
webView.getSettings().setLoadWithOverviewMode(false);
webView.addJavascriptInterface(newPayUJavaScriptInterface(activity),"PayUMoney");
Map<String,String> mapParams =newHashMap<String,String>();
mapParams.put("key",merchant_key);
mapParams.put("hash",PayMentGateWay.this.hash);
mapParams.put("txnid",(empty(PayMentGateWay.this.params.get("txnid")))?""

Log.d(tag,"txnid: "+PayMentGateWay.this.params.get("txnid"));
mapParams.put("service_provider","payu_paisa");
mapParams.put("amount",(empty(PayMentGateWay.this.params.get("amount")))?""

mapParams.put("firstname",(empty(PayMentGateWay.this.params.get("firstname")))?""

mapParams.put("email",(empty(PayMentGateWay.this.params.get("email")))?""

mapParams.put("phone",(empty(PayMentGateWay.this.params.get("phone")))?""

mapParams.put("productinfo",(empty(PayMentGateWay.this.params.get("productinfo")))?""

mapParams.put("surl",(empty(PayMentGateWay.this.params.get("surl")))?""

mapParams.put("furl",(empty(PayMentGateWay.this.params.get("furl")))?""

mapParams.put("lastname",(empty(PayMentGateWay.this.params.get("lastname")))?""

mapParams.put("address1",(empty(PayMentGateWay.this.params.get("address1")))?""

mapParams.put("address2",(empty(PayMentGateWay.this.params.get("address2")))?""

mapParams.put("city",(empty(PayMentGateWay.this.params.get("city")))?""

mapParams.put("state",(empty(PayMentGateWay.this.params.get("state")))?""

mapParams.put("country",(empty(PayMentGateWay.this.params.get("country")))?""

mapParams.put("zipcode",(empty(PayMentGateWay.this.params.get("zipcode")))?""

mapParams.put("udf1",(empty(PayMentGateWay.this.params.get("udf1")))?""

mapParams.put("udf2",(empty(PayMentGateWay.this.params.get("udf2")))?""

mapParams.put("udf3",(empty(PayMentGateWay.this.params.get("udf3")))?""

mapParams.put("udf4",(empty(PayMentGateWay.this.params.get("udf4")))?""

mapParams.put("udf5",(empty(PayMentGateWay.this.params.get("udf5")))?""

mapParams.put("pg",(empty(PayMentGateWay.this.params.get("pg")))?""

webview_ClientPost(webView, action1, mapParams.entrySet());
}
publicclassPayUJavaScriptInterface{
Context mContext;
/** Instantiate the interface and set the context */
PayUJavaScriptInterface(Context c){
mContext = c;
}
publicvoid success(long id,finalString paymentId){
mHandler.post(newRunnable(){
publicvoid run(){
mHandler =null;
Intent intent =newIntent(PayMentGateWay.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("result","success");
intent.putExtra("paymentId", paymentId);
startActivity(intent);
finish();
}
});
}
}
publicvoid webview_ClientPost(WebView webView,String url,Collection<Map.Entry<String,String>> postData){
StringBuilder sb =newStringBuilder();
sb.append("<html><head></head>");
sb.append("<body onload='form1.submit()'>");
sb.append(String.format("<form id='form1' action='%s' method='%s'>", url,"post"));
for(Map.Entry<String,String> item : postData){
sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue()));
}
sb.append("</form></body></html>");
Log.d(tag,"webview_ClientPost called");
webView.loadData(sb.toString(),"text/html","utf-8");
}
publicvoid success(long id,finalString paymentId){
mHandler.post(newRunnable(){
publicvoid run(){
mHandler =null;
Intent intent =newIntent(PayMentGateWay.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(Constants.ACCOUNT_NAME,"success");
intent.putExtra(Constants._ID, paymentId);
startActivity(intent);
finish();
}
});
}
publicboolean empty(String s)
{
if(s==null|| s.trim().equals(""))
returntrue;
else
returnfalse;
}
publicString hashCal(String type,String str){
byte[] hashseq=str.getBytes();
StringBuffer hexString =newStringBuffer();
try{
MessageDigest algorithm =MessageDigest.getInstance(type);
algorithm.reset();
algorithm.update(hashseq);
byte messageDigest[]= algorithm.digest();
for(int i=0;i<messageDigest.length;i++){
String hex=Integer.toHexString(0xFF& messageDigest);
if(hex.length()==1) hexString.append("0");
hexString.append(hex);
}
}catch(NoSuchAlgorithmException nsae){}
return hexString.toString();
}
publicvoid popup(String msg){
Toast.makeText(PayMentGateWay.this,""+ msg,Toast.LENGTH_LONG).show();
}
}