sud.getBalance
Queries the game currency balance and cumulative totals.
Parameters
Object object
| Property | Type | Default | Required | Description |
|---|---|---|---|---|
| env | number | 0 | No | 0 for production, 1 for testing |
| success | function | No | Callback for a successful query | |
| fail | function | No | Callback for a failed query | |
| complete | function | No | Callback invoked when the call ends, after either success or failure |
Callback Parameters
success
| Property | Type | Description |
|---|---|---|
| balance | number | Total game currency balance, including both paid and granted amounts |
| presentBalance | number | Current granted game currency balance |
| sumSave | number | Cumulative game currency topped up |
| sumPresent | number | Cumulative game currency granted |
| sumBalance | number | Cumulative game currency obtained |
| sumCost | number | Cumulative game currency spent |
The paid balance is balance - presentBalance. The API does not return it separately, so compute it yourself.
fail
| Property | Type | Description |
|---|---|---|
| errCode | number | Error code |
| errMsg | string | Error message |
complete carries no arguments. It is invoked after success or fail, and runs only once per request.
Notes
envmust match theenvused insignDatawhen the order was created, otherwise you will read the balance of the other environment.- Order delivery and reading the updated balance are two separate things. The balance may lag briefly after the server confirms delivery, so retry on a failed query instead of treating it as a delivery failure.
- Refresh the UI from this API's result. Do not treat locally incremented or decremented values on the client as the balance.
Example Code
javascript
sud.getBalance({
env: 0,
success: (res) => {
const paidBalance = res.balance - res.presentBalance;
console.log('Total balance', res.balance);
console.log('Paid balance', paidBalance);
console.log('Granted balance', res.presentBalance);
console.log('Topped up', res.sumSave, 'Spent', res.sumCost);
},
fail: (err) => {
console.error('Failed to query balance', err.errCode, err.errMsg);
},
complete: () => {
console.log('Query balance call finished');
},
});