0

I am doing my first application. Now i need to integrate payu integration in app using custom browser. i don't have knowledge on how to do this. I searched for this, but i failed. please any one suggest me or provide me links for this integration. thank you in advance.

like this :

https://drive.google.com/folderview?id=0B4URmsDLhGXmfjFmbDQ5b2V0bVhjdTZMNExMVHRFMG1PRFFYeUV0LU9nSWU4U0pqaW00OU0&usp=drive_web&ddrp=1#

kartheeki j
  • 2,206
  • 5
  • 27
  • 51

1 Answers1

3

Although u have to try at least once from yourself there are lot of SO questions available anyways this is complete solution to integrate payu in your app.It is very simple.

First define your variables likes this:

String hash,hashSequence;
String post_Data;
String merchant_key="your key";
String salt="your salt";
String surl = "your surl"; 
String furl = "your furl";
String productinfo = "your product info or you can put anything here";
String txnid ="";
String amount;
Handler mHandler = new Handler();
String firstname, email,phone;
WebView webView ;

After defining values your on createview should be like this:

@SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_view);

        webView = (WebView) findViewById(R.id.webView);

        String hashSequence1 = merchant_key + "|" + txnid + "|" + amount + "|" + productinfo + "|" +
                               firstname+  "|" + email + "|||||||||||" + salt;

        hash=hashCal("SHA-512",hashSequence1);

        webView.setWebViewClient(new WebViewClient() {


            @Override
            public void 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
            public void 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
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                System.out.println(">>>>>>>>>>>>>>shouldOverrideUrlLoading>>>>>>>>>>>>>>>>>>");
                return true;

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                System.out.println(">>>>>>>>>>>>>>onPageFinished>>>>>>>>>>>>>>>>>>");

                super.onPageFinished(view, url);

            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                System.out.println(">>>>>>>>>>>>>>onPageStarted>>>>>>>>>>>>>>>>>>");
            }
        });


        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(true);
        webView.getSettings().setLoadWithOverviewMode(true);

        webView.addJavascriptInterface(new PayUJavaScriptInterface(this), "PayUMoney");

         post_Data = "hash="+hash+"&key="+merchant_key+"&txnid="+txnid+"&amount="+amount+
                "&productinfo="+productinfo+"&firstname="+firstname+ "&email="+email+"&phone="+phone+
                "&surl="+surl+"&furl="+ furl+ "&service_provider="+ "payu_paisa";

        webView.postUrl("https://secure.payu.in/_payment", EncodingUtils.getBytes(post_Data, "base64"));


    }

PayUJavaScriptInterface method:

    public class PayUJavaScriptInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        PayUJavaScriptInterface(Context c) {
            mContext = c;
        }


        public void success(long id, final String paymentId) {

            mHandler.post(new Runnable() {

                public void run() {

                    mHandler = null;

                    Intent intent = new Intent(PayMentGateWay.this, BoutiqueActivity.class);

                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

                    intent.putExtra("result", "success");

                    intent.putExtra("paymentId", paymentId);

                    startActivity(intent);

                    finish();

                }

            });

        }

    }

Calculate hash from this method:

    public String hashCal(String type,String str){
        byte[] hashseq=str.getBytes();
        StringBuffer hexString = new StringBuffer();
        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[i]);
                if(hex.length()==1) hexString.append("0");
                hexString.append(hex);
            }

        }catch(NoSuchAlgorithmException nsae){ }

        return hexString.toString();


    }

}

That's it.

Pushpendra
  • 2,791
  • 4
  • 26
  • 49
  • thank you for reply... present i am using web view . so i am confused where i need to call this activity after web view or before web view? – kartheeki j Sep 02 '15 at 07:49
  • this activity is with web view only you do not have to call web view separately. check `oncreate` method properly. – Pushpendra Sep 02 '15 at 07:53
  • I think you are very new in developing. Yes it will work for everything. – Pushpendra Sep 02 '15 at 07:59
  • yes @spartan. this is my first app. new to developing too. sorry for asking again, how i send the productinfo,,,, i need to send orderid, uniqueid,deviceid,walletcash,...etc – kartheeki j Sep 02 '15 at 08:02
  • in product info you can put anything whatever you want or else can keep it empty also. – Pushpendra Sep 02 '15 at 08:04
  • how can i send multiple means orderid,uid, devitceid... to string product info – kartheeki j Sep 02 '15 at 08:05
  • we need to add any dependency in build gadle? – kartheeki j Oct 10 '15 at 09:25
  • @spartan.. i get checksum failed error...after working with your code android – Asif Sb Nov 19 '15 at 09:51
  • @AsifSb read your code once again, may be you forgot somthing because this code is working perfectly. – Pushpendra Nov 19 '15 at 12:24
  • @Spartan...thanks a lot man...after too many search i found your code working perfect and it is very ease...thanks again.. – Asif Sb Nov 20 '15 at 09:36
  • 1
    @Spartan I used same way but PayUJavaScriptInterface's success() not called ? – Harshid Dec 22 '15 at 06:48
  • @Spartan: how come i can call success method and where can i get the id and payment id pls.. help me – Manu Apr 04 '16 at 10:19
  • @ManoharPerepa surl is success url and furl is for fail, you will get details there. – Pushpendra Apr 04 '16 at 13:08
  • No i am talking about the method public void success(long id, final String paymentId) in PayUJavaScriptInterface.. this is not at all calling.. what i need to do to make this function to be called @Spartan – Manu Apr 05 '16 at 11:14
  • i couldn't understand about webView.postUrl("https://secure.payu.in/_payment", EncodingUtils.getBytes(post_Data, "base64")); EncoadingUtils is what ? – Dinesh May 26 '16 at 11:06
  • PayUJavaScriptInterface onsuccess is not called ?? any aditional settings needs to be set ?? – Surya Reddy May 16 '19 at 06:45