PaymentManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. Debug.Log("Billing initialize!");
  43. this.controller = controller;
  44. this.extensions = extensions;
  45. }
  46. /// <summary>
  47. /// Called when Unity IAP encounters an unrecoverable initialization error.
  48. ///
  49. /// Note that this will not be called if Internet is unavailable; Unity IAP
  50. /// will attempt initialization until it becomes available.
  51. /// </summary>
  52. public void OnInitializeFailed (InitializationFailureReason error)
  53. {
  54. Debug.Log("Billing failed to initialize!");
  55. switch (error)
  56. {
  57. case InitializationFailureReason.AppNotKnown:
  58. Debug.LogError("Is your App correctly uploaded on the relevant publisher console?");
  59. break;
  60. case InitializationFailureReason.PurchasingUnavailable:
  61. // Ask the user if billing is disabled in device settings.
  62. Debug.Log("Billing disabled!");
  63. break;
  64. case InitializationFailureReason.NoProductsAvailable:
  65. // Developer configuration error; check product metadata.
  66. Debug.Log("No products available for purchase!");
  67. break;
  68. }
  69. Toast.MakeText(Language.GetStr("Shop", "payFail")+" !");
  70. }
  71. public void OnPurchaseClicked(string productId)
  72. {
  73. if (controller != null) {
  74. // ProgressPanel.Show(Language.GetStr("Shop", "payBegin"));
  75. controller.InitiatePurchase (productId);
  76. } else {
  77. Toast.MakeText (Language.GetStr ("Shop", "payFail") + " !!");
  78. }
  79. }
  80. /// <summary>
  81. /// Called when a purchase completes.
  82. ///
  83. /// May be called at any time after OnInitialized().
  84. /// </summary>
  85. public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e)
  86. {
  87. Debug.Log("Purchase OK: " + e.purchasedProduct.definition.id);
  88. Debug.Log("Receipt: " + e.purchasedProduct.receipt);
  89. // #if RECEIPT_VALIDATION
  90. // Local validation is available for GooglePlay and Apple stores
  91. if (Application.platform == RuntimePlatform.Android ||
  92. Application.platform == RuntimePlatform.IPhonePlayer ||
  93. Application.platform == RuntimePlatform.OSXPlayer ||
  94. Application.platform == RuntimePlatform.tvOS) {
  95. try {
  96. var result = validator.Validate(e.purchasedProduct.receipt);
  97. Debug.Log("Receipt is valid. Contents:");
  98. foreach (IPurchaseReceipt productReceipt in result) {
  99. Debug.Log(productReceipt.productID);
  100. Debug.Log(productReceipt.purchaseDate);
  101. Debug.Log(productReceipt.transactionID);
  102. GooglePlayReceipt google = productReceipt as GooglePlayReceipt;
  103. if (null != google) {
  104. Debug.Log(google.purchaseState);
  105. Debug.Log(google.purchaseToken);
  106. }
  107. AppleInAppPurchaseReceipt apple = productReceipt as AppleInAppPurchaseReceipt;
  108. if (null != apple) {
  109. Debug.Log(apple.originalTransactionIdentifier);
  110. Debug.Log(apple.subscriptionExpirationDate);
  111. Debug.Log(apple.cancellationDate);
  112. Debug.Log(apple.quantity);
  113. IOSPayment(e.purchasedProduct.definition.id, e.purchasedProduct.receipt);
  114. }
  115. }
  116. } catch (IAPSecurityException) {
  117. Debug.Log("Invalid receipt, not unlocking content");
  118. return PurchaseProcessingResult.Complete;
  119. }
  120. }
  121. // #endif
  122. return PurchaseProcessingResult.Complete;
  123. }
  124. /// <summary>
  125. /// Called when a purchase fails.
  126. /// </summary>
  127. public void OnPurchaseFailed (Product item, PurchaseFailureReason r)
  128. {
  129. Debug.Log("Purchase failed: " + item.definition.id);
  130. Debug.Log(r);
  131. Toast.MakeText(Language.GetStr("Shop", "payFail")+" !!!");
  132. ProgressPanel.Hide();
  133. }
  134. public void IOSPayment(string product, string receipt)
  135. {
  136. UserData userData = Session.GetInstance ().myUserData;
  137. URLRequestData data = new URLRequestData(true);
  138. data.Add("userid", userData.id);
  139. data.Add("item", product);
  140. data.Add("receipt", receipt);
  141. URLRequest.CreateURLRequest(NetworkManager.GetURL(NetworkManager.URL.IOSPayment), data, (JsonData json)=>{
  142. int resCode = JsonUtil.ToInt(json["c"]);
  143. if(resCode == 0)
  144. {
  145. JsonData info = json["d"];
  146. if(JsonUtil.ContainKey(info, "d"))
  147. {
  148. userData.diamond = JsonUtil.ToInt(info["d"]);
  149. }
  150. if(JsonUtil.ContainKey(info, "c"))
  151. {
  152. userData.coin = JsonUtil.ToInt(info["c"]);
  153. }
  154. if(JsonUtil.ContainKey(info, "v"))
  155. {
  156. userData.vipExpireTime = info["v"].ToString();
  157. }
  158. BuyUtil.Bought(product);
  159. }
  160. ProgressPanel.Hide();
  161. }, URLRequest.Method.POST);
  162. }
  163. public void IOSPayment(string result)
  164. {
  165. string[] strArr = StringUtil.Split(result, '|');
  166. string itemCode = strArr[0];
  167. string receipt = strArr.Length > 1 ? strArr[1] : "none";
  168. UserData userData = Session.GetInstance ().myUserData;
  169. URLRequestData data = new URLRequestData(true);
  170. data.Add("userid", userData.id);
  171. data.Add("item", itemCode);
  172. data.Add("receipt", receipt);
  173. URLRequest.CreateURLRequest(NetworkManager.GetURL(NetworkManager.URL.IOSPayment), data, (JsonData json)=>{
  174. int resCode = JsonUtil.ToInt(json["c"]);
  175. if(resCode == 0)
  176. {
  177. JsonData info = json["d"];
  178. if(JsonUtil.ContainKey(info, "d"))
  179. {
  180. userData.diamond = JsonUtil.ToInt(info["d"]);
  181. }
  182. if(JsonUtil.ContainKey(info, "c"))
  183. {
  184. userData.coin = JsonUtil.ToInt(info["c"]);
  185. }
  186. if(JsonUtil.ContainKey(info, "v"))
  187. {
  188. userData.vipExpireTime = info["v"].ToString();
  189. }
  190. BuyUtil.Bought(itemCode);
  191. }
  192. ProgressPanel.Hide();
  193. }, URLRequest.Method.POST);
  194. }
  195. private static PaymentManager instance;
  196. public static PaymentManager GetIntance()
  197. {
  198. if(instance == null)
  199. {
  200. instance = new PaymentManager ();
  201. }
  202. return instance;
  203. }
  204. }