Initiate the Payment

Initialize the PayUCheckoutPro SDK by passing the payment parameters prepared in the previous step and a reference to the transaction listener.

Java
Kotlin
Java
PayUCheckoutPro.open(
Activity activity,
PayUPaymentParams payUPaymentParams,
PayUCheckoutProListener payUCheckoutProListener)
Kotlin
PayUCheckoutPro.open(
activity: Activity,
payUPaymentParams: PayUPaymentParams,
payUCheckoutProListener: PayUCheckoutProListener)

Sample Code

Java
Kotlin
Java
PayUCheckoutPro.open(
this,
payUPaymentParams,
new PayUCheckoutProListener() {
@Override
public void onPaymentSuccess(@NotNull Object response) {
//Cast response object to HashMap
HashMap<String,Object> result = (HashMap<String, Object>) response
String payuResponse = result.get(PayUCheckoutProConstants.CP_PAYU_RESPONSE);
String merchantResponse = result.get(PayUCheckoutProConstants.CP_MERCHANT_RESPONSE));
}
@Override
public void onPaymentFailure(@NotNull Object response) {
//Cast response object to HashMap
HashMap<String,Object> result = (HashMap<String, Object>) response
String payuResponse = result.get(PayUCheckoutProConstants.CP_PAYU_RESPONSE);
String merchantResponse = result.get(PayUCheckoutProConstants.CP_MERCHANT_RESPONSE));
}
@Override
public void onPaymentCancel(boolean isTxnInitiated) {
}
@Override
public void onError(@NotNull ErrorResponse errorResponse) {
String errorMessage = errorResponse.getErrorMessage();
}
@Override
public void generateHash(@NotNull HashMap<String, String> valueMap, @NotNull PayUHashGenerationListener hashGenerationListener) {
String hashName = valueMap.get(CP_HASH_NAME);
String hashData = valueMap.get(CP_HASH_STRING);
if (!TextUtils.isEmpty(hashName) && !TextUtils.isEmpty(hashData)) {
//Generate Hash from your backend here
String hash = HashGenerationUtils.INSTANCE.generateHashFromSDK(hashData, salt);
HashMap<String, String> dataMap = new HashMap<>();
dataMap.put(hashName, hash);
hashGenerationListener.onHashGenerated(dataMap);
}
}
}
}
);
Kotlin
PayUCheckoutPro.open(
this, payUPaymentParams,
object : PayUCheckoutProListener {
override fun onPaymentSuccess(response: Any) {
response as HashMap<*, *>
val payUResponse = response[PayUCheckoutProConstants.CP_PAYU_RESPONSE]
val merchantResponse = response[PayUCheckoutProConstants.CP_MERCHANT_RESPONSE]
}
override fun onPaymentFailure(response: Any) {
response as HashMap<*, *>
val payUResponse = response[PayUCheckoutProConstants.CP_PAYU_RESPONSE]
val merchantResponse = response[PayUCheckoutProConstants.CP_MERCHANT_RESPONSE]
}
override fun onPaymentCancel(isTxnInitiated:Boolean) {
}
override fun onError(errorResponse: ErrorResponse) {
val errorMessage: String
if (errorResponse != null && errorResponse.errorMessage != null && errorResponse.errorMessage!!.isNotEmpty())
errorMessage = errorResponse.errorMessage!!
else
errorMessage = resources.getString(R.string.some_error_occurred)
}
override fun generateHash(
valueMap: HashMap<String, String?>,
hashGenerationListener: PayUHashGenerationListener
) {
if ( valueMap.containsKey(CP_HASH_STRING)
&& valueMap.containsKey(CP_HASH_STRING) != null
&& valueMap.containsKey(CP_HASH_NAME)
&& valueMap.containsKey(CP_HASH_NAME) != null) {
val hashData = valueMap[CP_HASH_STRING]
val hashName = valueMap[CP_HASH_NAME]
val hash: String? =
HashGenerationUtils.generateHashFromSDK(hashData!!, salt)
if (!TextUtils.isEmpty(hash)) {
val dataMap: HashMap<String, String?> = HashMap()
dataMap[hashName!!] = hash!!
hashGenerationListener.onHashGenerated(dataMap)
}
}
}
})