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; private ConfigurationBuilder builder; private string itemCode; public PaymentManager () { builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); List list = ShopManager.GetInstance ().GetDataList (); for(int i=0; i ().RefreshAppReceipt ((string result) => { if (!StringUtil.Empty(result)) { // Refresh finished successfully. } else { // Refresh failed. } Debug.Log("RefreshAppReceipt result "+result); if(!StringUtil.Empty(itemCode)) { OnPurchaseClicked (itemCode); } }, () => { Debug.Log("RefreshAppReceipt error"); if(!StringUtil.Empty(itemCode)) { OnPurchaseClicked (itemCode); } }); } /// /// 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) { itemCode = ""; // ProgressPanel.Show(Language.GetStr("Shop", "payBegin")); controller.InitiatePurchase (productId); } else { itemCode = productId; UnityPurchasing.Initialize (this, builder); } } /// /// 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); bool isValidation = false; // #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 { Debug.Log("validator "+validator); var result = validator.Validate(e.purchasedProduct.receipt); Debug.Log("result "+result); 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); isValidation = true; } AppleInAppPurchaseReceipt apple = productReceipt as AppleInAppPurchaseReceipt; if (null != apple) { Debug.Log(apple.originalTransactionIdentifier); Debug.Log(apple.subscriptionExpirationDate); Debug.Log(apple.cancellationDate); Debug.Log(apple.quantity); isValidation = true; } } } catch (IAPSecurityException) { Debug.Log("Invalid receipt, not unlocking content"); return PurchaseProcessingResult.Complete; } } // #endif if (isValidation) { JsonData receiptJson = JsonMapper.ToObject (e.purchasedProduct.receipt); string receipt = receiptJson ["Payload"].ToString (); Debug.Log ("Final receipt " + receipt); IOSPayment (e.purchasedProduct.definition.id, receipt); } 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; } }