123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- 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());
- 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}
- });
- #if RECEIPT_VALIDATION
- validator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), Application.bundleIdentifier);
- #endif
- UnityPurchasing.Initialize (this, builder);
- }
- public void OnInitialized (IStoreController controller, IExtensionProvider extensions)
- {
- this.controller = controller;
- this.extensions = extensions;
- }
- /// <summary>
- /// 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.
- /// </summary>
- public void OnInitializeFailed (InitializationFailureReason error)
- {
- Debuger.Log("Billing failed to initialize!");
- switch (error)
- {
- case InitializationFailureReason.AppNotKnown:
- Debuger.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.
- Debuger.Log("Billing disabled!");
- break;
- case InitializationFailureReason.NoProductsAvailable:
- // Developer configuration error; check product metadata.
- Debuger.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")+" !!");
- }
- /// <summary>
- /// Called when a purchase completes.
- ///
- /// May be called at any time after OnInitialized().
- /// </summary>
- 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);
- }
- }
- } catch (IAPSecurityException) {
- Debug.Log("Invalid receipt, not unlocking content");
- return PurchaseProcessingResult.Complete;
- }
- }
- // #endif
- return PurchaseProcessingResult.Complete;
- }
- /// <summary>
- /// Called when a purchase fails.
- /// </summary>
- public void OnPurchaseFailed (Product i, PurchaseFailureReason p)
- {
- // if (p == PurchaseFailureReason.) {
- // // IAP may be disabled in device settings.
- // }
- Toast.MakeText(Language.GetStr("Shop", "payFail")+" !!!");
- ProgressPanel.Hide();
- }
- 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;
- }
- }
|