BuyVirtualGoodsRequest

Purchases a virtual good with an in game currency. Once purchased the virtual good will be added to the players account.

View interactive version here

Request Parameters

Parameter Required Type Description
currencyShortCode No string The short code of the currency to use
currencyType No number Which virtual currency to use. (1 to 6)
quantity Yes number The number of items to purchase
shortCode Yes string The short code of the virtual good to be purchased

Response Parameters

A response containing details of the bought items

Parameter Type Description
boughtItems Boughtitem[] A JSON object containing details of the bought items
currenciesAdded JSON An object containing the short code and amount added for each currency
currency1Added number How much currency type 1 was added
currency2Added number How much currency type 2 was added
currency3Added number How much currency type 3 was added
currency4Added number How much currency type 4 was added
currency5Added number How much currency type 5 was added
currency6Added number How much currency type 6 was added
currencyConsumed number For a buy with currency request, how much currency was used
currencyShortCode string For a buy with currency request, the short code of the currency used
currencyType number For a buy with currency request, which currency type was used
invalidItems string[] A list of invalid items for this purchase (if any). This field is populated only for store buys
scriptData ScriptData A JSON Map of any data added either to the Request or the Response by your Cloud Code
transactionIds string[] The list of transactionIds, for this purchase, if they exist. This field is populated only for store buys

Nested types

Boughtitem

A nested object that represents a bought item.

Parameter Type Description
quantity number The quantity of the bought item
shortCode string The short code of the bought item

ScriptData

A collection of arbitrary data that can be added to a message via a Cloud Code script.

Parameter Type Description
myKey string An arbitrary data key
myValue JSON An arbitrary data value.

Error Codes

Key Value Description
currencyType UNRECOGNISED Not a valid currency, valid values are 1 to 6
virtualGood CANNOT_BE_CURRENCY The player cannot buy a currency pack with virtual currency
currency1 INSUFFICIENT_FUNDS The player does not have enough currency 1 funds to complete the purchase
currency2 INSUFFICIENT_FUNDS The player does not have enough currency 2 funds to complete the purchase
currency3 INSUFFICIENT_FUNDS The player does not have enough currency 3 funds to complete the purchase
currency4 INSUFFICIENT_FUNDS The player does not have enough currency 4 funds to complete the purchase
currency5 INSUFFICIENT_FUNDS The player does not have enough currency 5 funds to complete the purchase
currency6 INSUFFICIENT_FUNDS The player does not have enough currency 6 funds to complete the purchase
shortCode UNKNOWN The shortCode supplied does not match a VirtualGood
shortCode DISABLED The VirtualGood requested is marked as disabled
quantity EXCEEDS_MAX_QUANTITY The requst would cause the player to exceed to maxQty of this VirtualGood
currencyShortCode UNRECOGNISED Not a valid currency

Code Samples

C#

    using GameSparks.Api;
    using GameSparks.Api.Requests;
    using GameSparks.Api.Responses;
    ...
    new BuyVirtualGoodsRequest()
        .SetCurrencyShortCode(currencyShortCode)
        .SetCurrencyType(currencyType)
        .SetQuantity(quantity)
        .SetShortCode(shortCode)
        .Send((response) => {
        GSEnumerable<var> boughtItems = response.BoughtItems; 
        GSData currenciesAdded = response.CurrenciesAdded; 
        long? currency1Added = response.Currency1Added; 
        long? currency2Added = response.Currency2Added; 
        long? currency3Added = response.Currency3Added; 
        long? currency4Added = response.Currency4Added; 
        long? currency5Added = response.Currency5Added; 
        long? currency6Added = response.Currency6Added; 
        long? currencyConsumed = response.CurrencyConsumed; 
        string currencyShortCode = response.CurrencyShortCode; 
        int? currencyType = response.CurrencyType; 
        IList<string> invalidItems = response.InvalidItems; 
        GSData scriptData = response.ScriptData; 
        IList<string> transactionIds = response.TransactionIds; 
        });

ActionScript 3

    import com.gamesparks.*;
    import com.gamesparks.api.requests.*;
    import com.gamesparks.api.responses.*;
    import com.gamesparks.api.types.*;
    ...

    gs.getRequestBuilder()
        .createBuyVirtualGoodsRequest()
        .setCurrencyShortCode(currencyShortCode)
        .setCurrencyType(currencyType)
        .setQuantity(quantity)
        .setShortCode(shortCode)
        .send(function(response:com.gamesparks.api.responses.BuyVirtualGoodResponse):void {
        var boughtItems:Vector.<Boughtitem> = response.getBoughtItems(); 
        var currenciesAdded:Object = response.getCurrenciesAdded(); 
        var currency1Added:Number = response.getCurrency1Added(); 
        var currency2Added:Number = response.getCurrency2Added(); 
        var currency3Added:Number = response.getCurrency3Added(); 
        var currency4Added:Number = response.getCurrency4Added(); 
        var currency5Added:Number = response.getCurrency5Added(); 
        var currency6Added:Number = response.getCurrency6Added(); 
        var currencyConsumed:Number = response.getCurrencyConsumed(); 
        var currencyShortCode:String = response.getCurrencyShortCode(); 
        var currencyType:Number = response.getCurrencyType(); 
        var invalidItems:Vector.<String> = response.getInvalidItems(); 
        var scriptData:ScriptData = response.getScriptData(); 
        var transactionIds:Vector.<String> = response.getTransactionIds(); 
        });

Objective-C

    #import "GS.h"
    #import "GSAPI.h"
    ...
    GSBuyVirtualGoodsRequest* request = [[GSBuyVirtualGoodsRequest alloc] init];
    [request setCurrencyShortCode:currencyShortCode;
    [request setCurrencyType:currencyType;
    [request setQuantity:quantity;
    [request setShortCode:shortCode;
    [request setCallback:^ (GSBuyVirtualGoodResponse* response) {
    NSArray* boughtItems = [response getBoughtItems]; 
    NSDictionary* currenciesAdded = [response getCurrenciesAdded]; 
    NSNumber* currency1Added = [response getCurrency1Added]; 
    NSNumber* currency2Added = [response getCurrency2Added]; 
    NSNumber* currency3Added = [response getCurrency3Added]; 
    NSNumber* currency4Added = [response getCurrency4Added]; 
    NSNumber* currency5Added = [response getCurrency5Added]; 
    NSNumber* currency6Added = [response getCurrency6Added]; 
    NSNumber* currencyConsumed = [response getCurrencyConsumed]; 
    NSString* currencyShortCode = [response getCurrencyShortCode]; 
    NSNumber* currencyType = [response getCurrencyType]; 
    NSArray* invalidItems = [response getInvalidItems]; 
    NSDictionary* scriptData = [response getScriptData]; 
    NSArray* transactionIds = [response getTransactionIds]; 
    }];
    [gs send:request];

C++


    #include <GameSparks/generated/GSRequests.h>
    using namespace GameSparks::Core;
    using namespace GameSparks::Api::Responses;
    using namespace GameSparks::Api::Requests;
    ...

    void BuyVirtualGoodsRequest_Response(GS& gsInstance, const BuyVirtualGoodResponse& response) {
    gsstl:vector<Types::Boughtitem*> boughtItems = response.getBoughtItems(); 
    GSData currenciesAdded = response.getCurrenciesAdded(); 
    Optional::t_LongOptional currency1Added = response.getCurrency1Added(); 
    Optional::t_LongOptional currency2Added = response.getCurrency2Added(); 
    Optional::t_LongOptional currency3Added = response.getCurrency3Added(); 
    Optional::t_LongOptional currency4Added = response.getCurrency4Added(); 
    Optional::t_LongOptional currency5Added = response.getCurrency5Added(); 
    Optional::t_LongOptional currency6Added = response.getCurrency6Added(); 
    Optional::t_LongOptional currencyConsumed = response.getCurrencyConsumed(); 
    gsstl::string currencyShortCode = response.getCurrencyShortCode(); 
    Optional::t_LongOptional currencyType = response.getCurrencyType(); 
    gsstl:vector<gsstl::string> invalidItems = response.getInvalidItems(); 
    GSData scriptData = response.getScriptData(); 
    gsstl:vector<gsstl::string> transactionIds = response.getTransactionIds(); 
    }
    ......

    BuyVirtualGoodsRequest request(gsInstance);
    request.SetCurrencyShortCode(currencyShortCode)
    request.SetCurrencyType(currencyType)
    request.SetQuantity(quantity)
    request.SetShortCode(shortCode)
    request.Send(BuyVirtualGoodsRequest_Response);

Java

import com.gamesparks.sdk.api.autogen.GSRequestBuilder.BuyVirtualGoodsRequest;
import com.gamesparks.sdk.api.autogen.GSResponseBuilder.BuyVirtualGoodResponse;
import com.gamesparks.sdk.api.autogen.GSTypes.*;
import com.gamesparks.sdk.api.GSEventListener;

...
gs.getRequestBuilder().createBuyVirtualGoodsRequest()
    .setCurrencyShortCode(currencyShortCode)
    .setCurrencyType(currencyType)
    .setQuantity(quantity)
    .setShortCode(shortCode)
    .send(new GSEventListener<BuyVirtualGoodResponse>() {
        @Override
        public void onEvent(BuyVirtualGoodResponse response) {
            List<Boughtitem> boughtItems = response.getBoughtItems(); 
            GSData currenciesAdded = response.getCurrenciesAdded(); 
            Long currency1Added = response.getCurrency1Added(); 
            Long currency2Added = response.getCurrency2Added(); 
            Long currency3Added = response.getCurrency3Added(); 
            Long currency4Added = response.getCurrency4Added(); 
            Long currency5Added = response.getCurrency5Added(); 
            Long currency6Added = response.getCurrency6Added(); 
            Long currencyConsumed = response.getCurrencyConsumed(); 
            String currencyShortCode = response.getCurrencyShortCode(); 
            Integer currencyType = response.getCurrencyType(); 
            List<String> invalidItems = response.getInvalidItems(); 
            GSData scriptData = response.getScriptData(); 
            List<String> transactionIds = response.getTransactionIds(); 
        }
    });

Cloud Code


    var request = new SparkRequests.BuyVirtualGoodsRequest();
    request.currencyShortCode = ...;
    request.currencyType = ...;
    request.quantity = ...;
    request.shortCode = ...;
    var response = request.Send();

var boughtItems = response.boughtItems; 
var currenciesAdded = response.currenciesAdded; 
var currency1Added = response.currency1Added; 
var currency2Added = response.currency2Added; 
var currency3Added = response.currency3Added; 
var currency4Added = response.currency4Added; 
var currency5Added = response.currency5Added; 
var currency6Added = response.currency6Added; 
var currencyConsumed = response.currencyConsumed; 
var currencyShortCode = response.currencyShortCode; 
var currencyType = response.currencyType; 
var invalidItems = response.invalidItems; 
var scriptData = response.scriptData; 
var transactionIds = response.transactionIds; 
Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here