Skip to content
Aprius edited this page Oct 3, 2024 · 16 revisions

Easy IAP in Unity

Enable IAP module via menu Tools > Pancake > Wizard (shortcut Shift + W)

Select menu InAppPurchase in left side menu

After Install In-App-Purchase Package

You must create IAPSettings via button Create IAP Setting. Default IAPSettings was create at path Assets/_Root/Resources

Settings

image

  1. Skus Data : List of product id

    • Id : product id for current platform.
    • Product Type:
      • Consumable : (pay everytime) A consumable In-App Purchase must be purchased every time the user downloads it. One-time services, such as fish food in a fishing app, are usually implemented as consumables.
      • Non-Consumable : (one time payment) Non-consumable In-App Purchases only need to be purchased once by users. Services that do not expire or decrease with use are usually implemented as non-consumables, such as new race tracks for a game app.
      • Subscriptions : (will deduct money from your credit card on a cycle complete) Subscriptions allow the user to purchase updating and dynamic content for a set duration of time. Subscriptions renew automatically unless the user opts out, such as magazine subscriptions.
  2. Button Validate Obfuscator

You need enter google play store public key of you app and click button ValidateObfuscator to generate. This is required to validate in-app purchases. If you ignore the transaction will always return the status as failed

  1. Button Generate Product:

Generate all product as asset contains event purchase success and purchase faild. You need to fill in two fields OnPurchaseSuccess and OnPurchaseFaild to process for purchase

image

The input data type here will be IAPpurchaseSuccess and IAPpurchaseFaild these are two abstract classes so you need to create inherited classes to handle separate transactions.

You need to implement Raise method and do separate handling here.The processing here should be in terms of data only. You must add attribute [CreateAssetMenu] to create asset because you need pass it on to field OnPurchaseSuccess and OnPurchaseFaild

Example:

[CreateAssetMenu]
[EditorIcon("scriptable_event_listener")]
public class IAPRemoveAdPurchaseSuccess : IAPPurchaseSuccess
{
    public override void Raise()
    {
        // todo something
        // Make sure they are only executed once
        if (Data.UserData.IsRemoveAd)
        {
          Data.UserData.IsRemoveAd = true;
          Debug.Log("Remove Ad Completed!");
        }
    }
}
[CreateAssetMenu]
[EditorIcon("scriptable_event_listener")]
public class IAPRemoveAdPurchaseFaild : IAPPurchaseFaild
{
    public override void Raise(string reason)
    {
        // todo something
        Debug.Log("Remove Ad Faild: " + reason);
    }
}

Purchase

You need place IAPManager in persistance scene. Like this

image

Anywhere, whenever you need to make a purchase, you need Product wanna purchase

Ex in menu scene we had button remove ad.

In MenuController you declare something like this:

[SerializeField] private IAPDataVariable productRemoveAd;
...

public void RemoveAd()
{
  productRemoveAd
    .OnPurchaseCompleted(() => Debug.Log("[RemoveAd] OnPurchaseCompleted  Callback"))
    .OnPurchaseFailed(reason => Debug.Log("[RemoveAd] OnPurchaseFailed Callback: " + reason))
    .Purchase();
}

Note: The user interface update operations after a successful purchase should be placed in the .OnPurchaseCompleted callback.

To check if the user owns the item you can do the following

[SerializeField] private IAPDataVariable productRemoveAd;
...

public bool IsPurchaseRemoveAds()
{
   return removeAdVariable.IsPurchased();
}

Restore Purchase

Restore purchase only applies to Non-Consumable items

Restore Purchase is a mandatory feature on iOS to be able to be released to the store.

On Android when you successfully purchased RemoveAds. Then you uninstall your game and reinstall it. If you click buy remove ads again, google play system will report that you already own this item and can't buy it again, now the user has removed ads but the game still displays ads (incorrect). We will need to handle restore purchase of the user's purchased items so that the user avoids this situation.

On Android restore purchase will be called automatically when you reinstall the game through checking if there is an receipt and whether it is valid in method OnInitialized. On ios you will need to create a restore purchase button for the user to click

When the restore is successful, it will automatically call the successful purchase callback of each item for further processing for the user

Clone this wiki locally