Exemplo n.º 1
0
        // set product to purchased after successful verification (or without)
        // Consumable IAPs must be consumed
        // For non consumable IAPs or subscriptions, alter database entry
        private void PurchaseVerified(string id)
        {
            if (!IAPObjects.ContainsKey(id))
            {
                return;
            }
            IAPObject obj = IAPObjects[id];

            if (obj.type == IAPType.consumable)
            {
                OpenIAB.consumeProduct(inventory.GetPurchase(obj.GetIdentifier()));                 //local id
                return;
            }

            //don't continue if the product is already purchased,
            //for example if we just want to verify an existing product again
            if (DBManager.isPurchased(id))
            {
                return;
            }

            if (obj.type == IAPType.nonConsumable || obj.type == IAPType.subscription)
            {
                DBManager.SetToPurchased(id);
            }

            purchaseSucceededEvent(id);
        }
Exemplo n.º 2
0
        // initialize IAP ids:
        // populate IAP dictionary and arrays with product ids
        private void InitIds()
        {
            //create temporary list for all IAPGroups,
            //as well as a list only for real money purchases
            List <IAPGroup> idsList = GetIAPs();
            List <string>   ids     = new List <string>();

            if (idsList.Count == 0)
            {
                Debug.LogError("Initializing IAPManager, but IAP List is empty."
                               + " Did you set up IAPs in the IAP Settings?");
            }

            //loop over all groups
            for (int i = 0; i < idsList.Count; i++)
            {
                //cache current group
                IAPGroup group = idsList[i];
                //loop over items in this group
                for (int j = 0; j < group.items.Count; j++)
                {
                    //cache item
                    IAPObject obj = group.items[j];
                    if (String.IsNullOrEmpty(obj.id))
                    {
                        Debug.LogError("Found IAP Object in IAP Settings without an ID."
                                       + " This will cause errors during runtime.");
                    }

                    //add this IAPObject to the dictionary of id <> IAPObject
                    IAPObjects.Add(obj.id, obj);
                    //if it's an IAP for real money, also add it to the id list
                    if (obj.type == IAPType.consumable || obj.type == IAPType.nonConsumable ||
                        obj.type == IAPType.subscription)
                    {
                        ids.Add(obj.GetIdentifier());
                    }
                }
            }

            //don't add the restore button to the list of online purchases
            if (ids.Contains("restore"))
            {
                ids.Remove("restore");
            }
            //convert and store list of real money IAP ids as string array,
            //this array is being used for initializing Google/Apple's billing system
            this.ids = ids.ToArray();
        }
Exemplo n.º 3
0
        //reads the saved data from the device
        //and initializes it into memory
        void InitDB()
        {
            //create new JSON data
            gameData = new JSONClass();

            //look up existing playerpref
            if (PlayerPrefs.HasKey(prefsKey))
            {
                //read existing data string
                string data = PlayerPrefs.GetString(prefsKey);
                //read data into memory
                //if encryption is enabled, decrypt before reading
                if (encrypt)
                {
                    gameData = JSON.Parse(Decrypt(data));
                }
                else
                {
                    gameData = JSON.Parse(data);
                }
            }

            //get all product ids, real and virtual
            //in case a IAPManager reference exists in the scene
            string[] IAPs = new string[0];
            if (IAPManager.GetInstance())
            {
                IAPs = IAPManager.GetIAPKeys();
            }
            else
            {
                return;
            }

            //delete legacy entries which were
            //removed in the IAP Settings editor
            if (!keepLegacy)
            {
                //create new string array for all existing entries
                //on the device and copy paste them to this array
                string[] entries = new string[gameData[content].Count];
                gameData[content].AsObject.Keys.CopyTo(entries, 0);
                //loop over entries
                for (int i = 0; i < entries.Length; i++)
                {
                    //cache entry and find corresponding
                    //IAPObject of the IAP Settings editor
                    string    id  = IAPManager.GetIAPIdentifier(entries[i]);
                    IAPObject obj = IAPManager.GetIAPObject(id);

                    //if the IAP does not exist in the game anymore,
                    //or a non consumable has been switched to a consumable
                    //(consumable items don't go in the database)
                    if (obj == null || obj.type == IAPType.consumable ||
                        obj.type == IAPType.consumableVirtual)
                    {
                        //remove this item id from contents
                        gameData[content].Remove(id);
                        //in case it was a selected one,
                        //loop over selected groups and delete that one too
                        for (int j = 0; j < gameData[selected].Count; j++)
                        {
                            if (gameData[selected][j].ToString().Contains(id))
                            {
                                gameData[selected][j].Remove(id);
                            }
                        }
                    }
                }

                //do the same with currency entries
                entries = new string[gameData[currency].Count];
                gameData[currency].AsObject.Keys.CopyTo(entries, 0);
                //get all currencies defiend in the IAP Editor
                List <IAPCurrency> currencies = IAPManager.GetCurrency();
                List <string>      curNames   = new List <string>();
                for (int i = 0; i < currencies.Count; i++)
                {
                    curNames.Add(currencies[i].name);
                }

                //loop over entries
                for (int i = 0; i < entries.Length; i++)
                {
                    //cache currency name
                    string id = entries[i];
                    //if it does not exist in the game anymore,
                    //remove this currency from the device
                    if (!curNames.Contains(id))
                    {
                        gameData[currency].Remove(id);
                    }
                }
            }

            //initialize IAP items
            //which aren't saved in the database yet
            //loop over all IAPs, real and virtual
            for (int i = 0; i < IAPs.Length; i++)
            {
                //cache entry and find corresponding
                //IAPObject of the IAP Settings editor
                string    id  = IAPs[i];
                IAPObject obj = IAPManager.GetIAPObject(id);

                //version 2.2 backwards compatibility fix (local ids in gameData)
                if (string.IsNullOrEmpty(gameData[content][id]) &&
                    obj.type != IAPType.consumableVirtual && obj.type != IAPType.nonConsumableVirtual)
                {
                    string localId = obj.GetIdentifier();
                    if (id != localId && !string.IsNullOrEmpty(gameData[content][localId]))
                    {
                        gameData[content][id].AsBool = gameData[content][localId].AsBool;
                        gameData[content].Remove(localId);
                    }
                }

                //check if the id doesn't exist already within contents
                if (string.IsNullOrEmpty(gameData[content][id]))
                {
                    //initialize new real money non consumable product or subscription
                    //as boolean - not purchased yet (false)
                    if (obj.type == IAPType.nonConsumable ||
                        obj.type == IAPType.subscription)
                    {
                        gameData[content][id].AsBool = false;
                    }
                    //when initializing virtual non consumable products,
                    //we first check the required virtual price to purchase
                    //them, as they could start as purchased without a price
                    else if (obj.type == IAPType.nonConsumableVirtual)
                    {
                        //start bool as purchased, then check requirements
                        bool startsPurchased = true;
                        //loop over prices
                        for (int j = 0; j < obj.virtualPrice.Count; j++)
                        {
                            //if we find one price that is greater than zero,
                            //the virtual product can't start as purchased
                            if (obj.virtualPrice[j].amount > 0)
                            {
                                startsPurchased = false;
                                break;
                            }
                        }
                        //initialize new virtual non consumable product
                        //with the boolean set earlier
                        gameData[content][id].AsBool = startsPurchased;
                    }
                }
            }

            //initialize list of currencies and loop through them
            List <IAPCurrency> curs = IAPManager.GetCurrency();

            for (int i = 0; i < curs.Count; i++)
            {
                //cache currency name
                string cur = curs[i].name;
                //don't create an empty currency name
                if (string.IsNullOrEmpty(cur))
                {
                    Debug.LogError("Found Currency in IAP Settings without a name. "
                                   + "The database will not know how to save it. Aborting.");
                    return;
                }
                //check if the currency doesn't exist already within currencies,
                //then set the initial amount to the value entered in IAP Settings editor
                if (string.IsNullOrEmpty(gameData[currency][cur]))
                {
                    gameData[currency][cur].AsInt = curs[i].amount;
                }
            }

            //save modified data on the device
            Save();
        }
Exemplo n.º 4
0
        //each VirtualGood gets created seperately
        VirtualGood CreateGood(IAPObject obj, bool market)
        {
            PurchaseType purchaseType = null;
            if (market)
                purchaseType = new PurchaseWithMarket(obj.GetIdentifier(), 0.00);
            else
                purchaseType = new PurchaseWithVirtualItem(obj.virtualPrice.name, obj.virtualPrice.amount);

            switch (obj.type)
            {
                case IAPType.SingleUseVG:
                    return new SingleUseVG(obj.title, obj.description, obj.id, purchaseType);
                case IAPType.SingleUsePackVG:
                    return new SingleUsePackVG(obj.specific, obj.amount, obj.title, obj.description, obj.id, purchaseType);
                case IAPType.LifetimeVG:
                    return new LifetimeVG(obj.title, obj.description, obj.id, purchaseType);
                case IAPType.EquippableVG:
                    EquippableVG.EquippingModel equipModel = EquippableVG.EquippingModel.LOCAL;
                    if (obj.specific == EquippableVG.EquippingModel.CATEGORY.ToString()) equipModel = EquippableVG.EquippingModel.CATEGORY;
                    else if (obj.specific == EquippableVG.EquippingModel.GLOBAL.ToString()) equipModel = EquippableVG.EquippingModel.GLOBAL;
                    return new EquippableVG(equipModel, obj.title, obj.description, obj.id, purchaseType);
                case IAPType.UpgradeVG:
                    string[] props = obj.specific.Split(';');
                    return new UpgradeVG(props[0], string.IsNullOrEmpty(props[2]) ? null : props[2], string.IsNullOrEmpty(props[1]) ? null : props[1],
                                         obj.title, obj.description, obj.id, purchaseType);
                default: return null;
            }
        }
Exemplo n.º 5
0
        //each CurrencyPack gets created seperately
        VirtualCurrencyPack CreateCurrencyPack(IAPObject obj, bool market)
        {
            PurchaseType purchaseType = null;
            if(market)
                purchaseType = new PurchaseWithMarket(obj.GetIdentifier(), 0.00);
            else
                purchaseType = new PurchaseWithVirtualItem(obj.virtualPrice.name, obj.virtualPrice.amount);

            return new VirtualCurrencyPack(obj.title, obj.description, obj.id, obj.amount,
                                           obj.specific, purchaseType);
        }