I'm Trying in implement PayUCheckoutPro android SDK After successfully done with implementation part in android when I try to start payment by default onError() function is calling with error message
5014 Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
Points I've checked before asking this question are
Checked official doc to generate the hash to use the bar'|' correctly
checked this question for credential issue url
Added my files for more clarification
public class MainActivity extends AppCompatActivity {
String strKey = "gtKFFx";
String strSalt = "4R38IvwiV57FwVpsgOvTXBdLE4tHUXFW";
String TAG = "TAG123";
String txnid = "001", amount = "1.0", productinfo = "test", firstname = "shyam", email = "user@gmailcom",
user_credentials = "", udf1 = "1", udf2 = "1", udf3 = "1", udf4 = "1", udf5 = "1", offerKey = "0", cardBin = "0",
phone = "123654789";
String hashString = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
generateHash();
}
public void startPayment(View v) {
HashMap<String, Object> additionalParams = new HashMap<>();
additionalParams.put(PayUCheckoutProConstants.CP_UDF1, udf1);
additionalParams.put(PayUCheckoutProConstants.CP_UDF2, udf2);
additionalParams.put(PayUCheckoutProConstants.CP_UDF3, udf3);
additionalParams.put(PayUCheckoutProConstants.CP_UDF4, udf4);
additionalParams.put(PayUCheckoutProConstants.CP_UDF5, udf5);
PayUPaymentParams.Builder builder = new PayUPaymentParams.Builder();
builder .setKey(strKey1)
.setIsProduction(true)
.setTransactionId(txnid)
.setAmount(amount)
.setProductInfo(productinfo)
.setFirstName(firstname)
.setEmail(email)
//.setPhone(phone)
.setSurl("https://payuresponse.firebaseapp.com/success")
.setFurl("https://payuresponse.firebaseapp.com/failure");
PayUPaymentParams payUPaymentParams = builder.build();
PayUCheckoutPro.open(this, payUPaymentParams, new PayUCheckoutProListener() {
@Override
public void onPaymentSuccess(Object response) {
//Cast response object to HashMap
HashMap<String, Object> result = (HashMap<String, Object>) response;
String payuResponse = (String) result.get(PayUCheckoutProConstants.CP_PAYU_RESPONSE);
String merchantResponse = (String) result.get(PayUCheckoutProConstants.CP_MERCHANT_RESPONSE);
Log.e(TAG, "onPaymentSuccess: "+payuResponse );
}
@Override
public void onPaymentFailure(Object response) {
//Cast response object to HashMap
HashMap<String, Object> result = (HashMap<String, Object>) response;
String payuResponse = (String) result.get(PayUCheckoutProConstants.CP_PAYU_RESPONSE);
String merchantResponse = (String) result.get(PayUCheckoutProConstants.CP_MERCHANT_RESPONSE);
Log.e(TAG, "onPaymentFailure: "+payuResponse );
}
@Override
public void onPaymentCancel(boolean isTxnInitiated) {
Log.e(TAG, "onPaymentCancel: "+isTxnInitiated );
}
@Override
public void onError(@NonNull ErrorResponse errorResponse) {
Log.e(TAG, "onError: "+errorResponse.getErrorCode() );
Log.e(TAG, "onError: "+errorResponse.getErrorMessage() );
Toast.makeText(MainActivity.this, String.valueOf(errorResponse.getErrorMessage()), Toast.LENGTH_SHORT).show();
}
@Override
public void setWebViewProperties(@Nullable WebView webView, @Nullable Object o) {
Log.e(TAG, "setWebViewProperties: " );
//For setting webview properties, if any. Check Customized Integration section for more details on this
}
@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)) {
//Do not generate hash from local, it needs to be calculated from server side only. Here, hashString contains hash created from your server side.
String hash = hashString;
Log.e(TAG, "----: "+hash );
HashMap<String, String> dataMap = new HashMap<>();
dataMap.put(hashName, hash);
hashGenerationListener.onHashGenerated(dataMap);
}
}
});
}
void generateHash() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, "url", 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);
//param.put("phone", phone);
//param.put("user_credentials", user_credentials);
//param.put("udf1", udf1);
//param.put("udf2", udf2);
//param.put("udf3", udf3);
//param.put("udf4", udf4);
//param.put("udf5", udf5);
//param.put("offerKey", offerKey);
//param.put("cardBin", cardBin);
Log.e(TAG, "getParams: "+param );
return param;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
plugins {
id 'com.android.application'
}
android {
namespace 'com.shyam.payudemo'
compileSdk 32
defaultConfig {
applicationId "com.shyam.payudemo"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
implementation 'in.payu:payu-checkout-pro:2.0.2'//Pay-U-Money
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.5.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation 'com.android.volley:volley:1.2.1'//Volley
Payu-Money payment-gateway is one the secured and safest payment-gateway hence the implementation part is also a bit tougher as compare to other payment-gateway
To Solve the 5014 Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
.setIsProduction(false)
Detailed codes are available at my git-repository