Search Unity

Checking to see if user bought a nonconsumable IAP on startup? [Android]

Discussion in 'Scripting' started by CogentJin, Mar 29, 2017.

  1. CogentJin

    CogentJin

    Joined:
    Feb 11, 2015
    Posts:
    10
    When my game first opens, the first level is playable while the remaining levels have lock icons on them. The nonconsumable IAP is to "Unlock full game," which I already have setup and working. The problem is when the game is closed and re-opened, the locks are back because I don't know how to have a startup check to see whether or not the IAP was bought. I have it so...

    Player spawns in main menu.
    Prefab hitboxes cover the "locked" levels like a door.
    IAP is bought once, and it destroys prefabs blocking the rest of levels.

    I didn't want to just throw something in the PlayerPrefs since it's easily modified externally, and at the same time I feel like trying to use the persistence save/load concept is the wrong way to go about doing this.

    I keep reading about the hasReceipt snippet, or maybe the Receipt validation method, but honestly I can't figure out how to implement them into an Awake function. If anyone has any advice, I'd love to hear it. Or if you can point me in the right direction I'll try to figure it the best I can. Thank you very much.
     
  2. CogentJin

    CogentJin

    Joined:
    Feb 11, 2015
    Posts:
    10
    I don't know if bumping is allowed here but I just figured I'd make an update on what I have thus far because I really thought this would be it. I threw this script on a random cube in my game just to test it out and see if it works--it doesn't. But I just can't understand where I went wrong.

    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Purchasing;

    public class IAPRestore : MonoBehaviour
    {
    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
    IAPManager.m_StoreController = controller;
    IAPManager.m_StoreExtensionProvider = extensions;
    RestoreNonConsumable();
    }

    public void RestoreNonConsumable()
    {
    if (IAPManager.m_StoreController.products.WithID("unlockgame").hasReceipt == true) //This is where it won't work.
    {
    Destroy(this);
    }
    }
    }

    I have another script called IAPManager listed for my actual purchasing (filled with Unity's example code), but I just wanted this script to check and see if the nonconsumable item was already bought.