/// <summary> /// Overload for purchasing virtual product based on its product identifier. /// </summary> public static void PurchaseProduct(IAPObject obj) { string productId = obj.id; //product is set to already owned, this should not happen if (obj.type != ProductType.Consumable && DBManager.GetPurchase(productId) > 0) { OnPurchaseFailed("Product already owned."); return; } #if PLAYFAB && !PLAYFAB_VALIDATION new PlayfabStore().Purchase(obj); return; #endif //check whether the player has enough funds bool didSucceed = DBManager.VerifyVirtualPurchase(obj); if (isDebug) { Debug.Log("Purchasing virtual product " + productId + ", result: " + didSucceed); } //on success, non-consumables are saved to the database. This automatically //saves the new substracted fund value, then and fire the succeeded event if (didSucceed) { if (obj.type == ProductType.Consumable) { DBManager.IncreasePlayerData(productId, obj.usageCount); purchaseSucceededEvent(productId); } else { DBManager.SetPurchase(obj.id); } purchaseSucceededEvent(productId); } else { OnPurchaseFailed("Insufficient funds."); } }
//returning products retrieved by the billing system back to Unity IAP private void OnProductsRetrieved(BillingProduct[] list, string error) { if (!string.IsNullOrEmpty(error)) { OnSetupFailed(error); return; } string itemId = null; for (int i = 0; i < list.Length; i++) { itemId = list[i].ProductIdentifier; if (!products.ContainsKey(itemId)) { products.Add(itemId, new ProductDescription(itemId, new ProductMetadata(list[i].LocalizedPrice, list[i].Name, list[i].Description, list[i].CurrencyCode, (decimal)list[i].Price))); } if (PlayerPrefs.HasKey(itemId)) { products[itemId] = new ProductDescription(itemId, products[itemId].metadata, DBManager.GetReceipt(itemId), ""); } //auto restore products in case database does not match #if UNITY_ANDROID string globalId = IAPManager.GetIAPIdentifier(itemId); if (NPBinding.Billing.IsProductPurchased(list[i]) && DBManager.GetPurchase(globalId) == 0) { DBManager.SetPurchase(globalId); } #endif } callback.OnProductsRetrieved(products.Values.ToList()); }