Game Maker Google Play Billing Extension

Game Maker Google Play Billing extension is a great tool for you to implement in-app purchases in your game. Currently, it supports

  • Making purchases (Buying in-app items etc.)
  • Verifying purchases
  • Querying purchased items, to check whether the player has purchased the items or not.
Game Maker Google Play Billing extension is written by the community member kroart. His forum post is available here

DOCUMENTATION

GooglePlayBilling_initialize(licenseKey)

Initializes Google Play Billing extension. licenseKey can be found in Google Play Developer Console. After selecting your game, head over the Development Tools > Services & APIs on the left menu. Your license key will be seen on the window.

Of course, don’t share this key with anybody 🙂

Using this function will trigger a Social Async Event with

  • id : Will be equal to GooglePlayBilling_ASYNC_RESPONSE_INITIALIZE constant
  • responseCode : Will be GooglePlayBilling_BILLING_RESPONSE_OK if initialized successfully
//Social Async Event
var ID, responseCode;
ID = async_load[? "id"];
responseCode = async_load[? "responseCode"];

if (ID==GooglePlayBilling_ASYNC_RESPONSE_INITIALIZE)
{
	//Check if we initialized successfully?
	if (responseCode==GooglePlayBilling_BILLING_RESPONSE_OK)
	{
		show_debug_message("INITIALIZATION IS COMPLETED");
	}
 
}

GooglePlayBilling_queryPurchases()

Returns all the purchased items in a JSON string. You can use json_decode() function to convert this string to a ds_map. This ds_map has a “default” key which includes a ds_list that includes all the SKUs(stock keeping unit / in-app item names).

If it fails, it’ll simply return a string “[]”, but this wouldn’t happen in a normal condition. So don’t worry.

var str = GooglePlayBilling_queryPurchases();

var item_map = json_decode(str);
var item_list = item_map[? "default"];

//item_list is a ds_list that includes our items.
//let's see all the items we purchased
for (var i = 0; i < ds_list_size(item_list); ++i) {
	var item = item_list[| i];
    show_debug_message("I had purchased "+string(item)+" before :)");
	//You can store them actually.
}

//Delete the unused maps.
ds_map_destroy(item_map);

GooglePlayBilling_launchBillingFlow(sku)

It’ll open a window which leads player to purchase the item. sku is the item’s name on in-app items panel. You’ll sell that item. It’ll return a Social Async Event with

  • id : Will be equal to GooglePlayBilling_ASYNC_RESPONSE_PURCHASE_UPDATED
  • responseCode : Will be equal to GooglePlayBilling_BILLING_RESPONSE_OK if user successfully purchased the item
  • sku : Will be the item’s name that you put as the argument
  • purchaseToken : A special token that identifies the purchase(Think it like a receipt code). We’ll need this if we wish to consume the product.
If you make the purchase with intention of consuming it, you must store the purchaseToken as you’ll need it in the process of consumption.
var ID = async_load[? "id"];

if (ID==GooglePlayBilling_ASYNC_RESPONSE_PURCHASE_UPDATED)
{
	
	var responseCode = async_load[? "responseCode"]
	
	if (responseCode==GooglePlayBilling_BILLING_RESPONSE_OK)
	{
		var sku = async_load[? "sku"];
		var purchaseToken = async_load[? "purchaseToken"];
		show_message_async("You've purchased the item "+string(sku)+". Thanks :)");
		show_debug_message("The purchase token is : "+string(purchaseToken));
	}
	
}

GooglePlayBilling_consumeAsync(purchaseToken)

Consumes the previously purchased item. purchaseToken is what we stored after buying the item. Notice that extension will try to make this process happen even though the process fails. So, all’s easy and smooth. Returns an Async Event with

  • id : GooglePlayBilling_ASYNC_RESPONSE_CONSUME
  • responseCode
  • purchaseToken : The token of the consumed item
var ID;
ID = async_load[? "id"];
if (ID==GooglePlayBilling_ASYNC_RESPONSE_CONSUME)
{
	var purchaseToken = async_load[? "purchaseToken"];
	//Check if we initialized successfully?
	show_debug_message(purchaseToken);
 
}

Consuming a Purchased Item

After calling GooglePlayBilling_launchBillingFlow function, in the Social Async Event, you can complete the consumption process easily.

var ID = async_load[? "id"];

switch(ID)
{
	case GooglePlayBilling_ASYNC_RESPONSE_PURCHASE_UPDATED:
	
	var responseCode = async_load[? "responseCode"]
	
	if (responseCode==GooglePlayBilling_BILLING_RESPONSE_OK)
	{
		var sku = async_load[? "sku"];
		var purchaseToken = async_load[? "purchaseToken"];
		show_message_async("You've purchased the item "+string(sku)+". Thanks :)");
		show_debug_message("The purchase token is : "+string(purchaseToken));
	}
	//Consume the item
	GooglePlayBilling_consumeAsync(purchaseToken);
	break;
	
	//When the consumption is completed
	case GooglePlayBilling_ASYNC_RESPONSE_CONSUME:
	show_message_async("Yes Item is consumed");
	global.gold+=100;
	break;
	
}
Subscription functionality may be added to the extension in the future.
GET THE EXTENSION

Open Source

Source code & Android project can be found here

Share

Marty

Truely speaking, I don't know what am I doing most of the time.

3 Responses

  1. Michail says:

    why is it better than existing, official one? I honestly don’t see any difference…

    • Michail says:

      Still good job though, thank you for your work

    • Hi Michail. Except the technical part, there’s no difference between this extension and the official one.

      However, the official extension has not been touched for years. This is more of a fresh one.

      Also, the author of the extension may bring in “subscription option”. The official extension probably never receive this update.

      In any case, I would use this extension in all scenarios.

      Thank you for your comment 🙂

Leave a Reply

%d bloggers like this: