In-App Purchasing with Playmaker

Hello, I want to share, how you can implement purchasing with Android Native Plugin using Playmaker.

In Android In-App Billing API v3 all purchases are now Managed purchases.

It's up to developer make purchase consumable or non-consumable.

The guide will cover the way to implement both:

  • Consumable items

  • Non-Consumable items

I hope this tutorial will be useful not for only Android Native user but for everyone who wants to implement In Apps in the Android Game.

To get started, you need:

First of all, we need to cover Google Play Setup which is described here. After all is done, we can move to the integration.


Note: I haven't used any global variable or events for this example. But you can feel free to use it on your own implementation to make it more elegant.

Note: Inside Android Native Plugin  the playmaker actions scripts can be found in the zip archive at: Assets/Extensions/AndroidNative/Addons/ 

And the example scene which we will make in this tutorial can be found at: Assets/Extensions/AndroidNative/xExample/Scenes/Playmaker/BillingWithPlaymakerExample.unity

The result scene will look like this


Now we can get started on In Apps implementation.

Product IDs

To define your in-app product ids, follow the next instructions:

Open Windows → Android Native → Edit Settings  add your products IDs and Base64 Key.

In my case a result is shown on the next screenshot:

For this tutorial I have two non-consumable items:

  • pm_green_sphere

  • pm_red_sphere

and one consumable

  • pm_coins


Billing Initialization

Firstly, pay attention to scene hierarchy view. All GameObjects with PlayMaker scripts stick together in PlayMakerActions game object. All our billing actions are in one place.


At the application start you need to use AN_initBilling action to actually initialize billing. As soon as the application is connected to the billing service, and you are able to send purchase requests, Success action will be fired.

After success initialization you need AN_RetriveInventory action to retrieve inventory. This logic is shown on the following screenshot

At the FSM start we will use AN_initBilling and AN_RetriveInventory actions, which will fire the Connected or Failed actions for us.

Let also add some messages to Connected and Failed  states using AN_MessagePopup action. At the beginning of connection let’s also use AN_ShowPreloader action, and AN_HidePreloader when the connection is finished.

Here is a view of AN_BillingInit, RetriveInventory and Connected states.


Purchase Implementation

Now let’s implement our purchase buttons. We need to be able to purchase red sphere, green sphere and 100 coins pack. So, let’s make 3 buttons for it.

Non-Consumable Purchases

We are going to use AN_BillingEventsListener action to listen for the InventoryLoaded event. As soon as we have this event fired, it will mean that  the billing is ready to use and we can check if this in-app product has been purchased.

Listen to InventoryLoaded event:

In AN_ISProductPurchased you have to provide purchase SKU as shown on the next screenshot:

If so, go to Purchase Button state - just draw the Purchase button.

When a user clicks the buttons, we will go to Purchase state with AN_Purchase action. Provide Purchase action with ProductID for purchasing.

Make sure you have set up products in plugin settings. If you haven’t done this yet, go to Windows → Android Native → Edit Settings and add your products ids that you have created earlier in Google Play Console.

One more important thing that on the Purchased state we are not actually unlocking the product or adding the coins. In my case I will simply show the pop up:

Consumable Purchases

There is a difference between consumable and non-consumable purchases. For non-consumable purchases we load inventory and check if product has been purchased. If it has been purchased - go to necessary logic (unique for every app), otherwise - we can purchase it.

In the case of consumable purchase, we check billing connection at first. If the connection is successful - purchase and consume product.

PlayMaker actions for comsumable product are shown below:

Use AN_BillingEventsListener for BillingConnected event in BillingEvents action:

Purchase and consume product with AN_PurchaseAndConsume action if Purchase button is clicked. You just need to provide comsumable product ID:

As in the previous examples, some Popup messages are used in this case too just to notify for successful or failed purchase.


Billing Controller

So, now we are going to the most important part. The purchase manager or actually billing controller. Here is how it looks like:

Starting from Inventory Loaded State action we start to check our consumable product states from the PlayerPrefs.

When our products states have loaded in our local variables, we can update the UI (or any other game play logic) according to our products  states.

In the case of coins, our coins amount has been already loaded in, so we need simply to convert it to String and update the UI:

For non-consumable products we need to check if some of this products were purchased using AN_ISProductPurchased action:

Anyway, (success or fail event fired) we can update UI or implement any other game logic.

In our case we update UI text by activating or deactivating game objects on the scene:

So far we have:

  • Connecting

  • Runing purchase and restore action

  • Loading produc states

  • Update Game UI according to the product state

The only thing is left is to listen for purchase/restore events and update PlayerPrefs depending on product id.

That’s why when our product update has been finished, we are going to Listen For Billing Events state.

First of all, we will go to Check If Coins Purchased state and check if that product was coins. We are cheking it because we save our consumable item state in a bit different way, point is we need to update coins amount instead of just saving purchased flag for our red and green sphere products.

So, we are checking current BoughtProductId with coins_item_id  and if they are equal, we are going to add 100 for our current coins amount and save it to PlayerPrefs:

Other non-consumable products we check using AN_ISProductPurchased action (as was described earlier).


That’s it! Consumable, Non-Consumable products have been just implemented in our game)