/// <summary> /// Sets a product to purchased after successful verification (or without). /// This alters the database entry for non-consumable or products with usage as well. /// </summary> public void PurchaseVerified(string id) { if (!IAPObjects.ContainsKey(id)) { id = GetIAPIdentifier(id); } if (!IAPObjects.ContainsKey(id)) { return; } IAPObject obj = IAPObjects[id]; switch (obj.editorType) { case IAPType.Currency: foreach (IAPCurrency cur in obj.virtualPrice) { DBManager.IncreaseFunds(cur.name, cur.amount); } break; default: //for consumables, add the defined usage count to tracking in player data //on non-consumables, don't continue if the product is already purchased, //for example if we just want to verify an existing product again switch (obj.type) { case ProductType.Consumable: if (obj.usageCount > 0) { DBManager.IncreasePlayerData(id, obj.usageCount); //update server player data #if PLAYFAB && !PLAYFAB_VALIDATION PlayfabManager.SetPlayerData(); #endif } break; default: if (DBManager.GetPurchase(id) > 0) { return; } DBManager.IncreasePurchase(id, 1); break; } break; } purchaseSucceededEvent(id); }
/// <summary> /// Callback from the billing system when a (virtual) purchase completes successfully. /// </summary> public void OnPurchaseSucceeded(PurchaseItemResult result) { ItemInstance item = result.Items[0]; Dictionary <string, int> currencies = DBManager.GetCurrencies(); //substract purchase price from the virtual currency locally //this is only for display purposes, as the funds are maintained on the server foreach (KeyValuePair <string, int> pair in currencies) { if (pair.Key.StartsWith(item.UnitCurrency.ToLower())) { DBManager.IncreaseFunds(pair.Key, (int)(-item.UnitPrice)); break; } } //can't call the native callback because PlayFab returns the same ItemInstanceId for stackable products //this would result in an Unity IAP 'Already recorded transaction' message thus not doing anything //callback.OnPurchaseSucceeded(item.ItemId, item.PurchaseDate.ToString(), item.ItemInstanceId); //instead we call the finish events ourselves IAPManager.GetInstance().PurchaseVerified(item.ItemId); FinishTransaction(IAPManager.controller.products.WithID(item.ItemId).definition, item.ItemInstanceId); }
/// <summary> /// handle purchases, for real money or ingame currency /// </summary> public void HandleSuccessfulPurchase(string id) { //differ between ids set in the IAP Settings editor if (IAPManager.isDebug) { Debug.Log("HandleSuccessfulPurchase: " + id); } IAPObject obj = IAPManager.GetIAPObject(id); //get instantiated shop item based on the IAP id IAPItem item = null; if (ShopManager.GetInstance()) { item = ShopManager.GetIAPItem(id); } //if the purchased item was non-consumable, //additionally block further purchase of the shop item if (item != null && obj != null && obj.type != ProductType.Consumable) { item.Purchased(true); } switch (id) { //section for in app purchases case "coins": //the user bought the item "coins", //increase coins by 1000 and show appropriate feedback DBManager.IncreaseFunds("coins", 1000); ShowMessage("1000 coins were added to your balance!"); break; case "coin_pack": DBManager.IncreaseFunds("coins", 2500); ShowMessage("2500 coins were added to your balance!"); break; case "big_coin_pack": DBManager.IncreaseFunds("coins", 6000); ShowMessage("6000 coins were added to your balance!"); break; case "huge_coin_pack": DBManager.IncreaseFunds("coins", 12000); ShowMessage("12000 coins were added to your balance!"); break; case "no_ads": //no_ads purchased. You can now check DBManager.isPurchased("no_ads") //before showing ads and block them ShowMessage("Ads disabled!"); break; case "abo_monthly": //same here - your code to unlock subscription content ShowMessage("Subscribed to monthly abo!"); break; case "restore": //nothing else to call here, //the actual restore is handled by IAPManager ShowMessage("Restored transactions!"); break; //section for in game content case "bullets": //for virtual items, you could use DBManager's custom data option in order //to save amounts of virtual products. E.g. increasing bullets by 100: //int bullets = DBManager.GetPlayerData("bullets").AsInt; //DBManager.SetPlayerData("bullets", new SimpleJSON.JSONData(bullets + 100)); ShowMessage("Bullets were added to your inventory!"); break; case "health": ShowMessage("Medikits were added to your inventory!"); break; case "energy": ShowMessage("Energy was added to your inventory!"); break; case "speed": ShowMessage("Speed boost unlocked!"); break; case "speed_1": case "speed_2": case "speed_3": ShowMessage("Speed boost upgraded!"); break; case "bonus": ShowMessage("Bonus level unlocked!"); break; case "uzi": ShowMessage("Uzi unlocked!"); break; case "ak47": ShowMessage("AK47 unlocked!"); break; case "m4": ShowMessage("M4 unlocked!"); break; case "hat": ShowMessage("Hat unlocked!"); break; case "backpack": ShowMessage("Backpack unlocked!"); break; case "belt": ShowMessage("Ammo belt unlocked!"); break; case "jetpack": ShowMessage("Jetpack unlocked!"); break; case "booster": ShowMessage("Double XP unlocked!"); break; } }