UPI App Deep linking using Intent - inconsistent and buggy behavior

喜欢而已 提交于 2019-11-30 04:04:52

It really works for the BHIM application also. Use this Code it works like a charm for every PSP enabled applications.

Note: Instead of using the "%" better to use "+" to replace the white space from the URL. That works better.

private String getUPIString(String payeeAddress, String payeeName, String payeeMCC, String trxnID, String trxnRefId,
                            String trxnNote, String payeeAmount, String currencyCode, String refUrl) {
    String UPI = "upi://pay?pa=" + payeeAddress + "&pn=" + payeeName
            + "&mc=" + payeeMCC + "&tid=" + trxnID + "&tr=" + trxnRefId
            + "&tn=" + trxnNote + "&am=" + payeeAmount + "&cu=" + currencyCode
            + "&refUrl=" + refUrl;
    return UPI.replace(" ", "+");
}

Then pass the parameters in the method and pass the string to the Intent in this way:

Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(UPI));
            Intent chooser = Intent.createChooser(intent, "Pay with...");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                startActivityForResult(chooser, 1, null);
            }

For displaying payment page correctly, you have to setAction(Intent.ACTION_VIEW) on your intent.

I am getting response only from BHIM app by using getStringsExtra("response") on Intent data.

public void UPI()
    {
         Long tsLong = System.currentTimeMillis()/1000;
         String transaction_ref_id = tsLong.toString()+"UPI"; // This is your Transaction Ref id - Here we used as a timestamp -

         String sOrderId= tsLong +"UPI";// This is your order id - Here we used as a timestamp -

         Log.e("TR Reference ID==>",""+transaction_ref_id);
        Uri myAction = Uri.parse("upi://pay?pa="+sVPA+"&pn="+"Merchant%20Finance"+"&mc="+"&tid="+transaction_ref_id +"&tr="+transaction_ref_id +"&tn=Pay%20to%20Merchant%20Finance%20Assets&am="+"1.00"+"&mam=null&cu=INR&url=https://mystar.com/orderid="+sOrderId);


         PackageManager packageManager = getPackageManager();
         //Intent intent = packageManager.getLaunchIntentForPackage("com.mgs.induspsp"); // Comment line - if you want to open specific application then you can pass that package name For example if you want to open Bhim app then pass Bhim app package name - 
         Intent intent = new Intent();

         if (intent != null) {
             intent.setAction(Intent.ACTION_VIEW);
             intent.setData(myAction);
            // startActivity(intent);
             Intent chooser = Intent.createChooser(intent, "Pay with...");
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                 startActivityForResult(chooser, 1, null);
             }

         }
    }


// For onActivityResult -

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        try
        {
            Log.e("UPI RESULT REQUEST CODE-->",""+requestCode);
            Log.e("UPI RESULT RESULT CODE-->",""+resultCode);
            Log.e("UPI RESULT DATA-->",""+data);



            if(resultCode == -1)
            {

                // 200 Success

            }
            else
            {
                // 400 Failed
            }


            YourActivity.this.finish(); 


        }
        catch(Exception e)
        {
            Log.e("Error in UPI onActivityResult->",""+e.getMessage());
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!