Sign In Register

How can we help you today?

Start a new topic
Answered

ListTransactionsRequest

How is it possible to access a player's transactions through Cloud code ?


- In Test Harness, there is the ListTransactionsRequest event

- In NoSQL tab I can see them in the system collection "PlayerTransactionAudit"


But in Cloud code or dynamic forms, didn't found...


Thanks for your help.


Best Answer

Hi Eric,


Not directly I'm afraid, Marcel has pointed you in the right direction here. You could indeed use SparkRequests in Cloud Code for this. 


//sets the from date to 10 days back from now
var dateFrom = new Date();
dateFrom.setDate(dateFrom.getDate()-10);

//sets the to date to now
var dateTo = new Date();
dateTo.setDate(dateTo.getDate());

//send our ListTransactionsRequest
var request = new SparkRequests.ListTransactionsRequest();
request.dateFrom = getDateString(dateFrom);
request.dateTo = getDateString(dateTo);
request.entryCount = 5;

var response = request.Send();

//display the response in scriptData
Spark.setScriptData("vGoods", response)


//function to correctly format the dates for the request
function getDateString(date){
    
    var dateString = date.toISOString();
    return  dateString.substr(0, dateString.lastIndexOf(".")) + "Z";
    
}


You could also include the "include" field in the request to filter the results. 


Thanks,

Liam



Hi Eric,


You can create such request like this: 

var req = new SparkRequests.ListTransactionsRequest();
req.dateFrom = ...;
req.dateTo = ...;
req.entryCount = ...;
req.include = ...;
req.offset = ...;
var resp = req.Send(); // This object contains the response


Best,


Marcel

Answer

Hi Eric,


Not directly I'm afraid, Marcel has pointed you in the right direction here. You could indeed use SparkRequests in Cloud Code for this. 


//sets the from date to 10 days back from now
var dateFrom = new Date();
dateFrom.setDate(dateFrom.getDate()-10);

//sets the to date to now
var dateTo = new Date();
dateTo.setDate(dateTo.getDate());

//send our ListTransactionsRequest
var request = new SparkRequests.ListTransactionsRequest();
request.dateFrom = getDateString(dateFrom);
request.dateTo = getDateString(dateTo);
request.entryCount = 5;

var response = request.Send();

//display the response in scriptData
Spark.setScriptData("vGoods", response)


//function to correctly format the dates for the request
function getDateString(date){
    
    var dateString = date.toISOString();
    return  dateString.substr(0, dateString.lastIndexOf(".")) + "Z";
    
}


You could also include the "include" field in the request to filter the results. 


Thanks,

Liam


Hi, 


Thanks for your help Marcel & Liam, it's getting clearer...

But I'm still stuck.

The final goal is to display last N transactions for a given player in dynamic forms, so I need to get this list for a given userId and ordered by descending date.

Can't see these 2 options...

Hi Eric,


You could try using SendRequestAs for this in Cloud Code, this will work the same as above but will return the transaction list for the given playerId.


var request = Spark.sendRequestAs(
    {
    "@class": ".ListTransactionsRequest", 
    "dateFrom": getDateString(dateFrom),
    "dateTo": getDateString(dateTo),
    "entryCount": 10,
    }, playerID);
    
Spark.setScriptData("vGoods", request.transactionList)

 

It would also use the date function from the code above, let me know if that helps.


Thanks,

Liam

Thanks Liam, I missed this way of doing...

Last thing : what is the sort order of the result ? Is it possible to change it ?

Hi Eric,


By default the results will list the transactions starting with the most recent one first and working backwards, once you have the transactionList array you can sort it however you'd like in the response. For example response.transactionList.reverse() will reverse the list. You can play around with with this until you find something that suits your needs.


Thanks,

Liam

Thanks Liam, you're right, once in an array, I can manipulate it locally.

Login to post a comment