Skip to main content

Programming & Callbacks

Note that most of the programming is already handled for you internally, such as:

  • Setting a non-consumable/subscription product to purchased after it has been bought
  • Granting virtual currency after a Currency product has been bought (via Project Settings)
  • Adding custom usage amounts for consumable products (via Project Settings)
  • Subtracting currency on virtual product purchases, and more

If you would like to present a nice feedback window to the user after a purchase, then that's still something you would add in the IAPListener - because you can define the text yourself. The IAPListener script has a HandleSuccessfulPurchase method, which is where you say what happens when a user buys a product, and the HandleSuccessfulConsume method which is fired when a product was consumed successfully.

IAPListener.HandleSuccessfulPurchase
case "coins_small":
//the user bought coins, get reward amount and show appropriate feedback
IAPProduct product = IAPManager.GetIAPProduct(productID);
ShowMessage(product.rewardList[0].amount + " coins were added to your balance!");
break;

case "health":
//for example, in case product was bought not in the shop,
//but during the game to receive a second chance or revive
if(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "MyGameScene")
{
//try to consume product immediately
IAPManager.Consume("health", 1);
}
else
{
ShowMessage("Medikits were added to your inventory!");
}
break;
IAPListener.HandleSuccessfulConsume
case "health":
//for example, add health to your custom player class
Player.GetInstance().AddHealth(75);
break;

Again, you do not want to handle any currency amounts or granting products in the IAPListener manually – as written above, this is all handled for you automatically. When looking at the coins_small code, note that only a message is displayed to the user – the amount is added internally.

An exception to this is if you are offering in-app purchases in a popup during the game, or maybe your shop scene is included in the game scene. The health code example above first checks for the current scene, where the product has been bought. If you are in the game scene, it tries to consume the product immediately. What should happen is then handled in HandleSuccessfulConsume.

To make the update process of UniPay easier, you might want to create your own listener script, subscribe to only the IAPManager methods needed for your game logic and attach it to the IAPManager prefab, instead of using the default IAPListener script.

There are also a range of cases where you want to call IAP methods on game launch, at certain events or checkpoints reached when playing, with some examples listed below.

Game logic, at level start
if(DBManager.IsSelected("weapon1")) //check if product was selected
Player.InstantiateWeapon(...) //give the corresponding weapon to the player

//returns remaining amount of virtual product, display it on a UI label text
label.text = DBManager.GetPurchase("energy").ToString();
Game logic, before showing ads
if(DBManager.GetPurchase("no_ads") > 0) //check if product has been bought, or
if(DBManager.IsPurchased("no_ads")) //shorthand for the same check
Game logic, during the game
IAPManager.Consume("bullets", 10); //decrease virtual product amount by 10
Game logic, at level end
DBManager.SetPlayerData("score", new SimpleJSON.JSONData(2250)); //save highscore
DBManager.AddPlayerData("xp", 100); //increase current user experience by 100
DBManager.AddCurrency("coins", 200); //increase user’s virtual currency by 200

You probably noticed that several methods make use of the DBManager. The DBManager manages our PlayerPrefs database and keeps track of purchases, selections, virtual currency and other player-related data. Basically, whenever you want to modify purchase data, you should call methods of the IAPManager. For any custom data related to the player, you should call methods of the DBManager.

Do not forget to include the namespace by adding

using UniPay;

at the top of your own scripts. There are a lot more actions and methods available for integration in your app, which should be flexible enough to cover almost all use cases related to in-app purchases. If a method can be accessed in a static context (e.g. IAPManager.xxx or DBManager.xxx), it is designed for you to make use of it.

For a full list, please visit the Scripting Reference.