Sign In Register

How can we help you today?

Start a new topic
Answered

How do I convert any currency to USD?

For example, I want to convert 2000 JPY to USD. I was told that this is possible in GameSparks. How do I do it in cloud code?


Best Answer
Hey Nipitpon,

I might have a workaround for you, but it would require you to find some site that has a REST api that will get the exchange rates you need. Sites that offer this data for free might not give you a full list of currencies though, so you might be limited.
The way this works is to keep a collection of exchange rates that you get from an external server or website and update the rate each day.
In this example i am using the calls from http://fixer.io/ because it was a really easy solution.

First you need to call the REST api and get the exchange rate. You will get a response like this...

 

So you can stick this code into the every-day script to make sure it gets the new rates everyday.

 

var response = Spark.getHttp("http://api.fixer.io/latest").get().getResponseJson(); // get the response from the rate provider
Spark.runtimeCollection('exchangeRates').drop(); // clear the collection because we want completly new rates for today

var rates = response.rates; // you can skip this step, ive just got it to make the code a bit neater
var noOfRates = Object.keys(rates).length; // get the number of rates in the response so we can loop though them

for(var i = 0; i < noOfRates; i++){

    var newRate = {};
    newRate.id = Object.keys(rates)[i].toString();
    newRate.rate = rates[Object.keys(rates)[i]];
    
    Spark.runtimeCollection('exchangeRates').insert(newRate); // insert a new doc into the colleciton
}

 

This will give you a collection you can query any rate.
For example if you need the EURO-> AUD rate you can query Spark.runtimeCollection.findOne({ "id" : "AUD" })

Now, this gets you all rates for euro conversion, but if you want you can use that rate to find out the relationship between each of them.

Hope that helps,

Sean



Hello Nipitpon Wongsuparatkul,


Currently there are no built-in modules that GameSparks provides for currency exchange functionality, the logic for handling conversions would have to be implemented by yourself. Additionally may I suggest raising a feature request, if it gets approved, the functionality would be added in a future release of the platform.


Regards,

Mantas

Answer
Hey Nipitpon,

I might have a workaround for you, but it would require you to find some site that has a REST api that will get the exchange rates you need. Sites that offer this data for free might not give you a full list of currencies though, so you might be limited.
The way this works is to keep a collection of exchange rates that you get from an external server or website and update the rate each day.
In this example i am using the calls from http://fixer.io/ because it was a really easy solution.

First you need to call the REST api and get the exchange rate. You will get a response like this...

 

So you can stick this code into the every-day script to make sure it gets the new rates everyday.

 

var response = Spark.getHttp("http://api.fixer.io/latest").get().getResponseJson(); // get the response from the rate provider
Spark.runtimeCollection('exchangeRates').drop(); // clear the collection because we want completly new rates for today

var rates = response.rates; // you can skip this step, ive just got it to make the code a bit neater
var noOfRates = Object.keys(rates).length; // get the number of rates in the response so we can loop though them

for(var i = 0; i < noOfRates; i++){

    var newRate = {};
    newRate.id = Object.keys(rates)[i].toString();
    newRate.rate = rates[Object.keys(rates)[i]];
    
    Spark.runtimeCollection('exchangeRates').insert(newRate); // insert a new doc into the colleciton
}

 

This will give you a collection you can query any rate.
For example if you need the EURO-> AUD rate you can query Spark.runtimeCollection.findOne({ "id" : "AUD" })

Now, this gets you all rates for euro conversion, but if you want you can use that rate to find out the relationship between each of them.

Hope that helps,

Sean


Hey Nipitpon,

Does that work for you?

Sean

 

It's not as clean as I'd expected. I thought there would be built-in module for it. But this will have to do.

Thanks for the suggestions.

Login to post a comment