using UnityEngine; using UnityEngine.Purchasing; using System.Collections; using System.Collections.Generic; using LitJson; using UnityEngine.Purchasing.Security; public class PaymentManager : IStoreListener { private IStoreController controller; private IExtensionProvider extensions; private CrossPlatformValidator validator; public PaymentManager () { var builder = ConfigurationBuilder.Instance (StandardPurchasingModule.Instance ()); if (Config.USER_PORT == Config.USER_PORT_INTERNATIONAL) { builder.AddProduct ("VIP4", ProductType.Subscription, new IDs { { "VIP4_EN", GooglePlay.Name }, { "VIP4_EN", AppleAppStore.Name } }); builder.AddProduct ("C10000", ProductType.Consumable, new IDs { { "C10000_EN", GooglePlay.Name }, { "C10000_EN", AppleAppStore.Name } }); builder.AddProduct ("C25000", ProductType.Consumable, new IDs { { "C25000_EN", GooglePlay.Name }, { "C25000_EN", AppleAppStore.Name } }); builder.AddProduct ("C75000", ProductType.Consumable, new IDs { { "C75000_EN", GooglePlay.Name }, { "C75000_EN", AppleAppStore.Name } }); } else { builder.AddProduct ("VIP4", ProductType.Subscription, new IDs { { "VIP4", GooglePlay.Name }, { "VIP4", AppleAppStore.Name } }); builder.AddProduct ("C10000", ProductType.Consumable, new IDs { { "C10000", GooglePlay.Name }, { "C10000", AppleAppStore.Name } }); builder.AddProduct ("C25000", ProductType.Consumable, new IDs { { "C25000", GooglePlay.Name }, { "C25000", AppleAppStore.Name } }); builder.AddProduct ("C75000", ProductType.Consumable, new IDs { { "C75000", GooglePlay.Name }, { "C75000", AppleAppStore.Name } }); } #if RECEIPT_VALIDATION validator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), Application.bundleIdentifier); #endif UnityPurchasing.Initialize (this, builder); } public void OnInitialized (IStoreController controller, IExtensionProvider extensions) { Debug.Log("Billing initialize!"); this.controller = controller; this.extensions = extensions; extensions.GetExtension ().RefreshAppReceipt ((string result) => { if (!StringUtil.Empty(result)) { // Refresh finished successfully. } else { // Refresh failed. } Debug.Log("RefreshAppReceipt result "+result); }, () => { Debug.Log("RefreshAppReceipt error"); }); } /// /// Called when Unity IAP encounters an unrecoverable initialization error. /// /// Note that this will not be called if Internet is unavailable; Unity IAP /// will attempt initialization until it becomes available. /// public void OnInitializeFailed (InitializationFailureReason error) { Debug.Log("Billing failed to initialize!"); switch (error) { case InitializationFailureReason.AppNotKnown: Debug.LogError("Is your App correctly uploaded on the relevant publisher console?"); break; case InitializationFailureReason.PurchasingUnavailable: // Ask the user if billing is disabled in device settings. Debug.Log("Billing disabled!"); break; case InitializationFailureReason.NoProductsAvailable: // Developer configuration error; check product metadata. Debug.Log("No products available for purchase!"); break; } Toast.MakeText(Language.GetStr("Shop", "payFail")+" !"); } public void OnPurchaseClicked(string productId) { if (controller != null) { // ProgressPanel.Show(Language.GetStr("Shop", "payBegin")); controller.InitiatePurchase (productId); } else { Toast.MakeText (Language.GetStr ("Shop", "payFail") + " !!"); } } /// /// Called when a purchase completes. /// /// May be called at any time after OnInitialized(). /// public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e) { Debug.Log("Purchase OK: " + e.purchasedProduct.definition.id); Debug.Log("Receipt: " + e.purchasedProduct.receipt); // #if RECEIPT_VALIDATION // Local validation is available for GooglePlay and Apple stores if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.tvOS) { try { var result = validator.Validate(e.purchasedProduct.receipt); Debug.Log("Receipt is valid. Contents:"); foreach (IPurchaseReceipt productReceipt in result) { Debug.Log(productReceipt.productID); Debug.Log(productReceipt.purchaseDate); Debug.Log(productReceipt.transactionID); GooglePlayReceipt google = productReceipt as GooglePlayReceipt; if (null != google) { Debug.Log(google.purchaseState); Debug.Log(google.purchaseToken); } AppleInAppPurchaseReceipt apple = productReceipt as AppleInAppPurchaseReceipt; if (null != apple) { Debug.Log(apple.originalTransactionIdentifier); Debug.Log(apple.subscriptionExpirationDate); Debug.Log(apple.cancellationDate); Debug.Log(apple.quantity); IOSPayment(e.purchasedProduct.definition.id, e.purchasedProduct.receipt); } } } catch (IAPSecurityException) { Debug.Log("Invalid receipt, not unlocking content"); return PurchaseProcessingResult.Complete; } } // #endif return PurchaseProcessingResult.Complete; } /// /// Called when a purchase fails. /// public void OnPurchaseFailed (Product item, PurchaseFailureReason r) { Debug.Log("Purchase failed: " + item.definition.id); Debug.Log(r); Toast.MakeText(Language.GetStr("Shop", "payFail")+" !!!"); ProgressPanel.Hide(); } public void IOSPayment(string product, string receipt) { UserData userData = Session.GetInstance ().myUserData; URLRequestData data = new URLRequestData(true); data.Add("userid", userData.id); data.Add("item", product); data.Add("receipt", receipt); URLRequest.CreateURLRequest(NetworkManager.GetURL(NetworkManager.URL.IOSPayment), data, (JsonData json)=>{ int resCode = JsonUtil.ToInt(json["c"]); if(resCode == 0) { JsonData info = json["d"]; if(JsonUtil.ContainKey(info, "d")) { userData.diamond = JsonUtil.ToInt(info["d"]); } if(JsonUtil.ContainKey(info, "c")) { userData.coin = JsonUtil.ToInt(info["c"]); } if(JsonUtil.ContainKey(info, "v")) { userData.vipExpireTime = info["v"].ToString(); } BuyUtil.Bought(product); } ProgressPanel.Hide(); }, URLRequest.Method.POST); } public void IOSPayment(string result) { string[] strArr = StringUtil.Split(result, '|'); string itemCode = strArr[0]; string receipt = strArr.Length > 1 ? strArr[1] : "none"; UserData userData = Session.GetInstance ().myUserData; URLRequestData data = new URLRequestData(true); data.Add("userid", userData.id); data.Add("item", itemCode); data.Add("receipt", receipt); URLRequest.CreateURLRequest(NetworkManager.GetURL(NetworkManager.URL.IOSPayment), data, (JsonData json)=>{ int resCode = JsonUtil.ToInt(json["c"]); if(resCode == 0) { JsonData info = json["d"]; if(JsonUtil.ContainKey(info, "d")) { userData.diamond = JsonUtil.ToInt(info["d"]); } if(JsonUtil.ContainKey(info, "c")) { userData.coin = JsonUtil.ToInt(info["c"]); } if(JsonUtil.ContainKey(info, "v")) { userData.vipExpireTime = info["v"].ToString(); } BuyUtil.Bought(itemCode); } ProgressPanel.Hide(); }, URLRequest.Method.POST); } private static PaymentManager instance; public static PaymentManager GetIntance() { if(instance == null) { instance = new PaymentManager (); } return instance; } }