public static void BuyProduct(ProductInfo product, System.Action <bool, string, Shop.BuyResponse> callback, ShopConfirm shopConfirmInterface, ShopNotEnoughCoins notEnoughCoinsInterface) { string billingid = product.BillingId; if (MobyShop.Shop.Verbose) { Debug.Log("MobyShop: Buy Product : " + product.ToString()); } if (product.billing == BillingType.IngameCurrency) { BillingUnityEditor_BuyProductInGameCurrency(product, callback, shopConfirmInterface, notEnoughCoinsInterface); return; } // isEditor... if (Application.isEditor || ShopConfig.instance.SimulateBillingOnDevice) { if (MobyShop.Shop.Verbose) { Debug.Log("MobyShop: Buying product with billing simulation turned on."); } BillingUnityEditor_BuyProductWithSimulatedBilling(product, callback); return; } // Has Recieved the Product catalogue. if (!HasRecvProductCatalogue) { Debug.LogError("MobyShop: has not recieved product catalogue yet, and products cannot be bought."); if (callback != null) { callback(false, "no product catalogue recieved and you cannot make purchase", Shop.BuyResponse.Failed); } Billing.ShowError("Product Catalogue not recieved yet"); return; } if (buyingProduct) { Debug.LogError("MobyShop: Store: Already buying another project"); } buyingProduct = true; Debug.Log("MobyShop: Store: Purchase product : " + billingid + " (init)"); #if UNITY_IOS if (iOS_BuyDeleg != null) { Debug.LogError("MobyShop: Ignoring double puchase, wait until previous is done"); if (callback != null) { callback(false, "Purchase already in progress", Shop.BuyResponse.Wait); } return; } iOS_BuyDeleg = callback; BillingiOS.Billing_BuyProduct(billingid, NativeCallback.Create((Hashtable args) => { bool ok = args.ContainsKey("ok") ? System.Convert.ToBoolean(args["ok"]) : false; string msg = args.ContainsKey("msg") ? System.Convert.ToString(args["msg"]) : ""; if (ok) { Debug.Log("MobyShop: Billing(iOS): ok=true; product bought : " + billingid + "; msg=" + msg); } else { Debug.LogError("MobyShop: Billing(iOS): ok=false; Failed to buy product; msg=" + msg); if (iOS_BuyDeleg != null) { var tmp = iOS_BuyDeleg; iOS_BuyDeleg = null; // remember to reset this one. tmp(false, msg, Shop.BuyResponse.Failed); } } /*if( callback!=null ) { * callback(ok,msg); * }*/ })); #elif UNITY_ANDROID BillingAndroid.BuyProduct(billingid, NativeCallback.Create((Hashtable args) => { bool ok = args.ContainsKey("ok") ? System.Convert.ToBoolean(args["ok"]) : false; string msg = args.ContainsKey("msg") ? System.Convert.ToString(args["msg"]) : ""; Debug.Log("MobyShop: OnBuyProduct Retuned : " + ok + " msg = " + msg); var prod = Billing.GetProductInfoByBillingId(billingid); if (prod == null) { prod = product; Debug.LogError("MobyShop: Error cannot get product wiht billingId: " + billingid); } if (ok) { if (prod == null) { Debug.LogError("MobyShop:Error unable to get the product by the given billing id : " + billingid); } CallUnlockProduct(BoughtOrRestored.Bought, prod); } else { } if (callback != null) { callback(ok, "", Shop.BuyResponse.Ok); } })); #else UnsupportedPlatform(); #endif }
/** * This method initializes the inapp purchase. */ static void BillingUnityEditor_BuyProductInGameCurrency(ProductInfo product, System.Action <bool, string, Shop.BuyResponse> callback, ShopConfirm shopConfirmInterface, ShopNotEnoughCoins notEnoughCoinsInterface) { if (MobyShop.Shop.Verbose) { Debug.Log("MobyShop: Buying product with virtual currency (" + product.ingameCurrencyClass + "); price=" + product.PriceTag + "; currency class=" + product.ingameCurrencyClass); } if (product.billing != BillingType.IngameCurrency) { throw new System.Exception("MobyShop: Billing with virtual currency is only allowed for : " + product.ProductId); } // Currency Class if (string.IsNullOrEmpty(product.ingameCurrencyClass)) { Debug.LogError("MobyShop: Error currecy class did not exist : '" + product.ingameCurrencyClass + "'"); callback(false, "Invalid product setup - no currency class given", Shop.BuyResponse.Failed); } if (ShopConfig.GetProductByClassId(product.ingameCurrencyClass) == null) { Debug.Log("MobyShop: Warning: No Product's exists with currency class id : '" + product.ingameCurrencyClass + "'\nNOTE: This might be okay if you don't intent for the user to buy the currency in the game shop"); } // Debug.Log( "MobyShop: Buy product with virtual currency (coins)" ); int amount = Mathf.Abs(Shop.GetProductClassAmount(product.ingameCurrencyClass)); if (amount < Mathf.Abs(product.price)) { //Debug.Log ("Not enough coins"); if (notEnoughCoinsInterface == null) { var uigo = instance.NotEnoughCoins; if (uigo == null) { if (Billing.instance.PrefabNotEnoughCoins == null) { Debug.LogError("MobyShop: Error 'Not Enough Coins' Dialog Prefab was not found"); callback(false, "", Shop.BuyResponse.Cancelled); return; } var goNotEn = GameObject.Instantiate(Billing.instance.PrefabNotEnoughCoins); uigo = goNotEn; } var canvas = CanvasHelper.GetCanvas(); uigo.transform.localPosition = Vector2.zero; uigo.gameObject.SetActive(true); uigo.transform.parent = canvas.transform; var rt = uigo.GetComponent <RectTransform> (); rt.FitAnchorsToCorners(); uigo.GetComponentCompatibleWith <global::MobyShop.ShopNotEnoughCoins> ().onDismissed = (global::MobyShop.ShopNotEnoughCoins.Dismissed dismissedWith) => { var response = dismissedWith == global::MobyShop.ShopNotEnoughCoins.Dismissed.BuyMoreCoins ? Shop.BuyResponse.BuyMoreCoins : Shop.BuyResponse.Cancelled; callback(false, "Not enough coins available", response); }; return; } else { notEnoughCoinsInterface.onDismissed = (ShopNotEnoughCoins.Dismissed dismissedWith) => { var response = dismissedWith == ShopNotEnoughCoins.Dismissed.BuyMoreCoins ? Shop.BuyResponse.BuyMoreCoins : Shop.BuyResponse.Cancelled; callback(false, "Not enough coins available", response); }; notEnoughCoinsInterface.Show(); return; } //return; } else { // if (shopConfirmInterface == null) { var uigo = MobyShop.Shop.BillingIngameCurrentUI; if (uigo == null) { callback(false, "Failed to get UI confirm object.", Shop.BuyResponse.Failed); return; } var canvas = CanvasHelper.GetCanvas(); uigo.transform.localPosition = Vector2.zero; uigo.gameObject.SetActive(true); uigo.transform.parent = canvas.transform; var rt = uigo.GetComponent <RectTransform> (); rt.offsetMin = rt.offsetMax = Vector2.zero; rt.anchorMin = Vector2.zero; rt.anchorMax = Vector2.one; uigo.GetComponentCompatibleWith <global::ShopConfirm> ().onDismissed = (BillingInGameCurrency.AcceptOrCancel result) => { //Debug.Log("OnBuyResult : " + result ); if (result == BillingInGameCurrency.AcceptOrCancel.Accepted) { Shop.IncrementProductClassAmount(product.ingameCurrencyClass, -Mathf.Abs(product.price)); CallUnlockProduct(BoughtOrRestored.Bought, product); callback(true, "", Shop.BuyResponse.Ok); } else { callback(false, "User cancelled", Shop.BuyResponse.Cancelled); } }; } else { shopConfirmInterface.onDismissed = (BillingInGameCurrency.AcceptOrCancel result) => { if (result == BillingInGameCurrency.AcceptOrCancel.Accepted) { Shop.IncrementProductClassAmount(product.ingameCurrencyClass, -Mathf.Abs(product.price)); CallUnlockProduct(BoughtOrRestored.Bought, product); callback(true, "", Shop.BuyResponse.Ok); } else { callback(false, "User cancelled", Shop.BuyResponse.Cancelled); } }; shopConfirmInterface.Show( ); return; } } }
/** * Initializes a session to buy a product, * The functino takes a product id, this is the id that you have configured the product to use * in the product configuration editor. * a callback can be given wihch will return true or false when he product has been buyght. * If you want to listen on the event emitted. */ public static void BuyProduct(string productId, System.Action <bool /*okay*/, string /*message*/, Shop.BuyResponse> callback, ShopConfirm confirmInterface, ShopNotEnoughCoins notEnoughCoinsInterface) { ProductInfo product = ShopConfig.GetProductByProductId(productId); if (product != null) { Billing.BuyProduct(product, (bool okay, string message, Shop.BuyResponse response) => { if (_instance != null && Verbose) { Debug.Log("MobyShop: On Buy Product Result : " + okay + " msg=" + message); } if (callback != null) { callback(okay, message, response); } //OnBuyProductResult(); if (okay && OnProductBought != null) { //ProductInfo productBought = null; OnProductBought.Invoke(BoughtOrRestored.Bought, product, product.GetClassAmount()); } }, confirmInterface, notEnoughCoinsInterface); } else { Debug.LogError("MobyShop:Error buying products"); } }