我正在尝试将 google pay 集成到我的 android 应用程序中。问题是完成交易后,谷歌支付应用程序再次重定向到继续支付页面,没有显示确认。如果交易成功,onActivityResult 也不会执行,否则如果我取消付款,它会正常工作(显示吐司“用户取消付款。”)。请让我知道我在这里做错了什么。任何帮助,将不胜感激!提前致谢。
final int UPI_PAYMENT=0;
void payUsingUpi( String name,String upiId, String note, String amount) {
Log.e("main ", "name "+name +"--up--"+upiId+"--"+ note+"--"+amount);
Uri uri = Uri.parse("upi://pay").buildUpon()
.appendQueryParameter("pa", upiId)
.appendQueryParameter("pn", name)
.appendQueryParameter("mc", "")
//.appendQueryParameter("tid", "02125412")
//.appendQueryParameter("tr", "25584584")
.appendQueryParameter("tn", note)
.appendQueryParameter("am", amount)
.appendQueryParameter("cu", "INR")
//.appendQueryParameter("refUrl", "blueapp")
.build();
Intent upiPayIntent = new Intent(Intent.ACTION_VIEW);
upiPayIntent.setData(uri);
// will always show a dialog to user to choose an app
Intent chooser = Intent.createChooser(upiPayIntent, "Pay with");
// check if intent resolves
if(null != chooser.resolveActivity(getPackageManager())) {
startActivityForResult(chooser, UPI_PAYMENT);
} else {
Utils.T(getApplicationContext(),"No UPI app found, please install one to continue");
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("main ", "response "+resultCode );
/*
E/main: response -1
E/UPI: onActivityResult: txnId=AXI4a3428ee58654a938811812c72c0df45&responseCode=00&Status=SUCCESS&txnRef=922118921612
E/UPIPAY: upiPaymentDataOperation: txnId=AXI4a3428ee58654a938811812c72c0df45&responseCode=00&Status=SUCCESS&txnRef=922118921612
E/UPI: payment successfull: 922118921612
*/
switch (requestCode) {
case UPI_PAYMENT:
if ((RESULT_OK == resultCode) || (resultCode == 11)) {
if (data != null) {
String trxt = data.getStringExtra("response");
Log.e("UPI", "onActivityResult: " + trxt);
ArrayList<String> dataList = new ArrayList<>();
dataList.add(trxt);
upiPaymentDataOperation(dataList);
} else {
Log.e("UPI", "onActivityResult: " + "Return data is null");
ArrayList<String> dataList = new ArrayList<>();
dataList.add("nothing");
upiPaymentDataOperation(dataList);
}
} else {
//when user simply back without payment
Log.e("UPI", "onActivityResult: " + "Return data is null");
ArrayList<String> dataList = new ArrayList<>();
dataList.add("nothing");
upiPaymentDataOperation(dataList);
}
break;
}
}
private void upiPaymentDataOperation(ArrayList<String> data) {
if (isConnectionAvailable(getApplicationContext())) {
String str = data.get(0);
Log.e("UPIPAY", "upiPaymentDataOperation: "+str);
String paymentCancel = "";
if(str == null) str = "discard";
String status = "";
String approvalRefNo = "";
String response[] = str.split("&");
for (int i = 0; i < response.length; i++) {
String equalStr[] = response[i].split("=");
if(equalStr.length >= 2) {
if (equalStr[0].toLowerCase().equals("Status".toLowerCase())) {
status = equalStr[1].toLowerCase();
}
else if (equalStr[0].toLowerCase().equals("ApprovalRefNo".toLowerCase()) || equalStr[0].toLowerCase().equals("txnRef".toLowerCase())) {
approvalRefNo = equalStr[1];
}
}
else {
paymentCancel = "Payment cancelled by user.";
}
}
if (status.equals("success")) {
//Code to handle successful transaction here.
Utils.T(getApplicationContext(),"Transaction successful.");
paymentstatus= "COMPLETED";
updateUPIPaymentStatus("UPI","PAID",approvalRefNo);
Log.e("UPI", "payment successfull: "+approvalRefNo);
}
else if("Payment cancelled by user.".equals(paymentCancel)) {
Utils.T(getApplicationContext(),"Payment cancelled by user.");
txFailed( "Payment cancelled by user." );
Log.e("UPI", "Cancelled by user: "+approvalRefNo);
Utils.SPadd( getApplicationContext(),"back" ,"googlepay");
}
else {
Utils.T(getApplicationContext(),"Transaction failed.Please try again");
txFailed( "Transaction failed.Please try again" );
Log.e("UPI", "failed payment: "+approvalRefNo);
}
} else {
Utils.T(getApplicationContext(),"Internet issue:");
txFailed( "Internet issue:" );
Log.e("UPI", "Internet issue: ");
}
}