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 want’s to implement In Apps in the Android Game.

To get started you need:

First of all out need to cover Google Play Setup, with is described here. After all 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 with we will make in this tutorial can be founded at: Assets/Extensions/AndroidNative/xExample/Scenes/Playmaker/BillingWithPlaymakerExample.unity

Result scene will looks like this


Now we can get started on In Apps implementation.

Product IDs

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

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

In my case result shown on next screenshot:

For this tutorial I have two non-consumable items:

  • pm_green_sphere

  • pm_red_sphere

and one consumable

  • pm_coins


Billing Initialization

At first, 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 will be connected to the billing service, and you will able to send purchase requests, Success action will be fired.

After success initialization you need AN_RetriveInventory action to retrieve inventory. This logic 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 connection is finished.

Here is view of AN_BillingInit, RetriveInventory and Connected states.


Purchase Implementation

Now let’s implement the 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 going to use AN_BillingEventsListener action to listen for the InventoryLoaded event. As soon as we will have this event fired, it means that billing is ready to use and we can check if this in-app product was 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 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 setupped 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 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 was purchased. If purchased - go to necessary logic (unique for every app), if not - we can purchase it.

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

PlayMaker actions for comsumable product shown below:

Use AN_BillingEventsListener for BillingConnected event in BillingEvents action:

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

As in the previous examples, some Popup messages using in this case to. Just to notify for success or failed purchase.


Billing Controller

So now we 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 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 was already loaded in, so we need simply convert it to String and update the UI:

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

Any way (success or fail event fired) we can update UI or implement any other our 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 listened for purchase/restore events and update PlayerPrefs depending on product id.

That’s why when our product update finished we 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 cheking it because we save our consumable item state in a but 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 checking current BoughtProductId with coins_item_id  and if it’s equals we 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 were just implemented in our game)