You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PayUCheckoutPro Android SDK集成启动支付触发5014错误求助

PayUCheckoutPro Android SDK集成报错:5014 无法将String转换为JSONObject

问题现象

完成SDK集成后启动支付,直接触发onError()方法,报错信息:

5014 Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

已完成排查

  • 核对官方文档,确认哈希生成时已正确使用分隔符|
  • 排查凭证相关问题

问题分析与修复方案

1. 核心问题:哈希请求URL无效

generateHash()方法中请求URL写为"url",导致请求返回HTML错误页面(如404),而非预期的JSON响应——这就是报错中<!DOCTYPE的来源,解析HTML字符串为JSONObject必然失败。
修复:替换为你的服务器端生成哈希的真实接口地址。

2. 变量名错误:未定义的strKey1

PayUPaymentParams.Builder中调用.setKey(strKey1),但代码里定义的密钥变量是strKey,会导致PayU服务器无法识别凭证。
修复:改为.setKey(strKey)。

3. 邮箱格式错误

email = "user@gmailcom"缺少.,正确格式应为user@gmail.com,格式错误会触发PayU接口校验失败。

4. 哈希生成逻辑缺陷

当前仅提前获取一次payment_hash,但PayU支付流程中可能需要多种动态哈希(如verify_vault_hash等),固定哈希无法满足需求。
修复:在generateHash()回调中,根据valueMap传递的哈希数据,实时请求服务器生成对应哈希并返回。


修正后的关键代码示例

修复哈希请求URL

void generateHash() {
    // 替换为你的真实服务器哈希接口地址
    StringRequest stringRequest = new StringRequest(Request.Method.POST, "https://your-server-domain/generate-hash", response -> {
        Log.e(TAG, "generateHash: " + response);
        try {
            JSONObject jsonObject = new JSONObject(response);
            hashString = jsonObject.getString("payment_hash");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }, error -> {
        Log.e(TAG, "generateHash: " + error.toString());
    }) {
        @Nullable
        @Override
        protected Map<String, String> getParams() {
            HashMap<String, String> param = new HashMap<>();
            param.put("txnid", txnid);
            param.put("amount", amount);
            param.put("productinfo", productinfo);
            param.put("firstname", firstname);
            param.put("email", email);
            Log.e(TAG, "getParams: "+param );
            return param;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

修复密钥参数与邮箱格式

// 修正邮箱格式
String email = "user@gmail.com";

// ...

public void startPayment(View v) {
    // ...
    PayUPaymentParams.Builder builder = new PayUPaymentParams.Builder();
    builder .setKey(strKey) // 替换strKey1为strKey
            .setIsProduction(true)
            // 其他参数保持不变

优化动态哈希生成逻辑

@Override
public void generateHash(HashMap<String, String> valueMap, PayUHashGenerationListener hashGenerationListener) {
    String hashName = valueMap.get(PayUCheckoutProConstants.CP_HASH_NAME);
    String hashData = valueMap.get(PayUCheckoutProConstants.CP_HASH_STRING);
    if (!TextUtils.isEmpty(hashName) && !TextUtils.isEmpty(hashData)) {
        // 实时请求服务器生成对应哈希
        StringRequest hashRequest = new StringRequest(Request.Method.POST, "https://your-server-domain/generate-dynamic-hash",
                response -> {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        String hash = jsonObject.getString(hashName);
                        HashMap<String, String> dataMap = new HashMap<>();
                        dataMap.put(hashName, hash);
                        hashGenerationListener.onHashGenerated(dataMap);
                    } catch (Exception e) {
                        e.printStackTrace();
                        hashGenerationListener.onHashGenerationFailed(e.getMessage());
                    }
                },
                error -> {
                    hashGenerationListener.onHashGenerationFailed(error.getMessage());
                }) {
            @Nullable
            @Override
            protected Map<String, String> getParams() {
                HashMap<String, String> param = new HashMap<>();
                param.put("hash_data", hashData);
                param.put("hash_name", hashName);
                return param;
            }
        };
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        requestQueue.add(hashRequest);
    }
}

额外注意事项

  • 测试环境下需将setIsProduction(true)改为false,使用PayU提供的测试密钥与盐值。
  • 服务器端哈希生成需严格遵循PayU规范:使用SHA-512加密,所有参与计算的参数用|分隔,末尾拼接盐值。

内容的提问来源于stack exchange,提问作者Shyam Sunder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 19:51:02