PayUMoney Integration in Android

After finally toiling through the PayUMoney SDK for 3 days i have finally managed to implement the payments and capture the response successfully into the Application. Given below is the bare bones code using PayUMoney WebView.

Explanation will come later as i am exhausted right now.

I will also try to put up code for integration with PayUMoney Android SDK. For now, here it is perfectly working on 01- Sep-2015 . Later if they change something then bad luck folks.

 

public class Main3Activity extends AppCompatActivity {
    private WebView webView;
    private BroadcastReceiver mReceiver;
    PayuConfig payuConfig;
    String PAYMENT_URL= "https://test.payu.in/_payment";
    String hash = "9834d7f67c8db537cb1f63cc9ad3b7dd55fa27b540ea3b32f72f6d94075671c833d2ff547e53120b00411a371da77243161b5198f70fa2765c2851e9edf372fe";
    String key = "enter prod key";
    String txnId = "" ;
    String Salt = "enter prod salt" ;
    String SURL = "https://dl.dropboxusercontent.com/s/dtnvwz5p4uymjvg/success.html" ;
    String FURL = "https://dl.dropboxusercontent.com/s/z69y7fupciqzr7x/furlWithParams.html";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        payuConfig = new PayuConfig();
        key = "gtKFFx";
        Salt = "eCwWELxi" ;

        webView = (WebView) findViewById(R.id.webview);
        try {
            Class.forName("com.payu.custombrowser.Bank");
            final Bank bank = new Bank() {
                @Override
                public void registerBroadcast(BroadcastReceiver broadcastReceiver, IntentFilter filter) {
                    mReceiver = broadcastReceiver;
                    registerReceiver(broadcastReceiver, filter);
                }

                @Override
                public void unregisterBroadcast(BroadcastReceiver broadcastReceiver) {
                    if(mReceiver != null){
                        unregisterReceiver(mReceiver);
                        mReceiver = null;
                    }
                }

                @Override
                public void onHelpUnavailable() {
                    findViewById(R.id.parent).setVisibility(View.GONE);
                    findViewById(R.id.trans_overlay).setVisibility(View.GONE);
                }

                @Override
                public void onBankError() {
                    findViewById(R.id.parent).setVisibility(View.GONE);
                    findViewById(R.id.trans_overlay).setVisibility(View.GONE);
                }

                @Override
                public void onHelpAvailable() {
                    findViewById(R.id.parent).setVisibility(View.VISIBLE);
                }
            };
            Bundle args = new Bundle();
            args.putInt("webView", R.id.webview);
            args.putInt("tranLayout",R.id.trans_overlay);
            args.putInt("mainLayout",R.id.r_layout);

            webView.setWebChromeClient(new PayUWebChromeClient(bank){
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    super.onProgressChanged(view, newProgress);
                }
            });
            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.setWebViewClient(new PayUWebViewClient(bank){
                @Override
                public void onPageFinished(WebView view, String url) {
                    Log.e("URL-" +url);
                }

                /**
                 * Notify the host application that the WebView will load the resource
                 * specified by the given url.
                 *
                 * @param view The WebView that is initiating the callback.
                 * @param url  The url of the resource the WebView will load.
                 */
                @Override
                public void onLoadResource(WebView view, String url) {
                    Log.e("URL-"+ url);
                    super.onLoadResource(view, url);
                }

                /**
                 * Give the host application a chance to take over the control when a new
                 * url is about to be loaded in the current WebView. If WebViewClient is not
                 * provided, by default WebView will ask Activity Manager to choose the
                 * proper handler for the url. If WebViewClient is provided, return true
                 * means the host application handles the url, while return false means the
                 * current WebView handles the url.
                 * This method is not called for requests using the POST "method".
                 *
                 * @param view The WebView that is initiating the callback.
                 * @param url  The url to be loaded.
                 * @return True if the host application wants to leave the current WebView
                 * and handle the url itself, otherwise return false.
                 */
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    Log.e("URL-"+url);
                    return super.shouldOverrideUrlLoading(view, url);
                }
            });

            String [] list =  payuConfig.getData().split("&");
            String txnId = null;
            String merchantKey = null;
            for (String item : list) {
                if(item.contains("txnid")){
                    txnId = item.split("=")[1];
                }else if (item.contains("key")){
                    merchantKey = item.split("=")[1];
                }
                if (null != txnId && null != merchantKey) break;
            }
            args.putString(Bank.TXN_ID, txnId == null ? String.valueOf(System.currentTimeMillis()) : txnId);
            args.putString("merchantid", null != merchantKey ? merchantKey : "could not find");
            PayUSdkDetails payUSdkDetails = new PayUSdkDetails();
            args.putString("sdkcode", "VersionCode: " + payUSdkDetails.getSdkVersionCode() + " VersionName: " + payUSdkDetails.getSdkVersionName());
            if(getIntent().getExtras().containsKey("showCustom")) {
                args.putBoolean("showCustom", getIntent().getBooleanExtra("showCustom", false));
            }
            args.putBoolean("showCustom", true);
            bank.setArguments(args);
            findViewById(R.id.parent).bringToFront();
            try {
                getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fade_in, R.anim.cb_face_out).add(R.id.parent, bank).commit();
            }catch(Exception e)
            {
                e.printStackTrace();
                finish();
            }


        }catch (Exception e)
        {
            e.printStackTrace();
        } 
webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        String firstname = "navraj";
        String lastname= "singh";
       /* String postData1 = "firstname=" + firstname + "&lastname=" + ""
                + "&surl=" + sucess_url + "&phone=" + phone
                + "&key=" + key + "&hash=" + hash + "&curl=" + ""
                + "&furl=" + failure_url + "&txnid=" + txnid
                + "&productinfo=" + productinfo + "&amount="
                + amount + "&email=" + email + "&pg="
                + "CC" + "&drop_category="
                + "NB";*/

        String hashSequence = key + "|" + "01237" + "|" + "10" + "|" +
                "p111" + "|" + "navraj" + "|"
                + "navraj@navraj.net" + "|||||||||||" + Salt;
        Log.e(hashSequence);
        hash = Utils.hashCal("SHA-512", hashSequence);

        String postData = "hash="+ hash +
                "&key="+
                key+"&txnid=01237&amount=10&productinfo=p111&firstname=navraj&email=navraj@navraj.net&" +
                "phone=9991940305&salt="+ Salt+"&surl="+SURL+"&furl="+FURL;
        Log.i(postData);

        webView.postUrl(PAYMENT_URL, EncodingUtils.getBytes(postData, "base64"));
        // In case of using PayU PG
        //url = payuConfig.getEnvironment() == PayuConstants.PRODUCTION_ENV?  PayuConstants.PRODUCTION_PAYMENT_URL : //PayuConstants.MOBILE_TEST_PAYMENT_URL ;
        //webView.postUrl(url, EncodingUtils.getBytes(payuConfig.getData(), "base64"));
    }