/** * 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"); } }
/** * Restores the Unlockable purchases. * * The callback onDone is invoked when the entire process is over or when failed. * The callback onRestoreProduct is called for each product restored. * Use the functions to changes to the gamestate or the game's data upon restoring old unlockable purcahses. */ public static void RestorePurchases(System.Action <bool /*success*/> onDone, System.Action <string /*ProductId*/> onRestoreProduct) { Billing.RestorePurchases((bool ok, string msg) => { if (onDone != null) { onDone(ok); } }, (string productid) => { if (onRestoreProduct != null) { onRestoreProduct(productid); } }); }
Billing() : base() { inst = this; }
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 }
/** * Sets up all infomration used to do native billing. */ void Awake() { if (Application.isEditor && this.gameObject.GetComponent <BillingSimulator>() == null) { this.gameObject.AddComponent <BillingSimulator>(); } DontDestroyOnLoad(this.gameObject); inst = this; if (initializing == true) { throw new System.Exception("MobyShop: Cannot initialize twice."); } initializing = true; #if !UNITY_EDITOR Debug.Log("MobyShop: Init"); Debug.Log("MobyShop: Base64 License Code : " + AndroidPurchaseLicenseBase64); Debug.Log("MobyShop: ProductId List ( Unlockables )" + string.Join(",", ProductIdsOfUnlockables)); Debug.Log("MobyShop: ProductId List ( Consumeables ) " + string.Join(",", ProductIdsOfConsumeables)); Debug.Log("MobyShop: this.Object " + this.gameObject.name); #endif initialized = false; if (Application.isEditor || ShopConfig.instance.SimulateBillingOnDevice) { BillingSimulator.Init(( bool ok ) => { if (MobyShop.Shop.Verbose) { Debug.Log("MobyShop: Initialized Billing simulator"); } }); return; } #if UNITY_IOS BillingiOS.Billing_Init(inst.name, string.Join(",", ProductBillingIdListForRealProducts), NativeCallback.Create(( Hashtable res ) => { initializing = false; bool ok = res.ContainsKey("ok") ? System.Convert.ToBoolean(res["ok"]) : false; string msg = res.ContainsKey("msg") ? System.Convert.ToString(res["msg"]) : ""; Debug.Log("MobyShop: Store Init Done; ok=" + ok + " msg = '" + msg + "'"); if (ok) { initialized = true; } else { } })); initializing = false; initialized = true; #elif UNITY_ANDROID initializing = true; BillingAndroid.Init( AndroidPurchaseLicenseBase64, ProductIdsOfUnlockables, ProductIdsOfConsumeables, this.gameObject.name, NativeCallback.Create(( Hashtable res ) => { initializing = false; bool ok = res.ContainsKey("ok") ? System.Convert.ToBoolean(res["ok"]) : false; string msg = res.ContainsKey("msg") ? System.Convert.ToString(res["msg"]) : ""; Debug.Log("MobyShop: Store Init Done; ok=" + ok + " msg = '" + msg + "'"); if (ok) { initialized = true; } else { } }) ); #else UnsupportedPlatform(); #endif }