Purchasing a Virtual Good with Multiple Currencies

When purchasing a Virtual Good using one of GameSparks' Currencies, the player sends a BuyVirtualGoodsRequest. By default, you can only specify a single Currency - that is, one of the GameSparks' default Currencies OR one custom Currency. But let's suppose you want to purchase the Virtual Good using multiple Currencies. This can't be done using a BuyVirtualGoodsRequest alone, it will require the addition of some custom Cloud Code. This article will step you through how to do this:

  • Creating Currencies
  • Adding a Virtual Good
  • Setting Up the Request
  • Adding Cloud Code
  • Testing

Creating Currencies

First, we create the Currencies we'll use to purchase the Virtual Good by navigating to Configurator > Currencies and selecting Add. Specifying a signup bonus for each new Currency ensures any newly created players sending the request will have sufficient funds to purchase the good. If you use an existing player, you can give them the Currency amounts the player needs in Cloud Code using SparkPlayer's 'credit' function.


Adding a Virtual Good

Next, we create the Virtual Good that will be purchased by going to Configurator > Virtual Goods and selecting Add:


Setting up the Request

Let's take a look at the BuyVirtualGoodsRequest we'll be sending. You can see that we are specifying one of our Currencies in the currencyShortCode field, as you would normally do with this request. The other Currency or Currencies to be used are sent as an array of strings in the request's scriptData. Note that in this example we are only using a single additional Currency, but this will also work with more Currency types.

    "@class": ".BuyVirtualGoodsRequest",
    "currencyShortCode": "gold",
    "quantity": 1,
    "shortCode": "sword",
    "scriptData": {
        "otherCurrencies": ["silver"]

Adding Cloud Code

We'll need to add Cloud Code to both the BuyVirtualGoodsRequest and BuyVirtualGoodsResponse. In the request script, we check the player's balance for each of the Currencies specified in the request's scriptData. The request itself will handle the Currency specified in the currencyShortCode field. If the player has insufficient balance in any Currencies specified, then we throw an error:

//Get the request json
var reqData = Spark.getData();

//Check if other currencies have been specified in the request's scriptData
if(reqData.scriptData && reqData.scriptData.otherCurrencies){
    //Get the good's costs for the other currencies
    var good = Spark.getConfig().getVirtualGood(reqData.shortCode);
    var otherCurrs = reqData.scriptData.otherCurrencies;
        //Calculate how much of the currency the good costs and check if 
        //the player has enough. Throw an error if not
        var cost = good.getCurrencyCosts()[currency]*reqData.quantity;
            Spark.setScriptError(currency, "INSUFFICIENT_FUNDS");
    //Pass the other currencies to the response script 
    //This is so we can deduct them from the player's balance
    Spark.setScriptData("otherCurrencies", otherCurrs);

In the response script, we make sure the purchase succeeded. If it did succeed, then we deduct the cost of the good from each Currency type specified in the request:

//Get the response JSON
var resData = Spark.getData();
var otherCurrs = Spark.getScriptData("otherCurrencies");

//If the purchase failed don't proceed any further
if(resData.error)   Spark.exit();

    //If other currencies were involved, check which item was bought
    //and use it's config data to calculate how much to deduct from the
    var player = Spark.getPlayer();
    var goodBought = resData.boughtItems[0];
    var currencyCosts = Spark.getConfig().getVirtualGood(goodBought.shortCode).getCurrencyCosts();
    var currencyConsumed = {};
        var amount = currencyCosts[currency]*goodBought.quantity;
        player.debit(currency, amount, "Purchased " + goodBought.shortCode);
        currencyConsumed[currency] = amount;
    Spark.setScriptData("otherCurrenciesConsumed", currencyConsumed);


First, we send an AccountDetailsRequest. The response will show us the player's starting balances in the Currencies being used as well as their current Virtual Goods:


Next we send the BuyVirtualGoodsRequest. The response will detail the good purchased and the amount of Currency consumed.


Sending a second BuyVirtualGoodsRequest should fail, because the checks we added to the request script will flag a lack of funds in the silver Currency:


Sending a final AccountDetailsRequest will show the purchased good as well as the updated Currency balances: