Track order status
After a user makes a purchase, your application can perform the following actions:
- show the order status in the application UI
- credit a user’s balance after a successful payment
- grant the purchased items after a successful payment
When using the SDK to implement the logic of these actions, you can track the order status in the following ways:
- subscribe to order status changes (recommended)
- request order status
If you use both the Store and Payments libraries in your application, we recommend using the Store library method to track the order.
If you only use the Payments library, subscribe to order status changes using the Payments library method. Use the method to request the order status only as an auxiliary way to retrieve data to avoid overloading Xsolla systems.
Subscribe to order status changes
Use Store library method
To subscribe to order status changes, use the getOrderStatus
SDK method from the Store library and pass the following parameters to the method:
listener
— listener object of typeOrderStatusListener
.orderId
— order ID received from the purchase via the shopping cart, one-click purchase, or purchase for virtual currency as the parameter.
Example of calling the XStore.getOrderStatus method:
- kotlin
XStore.getOrderStatus(object : OrderStatusListener() {
override fun onStatusUpdate(status: OrderResponse.Status) {
if(status == OrderResponse.Status.DONE) {
Log.d("MainActivity", "Success")
}
}
override fun onFailure() {
Log.d("MainActivity", "Failure")
}
}, orderId)
We recommend to call XStore.getOrderStatus
method when opening payment UI.
Purchase methods encapsulate several methods for tracking the order status. Tracking is performed according to the following algorithm:
- A web socket connection is established.
- If the web socket connection is established successfully and order status changes to
done
orcancel
, tracking stops. If a web socket connection fails or the response contains incorrect data, the order status is tracked using short-polling. - Order status tracking continues with short-polling. A simple HTTP order status request is sent once every 3 seconds. Tracking stops if order status changes to
done
orcancel
.
Use Payments library method
To track order status changes using the Payments library, use the payment status callback. To do this:- When creating the
XPayments.IntentBuilder
object, pass theStatusReceivedCallback
callback using thesetStatusReceivedCallback
method. - Implement the
onSuccess
method in theStatusReceivedCallback
callback, which is called with every unique status change.
Order status information is passed to the onSuccess
method in an object of the InvoicesDataResponse
type. This object contains an array of InvoiceData
objects. Each InvoiceData
object corresponds to a specific stage in the order processing and contains the status of this stage.
For example, if the user initially entered invalid data when placing the order, an object with the status InvoicesDataResponse.CANCELED
will appear in the InvoiceData
list. If the user then corrects the data and successfully pays for the order, a new InvoiceData
object will appear in the array, now with the status InvoicesDataResponse.Status.DONE
.
Status tracking stops if the final payment status (InvoicesDataResponse.Status.DONE
or InvoicesDataResponse.Status.ERROR
) is received.
Example:
- kotlin
val intent = XPayments.createIntentBuilder(this)
.accessToken(<accessToken>)
.isSandbox(<isSandbox>)
.setStatusReceivedCallback(object : StatusReceivedCallback {
override fun onSuccess(data: InvoicesDataResponse) {
Log.d(TAG, "StatusReceivedCallback is fired. Result data = $data")
}
})
.build()
Request order status
To request the current status of the payment, call the getOrder
method from the Store library, passing the following parameters to it:
orderId
— the order ID, which was received from the purchase via the shopping cart, one-click purchase, or purchase for virtual currency.callback
— the callback for successfully retrieving order information. When implementing a callback, use theGetStatusCallback
interface, and implement theonSuccess
andonError
methods.
Order status information is passed to the onSuccess
method in an object of the InvoicesDataResponse
type. This object contains an array of InvoiceData
objects. Each InvoiceData
object corresponds to a specific stage in the order processing and contains the status of this stage.
For example, if the user initially entered invalid data when placing the order, an object with the status InvoicesDataResponse.CANCELED
will appear in the InvoiceData
list. If the user then corrects the data and successfully pays for the order, a new InvoiceData
object will appear in the array, now with the status InvoicesDataResponse.Status.DONE
.
Status tracking stops if the final payment status (InvoicesDataResponse.Status.DONE
or InvoicesDataResponse.Status.ERROR
) is received.
Example:
- kotlin
XPayments.getStatus(<token>, <isSandbox>, object : GetStatusCallback {
override fun onSuccess(data: InvoicesDataResponse?) {
Log.d(TAG, "onSuccess is fired. Result data = $data")
}
override fun onError(throwable: Throwable?, errorMessage: String?) {
Log.d(TAG, "onError is fired. ErrorMessage = $errorMessage")
}
})
Useful links
Last updated: September 5, 2024Found a typo or other text error? Select the text and press Ctrl+Enter.