PaymentManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using UnityEngine;
  2. using UnityEngine.Purchasing;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using LitJson;
  6. using UnityEngine.Purchasing.Security;
  7. public class PaymentManager : IStoreListener
  8. {
  9. private IStoreController controller;
  10. private IExtensionProvider extensions;
  11. private CrossPlatformValidator validator;
  12. public PaymentManager ()
  13. {
  14. var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  15. builder.AddProduct("VIP4", ProductType.Subscription, new IDs
  16. {
  17. {"VIP4_EN", GooglePlay.Name},
  18. {"VIP4_EN", AppleAppStore.Name}
  19. });
  20. builder.AddProduct("C10000", ProductType.Consumable, new IDs
  21. {
  22. {"C10000_EN", GooglePlay.Name},
  23. {"C10000_EN", AppleAppStore.Name}
  24. });
  25. builder.AddProduct("C25000", ProductType.Consumable, new IDs
  26. {
  27. {"C25000_EN", GooglePlay.Name},
  28. {"C25000_EN", AppleAppStore.Name}
  29. });
  30. builder.AddProduct("C75000", ProductType.Consumable, new IDs
  31. {
  32. {"C75000_EN", GooglePlay.Name},
  33. {"C75000_EN", AppleAppStore.Name}
  34. });
  35. #if RECEIPT_VALIDATION
  36. validator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), Application.bundleIdentifier);
  37. #endif
  38. UnityPurchasing.Initialize (this, builder);
  39. }
  40. public void OnInitialized (IStoreController controller, IExtensionProvider extensions)
  41. {
  42. this.controller = controller;
  43. this.extensions = extensions;
  44. }
  45. /// <summary>
  46. /// Called when Unity IAP encounters an unrecoverable initialization error.
  47. ///
  48. /// Note that this will not be called if Internet is unavailable; Unity IAP
  49. /// will attempt initialization until it becomes available.
  50. /// </summary>
  51. public void OnInitializeFailed (InitializationFailureReason error)
  52. {
  53. Debuger.Log("Billing failed to initialize!");
  54. switch (error)
  55. {
  56. case InitializationFailureReason.AppNotKnown:
  57. Debuger.LogError("Is your App correctly uploaded on the relevant publisher console?");
  58. break;
  59. case InitializationFailureReason.PurchasingUnavailable:
  60. // Ask the user if billing is disabled in device settings.
  61. Debuger.Log("Billing disabled!");
  62. break;
  63. case InitializationFailureReason.NoProductsAvailable:
  64. // Developer configuration error; check product metadata.
  65. Debuger.Log("No products available for purchase!");
  66. break;
  67. }
  68. Toast.MakeText(Language.GetStr("Shop", "payFail")+" !");
  69. }
  70. public void OnPurchaseClicked(string productId)
  71. {
  72. if (controller != null) {
  73. // ProgressPanel.Show(Language.GetStr("Shop", "payBegin"));
  74. controller.InitiatePurchase (productId);
  75. }
  76. else
  77. Toast.MakeText(Language.GetStr("Shop", "payFail")+" !!");
  78. }
  79. /// <summary>
  80. /// Called when a purchase completes.
  81. ///
  82. /// May be called at any time after OnInitialized().
  83. /// </summary>
  84. public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e)
  85. {
  86. Debug.Log("Purchase OK: " + e.purchasedProduct.definition.id);
  87. Debug.Log("Receipt: " + e.purchasedProduct.receipt);
  88. // #if RECEIPT_VALIDATION
  89. // Local validation is available for GooglePlay and Apple stores
  90. if (Application.platform == RuntimePlatform.Android ||
  91. Application.platform == RuntimePlatform.IPhonePlayer ||
  92. Application.platform == RuntimePlatform.OSXPlayer ||
  93. Application.platform == RuntimePlatform.tvOS) {
  94. try {
  95. var result = validator.Validate(e.purchasedProduct.receipt);
  96. Debug.Log("Receipt is valid. Contents:");
  97. foreach (IPurchaseReceipt productReceipt in result) {
  98. Debug.Log(productReceipt.productID);
  99. Debug.Log(productReceipt.purchaseDate);
  100. Debug.Log(productReceipt.transactionID);
  101. GooglePlayReceipt google = productReceipt as GooglePlayReceipt;
  102. if (null != google) {
  103. Debug.Log(google.purchaseState);
  104. Debug.Log(google.purchaseToken);
  105. }
  106. AppleInAppPurchaseReceipt apple = productReceipt as AppleInAppPurchaseReceipt;
  107. if (null != apple) {
  108. Debug.Log(apple.originalTransactionIdentifier);
  109. Debug.Log(apple.subscriptionExpirationDate);
  110. Debug.Log(apple.cancellationDate);
  111. Debug.Log(apple.quantity);
  112. }
  113. }
  114. } catch (IAPSecurityException) {
  115. Debug.Log("Invalid receipt, not unlocking content");
  116. return PurchaseProcessingResult.Complete;
  117. }
  118. }
  119. // #endif
  120. return PurchaseProcessingResult.Complete;
  121. }
  122. /// <summary>
  123. /// Called when a purchase fails.
  124. /// </summary>
  125. public void OnPurchaseFailed (Product i, PurchaseFailureReason p)
  126. {
  127. // if (p == PurchaseFailureReason.) {
  128. // // IAP may be disabled in device settings.
  129. // }
  130. Toast.MakeText(Language.GetStr("Shop", "payFail")+" !!!");
  131. ProgressPanel.Hide();
  132. }
  133. public void IOSPayment(string result)
  134. {
  135. string[] strArr = StringUtil.Split(result, '|');
  136. string itemCode = strArr[0];
  137. string receipt = strArr.Length > 1 ? strArr[1] : "none";
  138. UserData userData = Session.GetInstance ().myUserData;
  139. URLRequestData data = new URLRequestData(true);
  140. data.Add("userid", userData.id);
  141. data.Add("item", itemCode);
  142. data.Add("receipt", receipt);
  143. URLRequest.CreateURLRequest(NetworkManager.GetURL(NetworkManager.URL.IOSPayment), data, (JsonData json)=>{
  144. int resCode = JsonUtil.ToInt(json["c"]);
  145. if(resCode == 0)
  146. {
  147. JsonData info = json["d"];
  148. if(JsonUtil.ContainKey(info, "d"))
  149. {
  150. userData.diamond = JsonUtil.ToInt(info["d"]);
  151. }
  152. if(JsonUtil.ContainKey(info, "c"))
  153. {
  154. userData.coin = JsonUtil.ToInt(info["c"]);
  155. }
  156. if(JsonUtil.ContainKey(info, "v"))
  157. {
  158. userData.vipExpireTime = info["v"].ToString();
  159. }
  160. BuyUtil.Bought(itemCode);
  161. }
  162. ProgressPanel.Hide();
  163. }, URLRequest.Method.POST);
  164. }
  165. private static PaymentManager instance;
  166. public static PaymentManager GetIntance()
  167. {
  168. if(instance == null)
  169. {
  170. instance = new PaymentManager ();
  171. }
  172. return instance;
  173. }
  174. }