IAP (In‐App‐Purchasing) - VirtueSky/sunflower GitHub Wiki

Use

Open tab In App Purchase in Magic Panel

Unity_sAPyn7oNwi

  • Add id iap into Skus Data and Generate Product From SKusData to generate IapDataVariable
  • Create Iap Purchase Product Event
  • Create Iap is Purchase Product Event

(Note: Remove Iap Purchase Product Event and Iap is Purchase Product Event from version 3.2.2)

  • Attach UnityServiceInitialization to scene to initialize unity service

Handle Purchase Success / Fail

  • Create script PurchaseSuccess inherit from IapPurchaseSuccess and PurchaseFail inherit from IapPurchaseFailed
  • Demo Script Purchase Success

using UnityEngine;
using VirtueSky.Iap;

[CreateAssetMenu(menuName = "IapRemoveAdsSuccess", fileName = "iap_remove_ads_success")]
public class IapRemoveAdsSuccess : IapPurchaseSuccess
{
    public override void Raise()
    {
        //Handle Remove Ads
        Debug.Log("Remove Ads success");
    }
}

  • Demo Script Purchase Fail

using UnityEngine;
using VirtueSky.Iap;

[CreateAssetMenu(menuName = "IapRemoveAdsFail", fileName = "iap_remove_ads_fail")]
public class IapRemoveAdsFail : IapPurchaseFailed
{
    public override void Raise()
    {
        UnityEngine.Debug.Log("Iap Remove Ads Fail");
    }
}

  • Create and attach scriptable PurchaseSuccess/PurchaseFail to IapDataVariable Screenshot 2024-02-16 173956

Attach IapManager to the object in the scene

iapM

Handle Script to use

  • Demo Script

using UnityEngine;
using VirtueSky.Iap;

public class IapTest : MonoBehaviour
{
    public IapDataVariable removeAds;


    public void BuyRemoveAds()
    {
        removeAds.OnPurchaseCompleted(() => { Debug.Log("Remove Ads Completed"); }).OnPurchaseFailed((reason) => { Debug.Log($"Remove ads faild, reason: {reason}"); }).Purchase();
    }

    public bool IsBuyRemoveAds()
    {
        return removeAds.IsPurchased();
    }
}

  • Note: You should use OnPurchaseCompleted() callback for ui setup or tracking after successful purchase. This callback is only called once when you click to buy

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 via method ConfirmPendingPurchase call in 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

  • Call Restore iOS
   IapManager.Restore();