首页 文章

在集成到android中时,PayUMoney支付网关中出现“发生了一些错误”

提问于
浏览
0

我已经验证了PayUMoney的帐户 . 在沙盒模式下单击paynow按钮时,它会显示"some error occurred" . 我用pnp sdk .

此代码用于onnlick on paynow按钮 .

payNowButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            userAddress = address_line1.get(0) + "," + address_line2.get(0) + "," + address_pincode.get(0)
                    + "," + address_taluka + "," + address_district + "," + address_state;
            if (payment_type.equals("online")) {
                launchPayUMoneyFlow();
            } else if (payment_type.equals("cod")) {
                launchNormalFlow();
            }

        }
    });

这是哈希计算功能

public static String hashCal(String str) {
    byte[] hashseq = str.getBytes();
    StringBuilder hexString = new StringBuilder();
    try {
        MessageDigest algorithm = MessageDigest.getInstance("SHA-512");
        algorithm.reset();
        algorithm.update(hashseq);
        byte messageDigest[] = algorithm.digest();
        for (byte aMessageDigest : messageDigest) {
            String hex = Integer.toHexString(0xFF & aMessageDigest);
            if (hex.length() == 1) {
                hexString.append("0");
            }
            hexString.append(hex);
        }
    } catch (NoSuchAlgorithmException ignored) {
    }
    return hexString.toString();
}

从PayUMoney收到回复后,将调用以下函数

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result Code is -1 send from Payumoney activity
    Log.d("MainActivity", "request code " + requestCode + " resultcode " + resultCode);
    if (requestCode == PayUmoneyFlowManager.REQUEST_CODE_PAYMENT && resultCode == RESULT_OK && data !=
            null) {
        TransactionResponse transactionResponse = data.getParcelableExtra(PayUmoneyFlowManager
                .INTENT_EXTRA_TRANSACTION_RESPONSE);

        ResultModel resultModel = data.getParcelableExtra(PayUmoneyFlowManager.ARG_RESULT);

        // Check which object is non-null
        if (transactionResponse != null && transactionResponse.getPayuResponse() != null) {
            if (transactionResponse.getTransactionStatus().equals(TransactionResponse.TransactionStatus.SUCCESSFUL)) {
                //Success Transaction
            } else {
                //Failure Transaction
            }

            // Response from Payumoney
            String payuResponse = transactionResponse.getPayuResponse();

            // Response from SURl and FURL
            String merchantResponse = transactionResponse.getTransactionDetails();

            new AlertDialog.Builder(this)
                    .setCancelable(false)
                    .setMessage("Payu's Data : " + payuResponse + "\n\n\n Merchant's Data: " + merchantResponse)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.dismiss();
                        }
                    }).show();

        } else if (resultModel != null && resultModel.getError() != null) {
            Log.d(TAG, "Error response : " + resultModel.getError().getTransactionResponse());
        } else {
            Log.d(TAG, "Both objects are null!");
        }
    }
}

LaunchPayUMoneyFlow是所有处理都发生的功能 .

String txnId="";

private void launchPayUMoneyFlow() {

    PayUmoneyConfig payUmoneyConfig = PayUmoneyConfig.getInstance();

    //Use this to set your custom text on result screen button
    payUmoneyConfig.setDoneButtonText("");

    //Use this to set your custom title for the activity
    payUmoneyConfig.setPayUmoneyActivityTitle("TEST GATEWAY");

    PayUmoneySdkInitializer.PaymentParam.Builder builder = new PayUmoneySdkInitializer.PaymentParam.Builder();

    try {
        amount = Double.parseDouble(final_total);

    } catch (Exception e) {
        e.printStackTrace();
    }
    txnId = System.currentTimeMillis() + "";
    String phone = userMobile;
    String productName = product_name;
    String firstName = userName;
    String email = "mayurdusane1@gmail.com";
    String udf1 = order_id;
    String udf2 = billingInfo;
    String udf3 = userAddress;
    String udf4 = userUID;
    String udf5 = request_time;
    String udf6 = "";
    String udf7 = "";
    String udf8 = "";
    String udf9 = "";
    String udf10 = "";

    AppEnvironment appEnvironment = AppEnvironment.SANDBOX;
    builder.setAmount(amount)
            .setTxnId(txnId)
            .setPhone(phone)
            .setProductName(productName)
            .setFirstName(firstName)
            .setEmail(email)
            .setsUrl(appEnvironment.surl())
            .setfUrl(appEnvironment.furl())
            .setUdf1(udf1)
            .setUdf2(udf2)
            .setUdf3(udf3)
            .setUdf4(udf4)
            .setUdf5(udf5)
            .setUdf6(udf6)
            .setUdf7(udf7)
            .setUdf8(udf8)
            .setUdf9(udf9)
            .setUdf10(udf10)
            .setIsDebug(false)
            .setKey("KEYHERE")
            .setMerchantId("MERCHANTIDHERE");

    try {
        mPaymentParams = builder.build();

generateHashFromServer(mPaymentParams);

} catch (Exception e) {
        // some exception occurred
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        payNowButton.setEnabled(true);
    }
}


/**
 * This method generates hash from server.
 *
 * @param paymentParam payments params used for hash generation
 */
public void generateHashFromServer(PayUmoneySdkInitializer.PaymentParam paymentParam) {
    RequestQueue queue1 = Volley.newRequestQueue(this);
    String url = "URL GOES HERE"; // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    JSONArray dataArray;
                    JSONObject jsonObject;
                    String merchantHash="";

                    try {
                        jsonObject = new JSONObject(response);
                        //dataArray = jsonObject.getJSONArray(JSON_ARRAY);
                        //Toast.makeText(getApplicationContext(), "m" + jsonObject.getString("result"), Toast.LENGTH_SHORT).show();
                        merchantHash = jsonObject.getString("result");

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    //setting up response values to the fragment
                    if (merchantHash.isEmpty() || merchantHash.equals("")) {
                        Toast.makeText(FinalCheckoutActivity.this, "Could not generate hash", Toast.LENGTH_SHORT).show();
                    } else {
                        mPaymentParams.setMerchantHash(merchantHash);
                        //Toast.makeText(FinalCheckoutActivity.this, "m:"+mPaymentParams.getParams(), Toast.LENGTH_SHORT).show();
                        //Log.e(TAG, "onPostExecute: "+mPaymentParams.getParams() );
                        PayUmoneyFlowManager.startPayUMoneyFlow(mPaymentParams, FinalCheckoutActivity.this, -1, false);
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplication(), "Error:" + error, Toast.LENGTH_LONG).show();
        }

    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put(PayUmoneyConstants.KEY,"KEYHERE" );
            params.put(PayUmoneyConstants.AMOUNT,amount+"" );
            params.put(PayUmoneyConstants.TXNID,txnId);
            params.put(PayUmoneyConstants.EMAIL,userEmail);
            params.put(PayUmoneyConstants.PRODUCT_INFO,product_name);
            params.put(PayUmoneyConstants.FIRSTNAME,userName);
            params.put(PayUmoneyConstants.UDF1,order_id );
            params.put(PayUmoneyConstants.UDF2,billingInfo );
            params.put(PayUmoneyConstants.UDF3,userAddress);
            params.put(PayUmoneyConstants.UDF4,userUID);
            params.put(PayUmoneyConstants.UDF5,request_time);

            return params;
        }
    };
    queue1.add(stringRequest);
}

执行流程:

选择在线支付后,

  • 用户点击支付按钮作为方法 .

  • launchPayUMoneyFlow()函数被执行

  • 所有参数附加到PayUmoneySdkInitializer.PaymentParam.Builder

  • 然后从服务器生成哈希并附加PayUmoneySdkInitializer.PaymentParam.Builder
    成功创建哈希后

  • 将执行PayUmoneyFlowManager.startPayUMoneyFlow() .

执行PayUmoneyFlowManager.startPayUMoneyFlow()后,它显示“发生了一些错误”

哈希生成脚本

<?php
$key=$_POST["key"];
$salt="SALTHERE";
$txnId=$_POST["txnid"];
$amount=$_POST["amount"];
$productName=$_POST["productInfo"];
$firstName=$_POST["firstName"];
$email=$_POST["email"];
$udf1=$_POST["udf1"];
$udf2=$_POST["udf2"];
$udf3=$_POST["udf3"];
$udf4=$_POST["udf4"];
$udf5=$_POST["udf5"];

$payhash_str = $key . '|' . checkNull($txnId) . '|' .checkNull($amount)  . '|' .checkNull($productName)  . '|' . checkNull($firstName) . '|' . checkNull($email) . '|' . checkNull($udf1) . '|' . checkNull($udf2) . '|' . checkNull($udf3) . '|' . checkNull($udf4) . '|' . checkNull($udf5) . '|||||||'. $salt;


function checkNull($value) {
            if ($value == null) {
                  return '';
            } else {
                  return $value;
            }
      }
$hash = strtolower(hash('sha512', $payhash_str));
$arr['result'] = $hash;
$arr['status']=0;
$arr['errorCode']=null;
$arr['responseCode']=null;
$output=$arr;

echo json_encode($output);
?>

注意:哈希生成脚本是从PayUMoney技术团队获得的 . 我在stackoverflow和github上提到了几乎所有的问题 . 仍然得到这个错误 . 我提到的几个来源 .

2 回答

  • 0

    PayU SDK应该用正确的错误消息替换“发生一些错误” .

    这个问题是因为你的PayUServer的 hash is mismatched .

    并且由于|的数量不匹配 .

    尝试在Php文件中替换它:

    . checkNull($udf5) . '||||||'. $salt
    
  • 5

    迟到的答案,但希望这将有助于仍在寻找解决方案的其他人 .

    不要更改PayU团队给你的代码 .

    对于 生产环境 ,

    • 只需在 AppPreference.java 文件中放入一些有效的详细信息 .

    • 现在在服务器 php script 中用 production salt = "xxxxxxx" 值替换 salt 值 .

    • 并且,按以下格式替换 Hash 生成格式:

    这会奏效 .

    $payhash_str = $key . '|' . checkNull($txnId) . '|' .checkNull($amount) . '|' .checkNull($productName) . '|' . checkNull($firstName) . '|' . checkNull($email) . '|' . checkNull($udf1) . '|' . checkNull($udf2) . '|' . checkNull($udf3) . '|' . checkNull($udf4) . '|' . checkNull($udf5) . '||||||'. $salt;
    

相关问题