我正在创建一个示例项目以通过用户付款,并且我使用口袋成功转移它,但我收到的响应是未定义的。
txnId =未定义&安培; responseCode =未定义&安培;状态=未定义&安培; txnRef =未定义
我准备好的网址是:upi:// pay?pn = john& tr = 598& am = 1& cu = INR& pa = 9469731359 @ upi
使用此示例代码
@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();
}
}
请帮忙。在此先感谢:)
答案 0 :(得分:6)
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);
在onActivityResult中添加以下代码,并首先检查结果后面的请求,您可以使用requestCode检查
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);
}