Android 如何解析来自UPI sdk的未定义响应?

Android 如何解析来自UPI sdk的未定义响应?,android,payment-gateway,upi,Android,Payment Gateway,Upi,我正在创建一个示例项目,以通过用户获得付款,我使用Pocket成功地将其转移,但我收到的响应未定义 txnId=未定义&响应代码=未定义&状态=未定义&txnRef=未定义 我准备的url是:upi://pay?pn=john&tr=598&am=1&cu=INR&pa=9469731359@万国邮联 使用此示例代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate

我正在创建一个示例项目,以通过用户获得付款,我使用Pocket成功地将其转移,但我收到的响应未定义

txnId=未定义&响应代码=未定义&状态=未定义&txnRef=未定义

我准备的url是:upi://pay?pn=john&tr=598&am=1&cu=INR&pa=9469731359@万国邮联

使用此示例代码

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            Uri.Builder builder = new Uri.Builder();
            builder.appendQueryParameter("pa" , "9469731359@upi")
                    .appendQueryParameter("pn", "john")         //payee name.
                    .appendQueryParameter("tr", "5983") //transaction reference id.
//                    .appendQueryParameter("mc", "0000")                                 //payee merchant code.
//                    .appendQueryParameter("tn", "transferring1rs")             //transaction description.
                    .appendQueryParameter("am", "1")                                 //amountPayable (hardcoded to make payment of Rupee 1)
                    .appendQueryParameter("cu", "INR");                                 //currency code.
            String upiQueryString = "upi://pay" + builder.build().toString();     // virtual payment address of the payee.
            Intent intent = new Intent();
            intent.setData(Uri.parse(upiQueryString));
            Intent chooser = Intent.createChooser(intent, "Choose respective UPI");                             //Shows a list of UPI enabled PSP apps if more than one is available.
            startActivityForResult(chooser, 1, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        try {
            if (intent != null) {
                String pspResonse = intent.getStringExtra("response");
                String[] pspResonseArray = pspResonse.split("&");
                Map<String, String> map = new HashMap<>();
                for (String param : pspResonseArray) {
                    String key = param.split("=")[0];
                    String value = param.split("=")[1];
                    map.put(key, value);
                }

                TextView txnStatus = (TextView)findViewById(R.id.editText10);
                TextView txnId = (TextView)findViewById(R.id.editText11);
                TextView txnRef = (TextView)findViewById(R.id.editText12);
                TextView amount = (TextView)findViewById(R.id.editText13);

                // hide layout 1
                RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
                RelativeLayout layout2 = (RelativeLayout) findViewById(R.id.layout2);

                layout1.setVisibility(View.GONE);
                layout2.setVisibility(View.VISIBLE);

                txnStatus.setText(map.get("Status"));
                txnId.setText(map.get("txnId"));
                txnRef.setText(map.get("txnRef"));
                amount.setText("1");
            } else {
                Toast.makeText(this, "Null intent received", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
试一试{
Uri.Builder=新的Uri.Builder();
建筑商附录查询参数(“pa”9469731359@upi")
.appendQueryParameter(“pn”、“john”)//收款人名称。
.appendQueryParameter(“tr”,“5983”)//事务引用id。
//.appendQueryParameter(“mc”,“0000”)//收款人商户代码。
//.appendQueryParameter(“tn”、“transferring1rs”)//事务描述。
.appendQueryParameter(“am”,“1”)//应付金额(硬编码以支付1卢比)
.appendQueryParameter(“cu”、“INR”);//货币代码。
字符串upiQueryString=”upi://pay“+builder.build().toString();//收款人的虚拟付款地址。
意图=新意图();
setData(Uri.parse(upiQueryString));
Intent chooser=Intent.createChooser(Intent,“选择各自的UPI”);//如果有多个可用的PSP应用程序,则显示启用UPI的PSP应用程序列表。
startActivityForResult(选择器,1,空);
}捕获(例外e){
e、 printStackTrace();
}
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图){
super.onActivityResult(请求代码、结果代码、意图);
试一试{
if(intent!=null){
String pspresense=intent.getStringExtra(“响应”);
字符串[]pspResonseArray=pspResonse.split(“&”);
Map Map=newhashmap();
for(字符串参数:pspResonseArray){
字符串键=参数拆分(“=”[0];
字符串值=参数拆分(“=”[1];
map.put(键、值);
}
TextView txnStatus=(TextView)findViewById(R.id.editText10);
TextView txnId=(TextView)findViewById(R.id.editText11);
TextView txnRef=(TextView)findViewById(R.id.editText12);
TextView金额=(TextView)findViewById(R.id.editText13);
//隐藏布局1
RelativeLayout layout1=(RelativeLayout)findViewById(R.id.layout1);
RelativeLayout layout2=(RelativeLayout)findViewById(R.id.layout2);
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);
txnStatus.setText(map.get(“Status”);
txnId.setText(map.get(“txnId”);
txnRef.setText(map.get(“txnRef”);
金额。设定文本(“1”);
}否则{
Toast.makeText(这是“接收到空意图”,Toast.LENGTH_LONG.show();
}
}捕获(例外e){
e、 printStackTrace();
}
}

请帮忙。提前感谢:)

Hiii,首先,您使用的是UPI意向调用,而不是UPI sdk集成

下面的代码可能会对您有所帮助

Uri uri = new Uri.Builder()
                  .scheme("upi")
                  .authority("pay")
                  .appendQueryParameter("pa", payeeVpaAddress)
                  .appendQueryParameter("pn", payeeName)
                  .appendQueryParameter("tr", transactionRefId)
                  .appendQueryParameter("tn", description)
                  .appendQueryParameter("am", amount should be with fixed to 00, ex. 10.00)
                  .appendQueryParameter("cu", "INR")
                  .build();
final Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
Intent chooser = Intent.createChooser(intent, "Pay with...");
startActivityForResult(chooser, <<requestCode>>, null);
if (requestCode == <<requestCode>>) {
  if (data != null) {
    Bundle bundle = data.getExtras();
    String response = bundle.getString("response");
    String responseValues[] = getKeyValueFromString(response, "&");
    HashMap<String, String> keyValueOfResponse = new HashMap<>();
    for(String responseValue: responseValues) {
        String []keyValue = getKeyValueFromString(responseValue, "=");
        if(keyValue!= null && keyValue.length > 1) {
            keyValueOfResponse.put(keyValue[0], keyValue[1]);
        }
    }
    String txnRef = keyValueOfResponse.get("txnRef");
    String responseCode = keyValueOfResponse.get("responseCode");
    String Status = keyValueOfResponse.get("Status");
    String txnId = keyValueOfResponse.get("txnId");
  }
}
private String[] getKeyValueFromString(String stringToSplit, String splitBy) {
  if (stringToSplit == null) {
    return null;
  }
  return stringToSplit.split(splitBy);
}