How to integrate PayU money Gateway in Android? -
i developing ecommerce app in want integrate payumoney payment gateway. can me procedure, link or tutorial, how so? thanks.
i have done :) :) have edit successurl
, failureurl
:
its working me perfectly.
public class payumoneyactivity extends appcompatactivity { /** * adding webview setcontentview */ webview webview; /** * context activity */ context activity; /** * order id * request updating payment status if payment done */ int mid; //getting previous activity /** * required fields */ // test variables /* private string mmerchantkey = "fcyqqz"; private string msalt = "sfbpga8e"; private string mbaseurl = "https://test.payu.in"; */ // final variables private string mmerchantkey = "your merchant key"; private string msalt = "salt"; private string mbaseurl = "https://secure.payu.in"; private string maction = ""; // final url private string mtxnid; // create below randomly private string mhash; // create below randomly private string mproductinfo = "food items"; //passing string private string mfirstname; // previous activity private string memailid; // previous activity private double mamount; // previous activity private string mphone; // previous activity private string mserviceprovider = "payu_paisa"; private string msuccessurl = "your success url"; private string mfailedurl = "your failure url"; boolean isfromorder; /** * handler */ handler mhandler = new handler(); /** * @param savedinstancestate */ @suppresslint({"addjavascriptinterface", "setjavascriptenabled"}) @override protected void oncreate(bundle savedinstancestate) { getwindow().requestfeature(window.feature_progress); super.oncreate(savedinstancestate); /** * setting webview screen */ setcontentview(r.layout.activity_webview_for_payumoney); /** * creating webview */ webview = (webview) findviewbyid(r.id.payumoney_webview); /** * context variable */ activity = getapplicationcontext(); /** * actionbar settings */ toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); actionbar ab = getsupportactionbar(); ab.setdisplayhomeasupenabled(true); // enabling action bar app icon , behaving toggle button ab.sethomebuttonenabled(true); ab.settitle(getstring(r.string.title_activity_online_payment)); /** * getting intent variables... */ bundle bundle = getintent().getextras(); if (bundle != null) { mfirstname = bundle.getstring("name"); memailid = bundle.getstring("email"); mamount = bundle.getdouble("amount"); mphone = bundle.getstring("phone"); mid = bundle.getint("id"); isfromorder = bundle.getboolean("isfromorder"); log.i(tag, "" + mfirstname + " : " + memailid + " : " + mamount + " : " + mphone); /** * creating transaction id */ random rand = new random(); string randomstring = integer.tostring(rand.nextint()) + (system.currenttimemillis() / 1000l); mtxnid = hashcal("sha-256", randomstring).substring(0, 20); mamount = new bigdecimal(mamount).setscale(0, roundingmode.up).intvalue(); /** * creating hash key */ mhash = hashcal("sha-512", mmerchantkey + "|" + mtxnid + "|" + mamount + "|" + mproductinfo + "|" + mfirstname + "|" + memailid + "|||||||||||" + msalt); /** * final action url... */ maction = mbaseurl.concat("/_payment"); /** * webview client */ webview.setwebviewclient(new webviewclient() { @override public void onreceivederror(webview view, webresourcerequest request, webresourceerror error) { super.onreceivederror(view, request, error); toast.maketext(activity, "oh no! " + error, toast.length_short).show(); } @override public void onreceivedsslerror(webview view, sslerrorhandler handler, sslerror error) { toast.maketext(activity, "ssl error! " + error, toast.length_short).show(); handler.proceed(); } @override public boolean shouldoverrideurlloading(webview view, string url) { return super.shouldoverrideurlloading(view, url); } @override public void onpagefinished(webview view, string url) { if (url.equals(msuccessurl)) { intent intent = new intent(payumoneyactivity.this, paymentstatusactivity.class); intent.putextra("status", true); intent.putextra("transaction_id", mtxnid); intent.putextra("id", mid); intent.putextra("isfromorder", isfromorder); intent.addflags(intent.flag_activity_clear_top); startactivity(intent); } else if (url.equals(mfailedurl)) { intent intent = new intent(payumoneyactivity.this, paymentstatusactivity.class); intent.putextra("status", false); intent.putextra("transaction_id", mtxnid); intent.putextra("id", mid); intent.putextra("isfromorder", isfromorder); intent.addflags(intent.flag_activity_clear_top); startactivity(intent); } super.onpagefinished(view, url); } }); webview.setvisibility(view.visible); 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(new payujavascriptinterface(payumoneyactivity.this), "payumoney"); /** * mapping compulsory key value pairs */ map<string, string> mapparams = new hashmap<>(); mapparams.put("key", mmerchantkey); mapparams.put("txnid", mtxnid); mapparams.put("amount", string.valueof(mamount)); mapparams.put("productinfo", mproductinfo); mapparams.put("firstname", mfirstname); mapparams.put("email", memailid); mapparams.put("phone", mphone); mapparams.put("surl", msuccessurl); mapparams.put("furl", mfailedurl); mapparams.put("hash", mhash); mapparams.put("service_provider", mserviceprovider); webviewclientpost(webview, maction, mapparams.entryset()); } else { toast.maketext(activity, "something went wrong, try again.", toast.length_long).show(); } } /** * posting data on payumoney site form * * @param webview * @param url * @param postdata */ public void webviewclientpost(webview webview, string url, collection<map.entry<string, string>> postdata) { stringbuilder sb = new stringbuilder(); sb.append("<html><head></head>"); sb.append("<body onload='form1.submit()'>"); sb.append(string.format("<form id='form1' action='%s' method='%s'>", url, "post")); (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", "webviewclientpost called: " + sb.tostring()); webview.loaddata(sb.tostring(), "text/html", "utf-8"); } /** * hash key calculation * * @param type * @param str * @return */ public string hashcal(string type, string str) { byte[] hashsequence = str.getbytes(); stringbuffer hexstring = new stringbuffer(); try { messagedigest algorithm = messagedigest.getinstance(type); algorithm.reset(); algorithm.update(hashsequence); byte messagedigest[] = algorithm.digest(); (int = 0; < messagedigest.length; i++) { string hex = integer.tohexstring(0xff & messagedigest[i]); if (hex.length() == 1) hexstring.append("0"); hexstring.append(hex); } } catch (nosuchalgorithmexception nsae) { } return hexstring.tostring(); } @override public boolean onoptionsitemselected(menuitem item) { if(item.getitemid() == android.r.id.home) { onpressingback(); } return super.onoptionsitemselected(item); } @override public void onbackpressed() { onpressingback(); } /** * on pressing * giving alert... */ private void onpressingback() { final intent intent; if(isfromorder) intent = new intent(payumoneyactivity.this, productincartlist.class); else intent = new intent(payumoneyactivity.this, mainactivity.class); intent.setflags(intent.flag_activity_clear_top); alertdialog.builder alertdialog = new alertdialog.builder(payumoneyactivity.this); // setting dialog title alertdialog.settitle("warning"); // setting dialog message alertdialog.setmessage("do cancel transaction?"); // on pressing settings button alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { finish(); startactivity(intent); } }); // on pressing cancel button alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }); // showing alert message alertdialog.show(); } public class payujavascriptinterface { context mcontext; /** * instantiate interface , set context */ payujavascriptinterface(context c) { mcontext = c; } public void success(long id, final string paymentid) { mhandler.post(new runnable() { public void run() { mhandler = null; toast.maketext(payumoneyactivity.this, "payment successfully.", toast.length_short).show(); } }); } } }
Comments
Post a Comment