Initiate the Payment
(Mandatory Step)

Step 4: 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
1
PayUCheckoutPro.open(
2
Activity activity,
3
PayUPaymentParams payUPaymentParams,
4
PayUCheckoutProListener payUCheckoutProListener)
Copied!
1
PayUCheckoutPro.open(
2
activity: Activity,
3
payUPaymentParams: PayUPaymentParams,
4
payUCheckoutProListener: PayUCheckoutProListener)
Copied!

Sample Code

Java
Kotlin
1
PayUCheckoutPro.open(
2
this,
3
payUPaymentParams,
4
new PayUCheckoutProListener() {
5
6
@Override
7
public void onPaymentSuccess(Object response) {
8
//Cast response object to HashMap
9
HashMap<String,Object> result = (HashMap<String, Object>) response;
10
String payuResponse = (String)result.get(PayUCheckoutProConstants.CP_PAYU_RESPONSE);
11
String merchantResponse = (String) result.get(PayUCheckoutProConstants.CP_MERCHANT_RESPONSE);
12
}
13
14
@Override
15
public void onPaymentFailure(Object response) {
16
//Cast response object to HashMap
17
HashMap<String,Object> result = (HashMap<String, Object>) response;
18
String payuResponse = (String)result.get(PayUCheckoutProConstants.CP_PAYU_RESPONSE);
19
String merchantResponse = (String) result.get(PayUCheckoutProConstants.CP_MERCHANT_RESPONSE);
20
}
21
22
@Override
23
public void onPaymentCancel(boolean isTxnInitiated) {
24
}
25
26
@Override
27
public void onError(ErrorResponse errorResponse) {
28
String errorMessage = errorResponse.getErrorMessage();
29
}
30
31
@Override
32
public void setWebViewProperties(@Nullable WebView webView, @Nullable Object o) {
33
//For setting webview properties, if any. Check Customized Integration section for more details on this
34
}
35
36
@Override
37
public void generateHash(HashMap<String, String> valueMap, PayUHashGenerationListener hashGenerationListener) {
38
String hashName = valueMap.get(PayUCheckoutProConstants.CP_HASH_NAME);
39
String hashData = valueMap.get(PayUCheckoutProConstants.CP_HASH_STRING);
40
if (!TextUtils.isEmpty(hashName) && !TextUtils.isEmpty(hashData)) {
41
//Do not generate hash from local, it needs to be calculated from server side only. Here, hashString contains hash created from your server side.
42
String hash = hashString;
43
HashMap<String, String> dataMap = new HashMap<>();
44
dataMap.put(hashName, hash);
45
hashGenerationListener.onHashGenerated(dataMap);
46
}
47
}
48
}
49
);
Copied!
1
PayUCheckoutPro.open(
2
this, payUPaymentParams,
3
object : PayUCheckoutProListener {
4
5
6
override fun onPaymentSuccess(response: Any) {
7
response as HashMap<*, *>
8
val payUResponse = response[PayUCheckoutProConstants.CP_PAYU_RESPONSE]
9
val merchantResponse = response[PayUCheckoutProConstants.CP_MERCHANT_RESPONSE]
10
}
11
12
13
override fun onPaymentFailure(response: Any) {
14
response as HashMap<*, *>
15
val payUResponse = response[PayUCheckoutProConstants.CP_PAYU_RESPONSE]
16
val merchantResponse = response[PayUCheckoutProConstants.CP_MERCHANT_RESPONSE]
17
}
18
19
20
override fun onPaymentCancel(isTxnInitiated:Boolean) {
21
}
22
23
24
override fun onError(errorResponse: ErrorResponse) {
25
val errorMessage: String
26
if (errorResponse != null && errorResponse.errorMessage != null && errorResponse.errorMessage!!.isNotEmpty())
27
errorMessage = errorResponse.errorMessage!!
28
else
29
errorMessage = resources.getString(R.string.some_error_occurred)
30
}
31
32
override fun setWebViewProperties(webView: WebView?, bank: Any?) {
33
//For setting webview properties, if any. Check Customized Integration section for more details on this
34
}
35
36
override fun generateHash(
37
valueMap: HashMap<String, String?>,
38
hashGenerationListener: PayUHashGenerationListener
39
) {
40
if ( valueMap.containsKey(CP_HASH_STRING)
41
&& valueMap.containsKey(CP_HASH_STRING) != null
42
&& valueMap.containsKey(CP_HASH_NAME)
43
&& valueMap.containsKey(CP_HASH_NAME) != null) {
44
45
val hashData = valueMap[CP_HASH_STRING]
46
val hashName = valueMap[CP_HASH_NAME]
47
48
//Do not generate hash from local, it needs to be calculated from server side only. Here, hashString contains hash created from your server side.
49
val hash: String? = hashString
50
if (!TextUtils.isEmpty(hash)) {
51
val dataMap: HashMap<String, String?> = HashMap()
52
dataMap[hashName!!] = hash!!
53
hashGenerationListener.onHashGenerated(dataMap)
54
}
55
}
56
}
57
})
Copied!
Last modified 8mo ago