android - 如何解决来自 UPI sdk 的未定义响应?

标签 android payment-gateway upi

我正在创建一个示例项目以通过用户获得付款,我成功地使用 pockets 进行了转账,但我收到的回复是不确定的。

txnId=undefined&responseCode=undefined&Status=undefined&txnRef=undefined

我准备的网址是: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();
        }
    }

请帮忙。提前致谢:)

最佳答案

您好,首先您使用的是 UPI intent 调用,而不是 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中添加下面的代码,先检查后面的结果是哪个request,可以用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);
}

关于android - 如何解决来自 UPI sdk 的未定义响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43347017/

相关文章:

javascript - 从 webapp 深度链接到 UPI 应用程序,例如 Google Pay

java - 如何在 Android 谷歌地图中制作自定义 Tiles

php - Paypal 与 Laravel 5.1 的集成

Firebase 支付网关?

security - 使用 UPI(特别是 GooglePay)验证付款是否实际完成

ios - 使用超链接集成 UPI 后如何获得响应

java - 我错过了一个java类吗?

android - 如何从 Android 应用程序的 Google 电子表格中读取数据

android - FCM (Firebase Cloud Messaging) 发送到多个设备

PHP PayPal 自适应支付 - 链式支付