GooglePlayBuyGoodsRequest

Processes the response from a Google Play in app purchase flow.

The GameSparks platform will validate the signature and signed data with the Google Play Public Key configured against the game.

The orderId in the data will be recorded and the request will be rejected if the orderId has previously been processed, this prevents replay attacks.

Once verfied, the players account will be credited with the Virtual Good, or Virtual Currency the purchase contains. The virtual good will be looked up by matching the productId in the signed data with the 'Google Product ID' configured against the virtual good.

It is critical that the signedData is sent exactly as it is returned form google, including any whitespace. Any modification of the signedData will cause the verification process to fail.

View interactive version here

Request Parameters

Parameter Required Type Description
currencyCode No string The ISO 4217 currency code representing the real-world currency used for this transaction.
signature Yes string The value obtained from data.getStringExtra("INAPP_DATA_SIGNATURE");
signedData Yes string The value obtained from data.getStringExtra("INAPP_PURCHASE_DATA")
subUnitPrice No number The price of this purchase
uniqueTransactionByPlayer No boolean If set to true, the transactionId from this receipt will not be globally valdidated, this will mean replays between players are possible.

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
verificationError 5 The orderId in the signedData has previously been processed
verificationError 4 The google play public key is not configured against the game
verificationError 3 There was an error connecting to the google play service
verificationError 2 The signature does not match the signed data
verificationError 1 No matching virtual good can be found

Code Samples

C#

    using GameSparks.Api;
    using GameSparks.Api.Requests;
    using GameSparks.Api.Responses;
    ...
    new GooglePlayBuyGoodsRequest()
        .SetCurrencyCode(currencyCode)
        .SetSignature(signature)
        .SetSignedData(signedData)
        .SetSubUnitPrice(subUnitPrice)
        .SetUniqueTransactionByPlayer(uniqueTransactionByPlayer)
        .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()
        .createGooglePlayBuyGoodsRequest()
        .setCurrencyCode(currencyCode)
        .setSignature(signature)
        .setSignedData(signedData)
        .setSubUnitPrice(subUnitPrice)
        .setUniqueTransactionByPlayer(uniqueTransactionByPlayer)
        .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"
    ...
    GSGooglePlayBuyGoodsRequest* request = [[GSGooglePlayBuyGoodsRequest alloc] init];
    [request setCurrencyCode:currencyCode;
    [request setSignature:signature;
    [request setSignedData:signedData;
    [request setSubUnitPrice:subUnitPrice;
    [request setUniqueTransactionByPlayer:uniqueTransactionByPlayer;
    [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 GooglePlayBuyGoodsRequest_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(); 
    }
    ......

    GooglePlayBuyGoodsRequest request(gsInstance);
    request.SetCurrencyCode(currencyCode)
    request.SetSignature(signature)
    request.SetSignedData(signedData)
    request.SetSubUnitPrice(subUnitPrice)
    request.SetUniqueTransactionByPlayer(uniqueTransactionByPlayer)
    request.Send(GooglePlayBuyGoodsRequest_Response);

Java

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

...
gs.getRequestBuilder().createGooglePlayBuyGoodsRequest()
    .setCurrencyCode(currencyCode)
    .setSignature(signature)
    .setSignedData(signedData)
    .setSubUnitPrice(subUnitPrice)
    .setUniqueTransactionByPlayer(uniqueTransactionByPlayer)
    .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.GooglePlayBuyGoodsRequest();
    request.currencyCode = ...;
    request.signature = ...;
    request.signedData = ...;
    request.subUnitPrice = ...;
    request.uniqueTransactionByPlayer = ...;
    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