androidandroid-studiopayumoney

Some error occurred message in payUmoney integration after generating successfully Hashkey


I was trying to integrate payment gateway of payUmoney. I was following this method Hash key was successfully generated but still i am not able to go to payment page using live credential. I am using this Code for generating the HashKey--

<?php
/**
 * Created by PhpStorm.
 * User: Mayur Dusane
 * Date: 21-12-2017
 * Time: 11:27
 */
/**************
Below is the test card details for doing a test transaction in the testing mode.
Card No - 5123456789012346
Expiry - 05/2020
CVV - 123
****************/
/***************** NECESSARY FIELDS GOES HERE ***********************/
$key=$_POST["key"]; //posted merchant key from client
$salt="e5iIg1jwi8"; // add salt here from your credentials in payUMoney dashboard
$txnId=$_POST["txnid"]; //posted txnid from client
$amount=$_POST["amount"]; //posted amount from client 
$productName=$_POST["productInfo"]; // posted product info from client
$firstName=$_POST["firstName"]; // posted firstname from and must be without space
$email=$_POST["email"]; // posted email from client
/***************** USER DEFINED VARIABLES GOES HERE ***********************/
//all varibles posted from client
$udf1=$_POST["udf1"];
$udf2=$_POST["udf2"];
$udf3=$_POST["udf3"];
$udf4=$_POST["udf4"];
$udf5=$_POST["udf5"];
/***************** DO NOT EDIT ***********************/
$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));
/***************** DO NOT EDIT ***********************/
$arr['result'] = $hash;
$arr['status']=0;
$arr['errorCode']=null;
$arr['responseCode']=null;
$output=$arr;
echo json_encode($output);
?>

when i was sending this hash key generated from above code using this method:

  private class GetHashesFromServerTask extends AsyncTask<String, String, String> {
    private ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Please wait...");
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... postParams) {

        String merchantHash = "";
        try {
            //TODO Below url is just for testing purpose, merchant needs to replace this with their server side hash generation url
            URL url = new URL("https://payu.herokuapp.com/get_hash");

            String postParam = postParams[0];

            byte[] postParamsByte = postParam.getBytes("UTF-8");

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", String.valueOf(postParamsByte.length));
            conn.setDoOutput(true);
            conn.getOutputStream().write(postParamsByte);

            InputStream responseInputStream = conn.getInputStream();
            StringBuffer responseStringBuffer = new StringBuffer();
            byte[] byteContainer = new byte[1024];
            for (int i; (i = responseInputStream.read(byteContainer)) != -1; ) {
                responseStringBuffer.append(new String(byteContainer, 0, i));
            }

            JSONObject response = new JSONObject(responseStringBuffer.toString());

            Iterator<String> payuHashIterator = response.keys();
            while (payuHashIterator.hasNext()) {
                String key = payuHashIterator.next();
                switch (key) {
                    /**
                     * This hash is mandatory and needs to be generated from merchant's server side
                     *
                     */
                    case "payment_hash":
                        merchantHash = response.getString(key);
                        break;
                    default:
                        break;
                }
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return merchantHash;
    }

    @Override
    protected void onPostExecute(String merchantHash) {
        super.onPostExecute(merchantHash);

        progressDialog.dismiss();
        payNowButton.setEnabled(true);

        if (merchantHash.isEmpty() || merchantHash.equals("")) {
            Toast.makeText(MainActivity.this, "Could not generate hash", Toast.LENGTH_SHORT).show();
        } else {
            mPaymentParams.setMerchantHash(merchantHash);

            if (AppPreference.selectedTheme != -1) {
                PayUmoneyFlowManager.startPayUMoneyFlow(mPaymentParams, MainActivity.this, AppPreference.selectedTheme, mAppPreference.isOverrideResultScreen());
            } else {
                PayUmoneyFlowManager.startPayUMoneyFlow(mPaymentParams, MainActivity.this, R.style.AppTheme_default, mAppPreference.isOverrideResultScreen());
            }
        }
    }
}

Is the code is ok for generating hash key
Thanks in Andvance


Solution

  • Make sure your keys which you are sending to the payUmoney gateway are same as defined in PHP script hash generation

    for example: key for product name is productInfo make sure of each character is same.

    else the code is perfect this is only where you might have mistaken