Handling Responses
Handling Responses
The SDK returns the result via PaymentResponseService.responseCallback.
Display the JSON response in a DialogActivity.
This module sets up a response callback for handling payment responses received from the PaymentResponseService. When a response is received, the callback creates an Intent to launch the DialogActivity, passing the response data as a JSON string via the "json_response" extra. The Intent.FLAG_ACTIVITY_NEW_TASK flag ensures that the activity is started in a new task, which is useful when launching from a non-activity context. This design allows the application to display transaction results or messages in a dedicated dialog interface, improving user feedback and flow control after a payment operation.
What Handling Responses Does
1
Registers Listeners Early
It ensures that your app is ready to receive and process responses (e.g. transaction results, errors, device status) as soon as the activity starts.
2
Enables Real-Time Feedback
Callbacks allow the app to react immediately to SDK events—such as payment success, failure, or timeout—without polling or waiting.
3
Improves User Experience
By handling responses promptly, you can update the UI (e.g. show a receipt, display an error message, or prompt for retry) in a smooth and responsive way.
4
Supports Asynchronous Operations
Many SDKs operate asynchronously. Callbacks let you manage these operations without blocking the main thread, keeping the app fast and fluid.
5
Centralises Event Handling
Setting up callbacks helps consolidate logic for SDK interactions, making the codebase cleaner and easier to maintain.
6
Ensures Lifecycle Awareness
By tying callbacks to the activity lifecycle, you avoid memory leaks and ensure that responses are only handled when the activity is active.
1
2
3
4
5
6
7
PaymentResponseService.responseCallback = { response ->
val intent = Intent(this, DialogActivity::class.java).apply {
putExtra("json_response", response)
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
startActivity(intent)
}