PayUBiz - Android:由于哈希参数计算不正确,交易失败
PayUBiz - Android: Transaction failed due to incorrectly calculated hash parameter
我正在尝试将 PayUBiz 集成到我的 Android 应用程序中。它在测试环境中运行良好。
我用的是下面这样的。
- 测试商家密钥/盐:gtKFFx / eCwWELxi
- 我们的服务器 URL 用于生成哈希:http://xx.xxx.xx.xx/payment/getPaymentData
- 成功URL-失败URL:https://payu.herokuapp.com/success - https://payu.herokuapp.com/failure
- 我在我们的服务器 URL 中传递 orderId 和 userId 以生成哈希。
我可以转到可以输入测试卡详细信息的屏幕。但是在输入卡详细信息后,我得到 "Error Reason: Transaction failed due to incorrectly calculated hash parameter"
完整的错误截图如下。
我在代码中所做的如下。
ActivityConfirmOrder.java
private String merchantKey = "gtKFFx";
private String merchantSalt = "eCwWELxi";
private String userCredentials = merchantKey + ":" + "maulik@techniche.co";
private PayuConfig payuConfig;
private PaymentParams mPaymentParams;
我在 onCreate 中输入
// PayUBiz initialisation
Payu.setInstance(this);
以下方法不在 onCreate 方法中。
private void makePayment() {
int environment = PayuConstants.STAGING_ENV;
sharedPref = new UserSharedPref(this);
mPaymentParams = new PaymentParams();
mPaymentParams.setKey(merchantKey);
mPaymentParams.setAmount(String.valueOf(totalPrice));
mPaymentParams.setProductInfo("product_info");
mPaymentParams.setFirstName("Maulik");
mPaymentParams.setEmail("maulik@techniche.co");
mPaymentParams.setTxnId(OrderNumber);
mPaymentParams.setSurl("https://payu.herokuapp.com/success");
mPaymentParams.setFurl("https://payu.herokuapp.com/failure");
mPaymentParams.setUdf1("");
mPaymentParams.setUdf2("");
mPaymentParams.setUdf3("");
mPaymentParams.setUdf4("");
mPaymentParams.setUdf5("");
mPaymentParams.setUserCredentials(userCredentials);
payuConfig = new PayuConfig();
payuConfig.setEnvironment(environment);
generatePayUHashFromServer(mPaymentParams);
}
private void generatePayUHashFromServer(PaymentParams mPaymentParams) {
StringBuffer postParamsBuffer = new StringBuffer();
postParamsBuffer.append(concatParams(PayuConstants.KEY, mPaymentParams.getKey()));
postParamsBuffer.append(concatParams(PayuConstants.AMOUNT, mPaymentParams.getAmount()));
postParamsBuffer.append(concatParams(PayuConstants.TXNID, mPaymentParams.getTxnId()));
postParamsBuffer.append(concatParams(PayuConstants.EMAIL, null == mPaymentParams.getEmail() ? "" : mPaymentParams.getEmail()));
postParamsBuffer.append(concatParams(PayuConstants.PRODUCT_INFO, mPaymentParams.getProductInfo()));
postParamsBuffer.append(concatParams(PayuConstants.FIRST_NAME, null == mPaymentParams.getFirstName() ? "" : mPaymentParams.getFirstName()));
postParamsBuffer.append(concatParams(PayuConstants.UDF1, mPaymentParams.getUdf1() == null ? "" : mPaymentParams.getUdf1()));
postParamsBuffer.append(concatParams(PayuConstants.UDF2, mPaymentParams.getUdf2() == null ? "" : mPaymentParams.getUdf2()));
postParamsBuffer.append(concatParams(PayuConstants.UDF3, mPaymentParams.getUdf3() == null ? "" : mPaymentParams.getUdf3()));
postParamsBuffer.append(concatParams(PayuConstants.UDF4, mPaymentParams.getUdf4() == null ? "" : mPaymentParams.getUdf4()));
postParamsBuffer.append(concatParams(PayuConstants.UDF5, mPaymentParams.getUdf5() == null ? "" : mPaymentParams.getUdf5()));
postParamsBuffer.append(concatParams(PayuConstants.USER_CREDENTIALS, mPaymentParams.getUserCredentials() == null ? PayuConstants.DEFAULT : mPaymentParams.getUserCredentials()));
if (null != mPaymentParams.getOfferKey())
postParamsBuffer.append(concatParams(PayuConstants.OFFER_KEY, mPaymentParams.getOfferKey()));
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("orderId", orderId);
jsonObject.put("userId", sharedPref.getUserId());
} catch (JSONException e) {
e.printStackTrace();
}
// String postParams = jsonObject.toString();
// String postParams = postParamsBuffer.charAt(postParamsBuffer.length() - 1) == '&' ? postParamsBuffer.substring(0, postParamsBuffer.length() - 1).toString() : postParamsBuffer.toString();
GetHashesFromServerTask getHashesFromServerTask = new GetHashesFromServerTask();
getHashesFromServerTask.execute(jsonObject);
}
protected String concatParams(String key, String value) {
return key + "=" + value + "&";
}
private class GetHashesFromServerTask extends AsyncTask<JSONObject, String, PayuHashes> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(ActivityConfirmOrder.this);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected PayuHashes doInBackground(JSONObject... postParams) {
PayuHashes payuHashes = new PayuHashes();
try {
URL url = new URL(AppConstant.BASE_URL + "/payment/getPaymentData");
String postParam = postParams[0].toString();
byte[] postParamsByte = postParam.getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
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) {
case "payment_hash":
payuHashes.setPaymentHash(response.getString(key));
break;
case "vas_for_mobile_sdk_hash":
payuHashes.setVasForMobileSdkHash(response.getString(key));
break;
case "payment_related_details_for_mobile_sdk_hash":
payuHashes.setPaymentRelatedDetailsForMobileSdkHash(response.getString(key));
break;
case "delete_user_card_hash":
payuHashes.setDeleteCardHash(response.getString(key));
break;
case "get_user_cards_hash":
payuHashes.setStoredCardsHash(response.getString(key));
break;
case "edit_user_card_hash":
payuHashes.setEditCardHash(response.getString(key));
break;
case "save_user_card_hash":
payuHashes.setSaveCardHash(response.getString(key));
break;
case "check_offer_status_hash":
payuHashes.setCheckOfferStatusHash(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 payuHashes;
}
@Override
protected void onPostExecute(PayuHashes payuHashes) {
super.onPostExecute(payuHashes);
progressDialog.dismiss();
launchSdkUI(payuHashes);
}
}
public void launchSdkUI(PayuHashes payuHashes) {
Intent intent = new Intent(ActivityConfirmOrder.this, PayUBaseActivity.class);
intent.putExtra(PayuConstants.PAYU_CONFIG, payuConfig);
intent.putExtra(PayuConstants.PAYMENT_PARAMS, mPaymentParams);
intent.putExtra(PayuConstants.PAYU_HASHES, payuHashes);
intent.putExtra(PayuConstants.SALT, merchantSalt);
intent.putExtra("PaymentType", "PAYU");
startActivityForResult(intent, PayuConstants.PAYU_REQUEST_CODE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PayuConstants.PAYU_REQUEST_CODE) {
if (data != null) {
Log.e("PayuResponse", data.getStringExtra("payu_response"));
if (!data.getStringExtra("payu_response").equals("")) {
PayUSuccessRequest request = new PayUSuccessRequest(ActivityConfirmOrder.this);
try {
JSONObject paySuccessRes = new JSONObject(data.getStringExtra("payu_response"));
request.setPayUSuccessResJsonObject(paySuccessRes);
} catch (JSONException e) {
e.printStackTrace();
}
new AsyncTaskExecutor<A2BRequest, Void, A2BResponse>().execute(
new RequestProcessor(ActivityConfirmOrder.this, ActivityConfirmOrder.this, true), request);
}
try {
JSONObject responseObject = new JSONObject(data.getStringExtra("payu_response"));
if (responseObject != null) {
if (responseObject.optString("status").equalsIgnoreCase("failure")) {
Toast.makeText(mContext, "Failure..", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ActivityConfirmOrder.this, ActivityOrderFailure.class);
ActivityConfirmOrder.this.startActivity(intent);
} else {
//Toast.makeText(getActivity(), getString(R.string.could_not_receive_data), Toast.LENGTH_LONG).show();
}
} else {
//Toast.makeText(getActivity(), getString(R.string.could_not_receive_data), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
//Toast.makeText(getActivity(), getString(R.string.could_not_receive_data), Toast.LENGTH_LONG).show();
}
} else {
//Toast.makeText(getActivity(), getString(R.string.could_not_receive_data), Toast.LENGTH_LONG).show();
}
} else {
Log.e("Log MSg", "No Payu SDK Request Code");
}
}
我找到了解决方案,但我还没有申请。
我给 PayUBiz 的客户支持发了邮件,他们的回复很好。
电子邮件 ID:tech@payu.in / mobile-integration@payu.in
正如我在问题中提到的,我正在使用 TEST ENVIRONMENT and TEST CREDENTIALS and OUR SERVER URL用于生成hash,这是我错的地方。如果您使用 TEST CREDENTIALS 和 YOUR SERVER URL 作为散列,它将不起作用。
在TEST ENVIRONMENT中使用所有TESTING RELATED东西,对于LIVE ENVIRONMENT使用所有现场相关 东西。
我用 PayUBiz 的 TEST URL [=10 替换了 OUR SERVER URL =] 它正在工作。
我正在尝试将 PayUBiz 集成到我的 Android 应用程序中。它在测试环境中运行良好。
我用的是下面这样的。
- 测试商家密钥/盐:gtKFFx / eCwWELxi
- 我们的服务器 URL 用于生成哈希:http://xx.xxx.xx.xx/payment/getPaymentData
- 成功URL-失败URL:https://payu.herokuapp.com/success - https://payu.herokuapp.com/failure
- 我在我们的服务器 URL 中传递 orderId 和 userId 以生成哈希。
我可以转到可以输入测试卡详细信息的屏幕。但是在输入卡详细信息后,我得到 "Error Reason: Transaction failed due to incorrectly calculated hash parameter"
完整的错误截图如下。
我在代码中所做的如下。
ActivityConfirmOrder.java
private String merchantKey = "gtKFFx";
private String merchantSalt = "eCwWELxi";
private String userCredentials = merchantKey + ":" + "maulik@techniche.co";
private PayuConfig payuConfig;
private PaymentParams mPaymentParams;
我在 onCreate 中输入
// PayUBiz initialisation
Payu.setInstance(this);
以下方法不在 onCreate 方法中。
private void makePayment() {
int environment = PayuConstants.STAGING_ENV;
sharedPref = new UserSharedPref(this);
mPaymentParams = new PaymentParams();
mPaymentParams.setKey(merchantKey);
mPaymentParams.setAmount(String.valueOf(totalPrice));
mPaymentParams.setProductInfo("product_info");
mPaymentParams.setFirstName("Maulik");
mPaymentParams.setEmail("maulik@techniche.co");
mPaymentParams.setTxnId(OrderNumber);
mPaymentParams.setSurl("https://payu.herokuapp.com/success");
mPaymentParams.setFurl("https://payu.herokuapp.com/failure");
mPaymentParams.setUdf1("");
mPaymentParams.setUdf2("");
mPaymentParams.setUdf3("");
mPaymentParams.setUdf4("");
mPaymentParams.setUdf5("");
mPaymentParams.setUserCredentials(userCredentials);
payuConfig = new PayuConfig();
payuConfig.setEnvironment(environment);
generatePayUHashFromServer(mPaymentParams);
}
private void generatePayUHashFromServer(PaymentParams mPaymentParams) {
StringBuffer postParamsBuffer = new StringBuffer();
postParamsBuffer.append(concatParams(PayuConstants.KEY, mPaymentParams.getKey()));
postParamsBuffer.append(concatParams(PayuConstants.AMOUNT, mPaymentParams.getAmount()));
postParamsBuffer.append(concatParams(PayuConstants.TXNID, mPaymentParams.getTxnId()));
postParamsBuffer.append(concatParams(PayuConstants.EMAIL, null == mPaymentParams.getEmail() ? "" : mPaymentParams.getEmail()));
postParamsBuffer.append(concatParams(PayuConstants.PRODUCT_INFO, mPaymentParams.getProductInfo()));
postParamsBuffer.append(concatParams(PayuConstants.FIRST_NAME, null == mPaymentParams.getFirstName() ? "" : mPaymentParams.getFirstName()));
postParamsBuffer.append(concatParams(PayuConstants.UDF1, mPaymentParams.getUdf1() == null ? "" : mPaymentParams.getUdf1()));
postParamsBuffer.append(concatParams(PayuConstants.UDF2, mPaymentParams.getUdf2() == null ? "" : mPaymentParams.getUdf2()));
postParamsBuffer.append(concatParams(PayuConstants.UDF3, mPaymentParams.getUdf3() == null ? "" : mPaymentParams.getUdf3()));
postParamsBuffer.append(concatParams(PayuConstants.UDF4, mPaymentParams.getUdf4() == null ? "" : mPaymentParams.getUdf4()));
postParamsBuffer.append(concatParams(PayuConstants.UDF5, mPaymentParams.getUdf5() == null ? "" : mPaymentParams.getUdf5()));
postParamsBuffer.append(concatParams(PayuConstants.USER_CREDENTIALS, mPaymentParams.getUserCredentials() == null ? PayuConstants.DEFAULT : mPaymentParams.getUserCredentials()));
if (null != mPaymentParams.getOfferKey())
postParamsBuffer.append(concatParams(PayuConstants.OFFER_KEY, mPaymentParams.getOfferKey()));
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("orderId", orderId);
jsonObject.put("userId", sharedPref.getUserId());
} catch (JSONException e) {
e.printStackTrace();
}
// String postParams = jsonObject.toString();
// String postParams = postParamsBuffer.charAt(postParamsBuffer.length() - 1) == '&' ? postParamsBuffer.substring(0, postParamsBuffer.length() - 1).toString() : postParamsBuffer.toString();
GetHashesFromServerTask getHashesFromServerTask = new GetHashesFromServerTask();
getHashesFromServerTask.execute(jsonObject);
}
protected String concatParams(String key, String value) {
return key + "=" + value + "&";
}
private class GetHashesFromServerTask extends AsyncTask<JSONObject, String, PayuHashes> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(ActivityConfirmOrder.this);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected PayuHashes doInBackground(JSONObject... postParams) {
PayuHashes payuHashes = new PayuHashes();
try {
URL url = new URL(AppConstant.BASE_URL + "/payment/getPaymentData");
String postParam = postParams[0].toString();
byte[] postParamsByte = postParam.getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
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) {
case "payment_hash":
payuHashes.setPaymentHash(response.getString(key));
break;
case "vas_for_mobile_sdk_hash":
payuHashes.setVasForMobileSdkHash(response.getString(key));
break;
case "payment_related_details_for_mobile_sdk_hash":
payuHashes.setPaymentRelatedDetailsForMobileSdkHash(response.getString(key));
break;
case "delete_user_card_hash":
payuHashes.setDeleteCardHash(response.getString(key));
break;
case "get_user_cards_hash":
payuHashes.setStoredCardsHash(response.getString(key));
break;
case "edit_user_card_hash":
payuHashes.setEditCardHash(response.getString(key));
break;
case "save_user_card_hash":
payuHashes.setSaveCardHash(response.getString(key));
break;
case "check_offer_status_hash":
payuHashes.setCheckOfferStatusHash(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 payuHashes;
}
@Override
protected void onPostExecute(PayuHashes payuHashes) {
super.onPostExecute(payuHashes);
progressDialog.dismiss();
launchSdkUI(payuHashes);
}
}
public void launchSdkUI(PayuHashes payuHashes) {
Intent intent = new Intent(ActivityConfirmOrder.this, PayUBaseActivity.class);
intent.putExtra(PayuConstants.PAYU_CONFIG, payuConfig);
intent.putExtra(PayuConstants.PAYMENT_PARAMS, mPaymentParams);
intent.putExtra(PayuConstants.PAYU_HASHES, payuHashes);
intent.putExtra(PayuConstants.SALT, merchantSalt);
intent.putExtra("PaymentType", "PAYU");
startActivityForResult(intent, PayuConstants.PAYU_REQUEST_CODE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PayuConstants.PAYU_REQUEST_CODE) {
if (data != null) {
Log.e("PayuResponse", data.getStringExtra("payu_response"));
if (!data.getStringExtra("payu_response").equals("")) {
PayUSuccessRequest request = new PayUSuccessRequest(ActivityConfirmOrder.this);
try {
JSONObject paySuccessRes = new JSONObject(data.getStringExtra("payu_response"));
request.setPayUSuccessResJsonObject(paySuccessRes);
} catch (JSONException e) {
e.printStackTrace();
}
new AsyncTaskExecutor<A2BRequest, Void, A2BResponse>().execute(
new RequestProcessor(ActivityConfirmOrder.this, ActivityConfirmOrder.this, true), request);
}
try {
JSONObject responseObject = new JSONObject(data.getStringExtra("payu_response"));
if (responseObject != null) {
if (responseObject.optString("status").equalsIgnoreCase("failure")) {
Toast.makeText(mContext, "Failure..", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ActivityConfirmOrder.this, ActivityOrderFailure.class);
ActivityConfirmOrder.this.startActivity(intent);
} else {
//Toast.makeText(getActivity(), getString(R.string.could_not_receive_data), Toast.LENGTH_LONG).show();
}
} else {
//Toast.makeText(getActivity(), getString(R.string.could_not_receive_data), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
//Toast.makeText(getActivity(), getString(R.string.could_not_receive_data), Toast.LENGTH_LONG).show();
}
} else {
//Toast.makeText(getActivity(), getString(R.string.could_not_receive_data), Toast.LENGTH_LONG).show();
}
} else {
Log.e("Log MSg", "No Payu SDK Request Code");
}
}
我找到了解决方案,但我还没有申请。
我给 PayUBiz 的客户支持发了邮件,他们的回复很好。 电子邮件 ID:tech@payu.in / mobile-integration@payu.in
正如我在问题中提到的,我正在使用 TEST ENVIRONMENT and TEST CREDENTIALS and OUR SERVER URL用于生成hash,这是我错的地方。如果您使用 TEST CREDENTIALS 和 YOUR SERVER URL 作为散列,它将不起作用。
在TEST ENVIRONMENT中使用所有TESTING RELATED东西,对于LIVE ENVIRONMENT使用所有现场相关 东西。
我用 PayUBiz 的 TEST URL [=10 替换了 OUR SERVER URL =] 它正在工作。