PayUnityIAPImplement.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using FrameWork.SDKManager;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. #if UnityIAP
  8. using UnityEngine.Purchasing;
  9. #endif
  10. public class PayUnityIAPImplement : PayInterface
  11. {
  12. public StoreName storeName;
  13. public override StoreName GetStoreName()
  14. {
  15. return storeName;
  16. }
  17. #if UnityIAP
  18. private IAPStoreListener listener;
  19. protected override void ExtraInit()
  20. {
  21. listener = new IAPStoreListener();
  22. List<ProductDefinition> products = new List<ProductDefinition>();
  23. foreach (var item in productDefinitions)
  24. {
  25. ProductType productType = GoodsType2ProductType(item.goodsType);
  26. ProductDefinition p = new ProductDefinition(item.goodsID, productType);
  27. products.Add(p);
  28. }
  29. AppStore appStore = (AppStore)Enum.Parse(typeof(AppStore), storeName.ToString());
  30. listener.Initialize(appStore, products);
  31. listener.onInitialized = OnInitialized;
  32. listener.onInitializeFailed = OnInitializeFailed;
  33. listener.onPurchaseFailed = OnPurchaseFailed;
  34. listener.onPurchaseSuccess = OnPurchaseSuccess;
  35. }
  36. private ProductType GoodsType2ProductType(FrameWork.SDKManager.GoodsType m_ProductType)
  37. {
  38. ProductType productType = ProductType.Consumable;
  39. switch (m_ProductType)
  40. {
  41. case FrameWork.SDKManager.GoodsType.NORMAL:
  42. productType = ProductType.Consumable;
  43. break;
  44. case FrameWork.SDKManager.GoodsType.ONCE_ONLY:
  45. productType = ProductType.NonConsumable;
  46. break;
  47. case FrameWork.SDKManager.GoodsType.RIGHTS:
  48. productType = ProductType.Subscription;
  49. break;
  50. }
  51. return productType;
  52. }
  53. private FrameWork.SDKManager.GoodsType ProductType2GoodsType(ProductType m_ProductType)
  54. {
  55. FrameWork.SDKManager.GoodsType productType = FrameWork.SDKManager.GoodsType.NORMAL;
  56. switch (m_ProductType)
  57. {
  58. case ProductType.Consumable:
  59. productType = FrameWork.SDKManager.GoodsType.NORMAL;
  60. break;
  61. case ProductType.NonConsumable:
  62. productType = FrameWork.SDKManager.GoodsType.ONCE_ONLY;
  63. break;
  64. case ProductType.Subscription:
  65. productType = FrameWork.SDKManager.GoodsType.RIGHTS;
  66. break;
  67. }
  68. return productType;
  69. }
  70. private void OnPurchaseSuccess(Product t,string receipt)
  71. {
  72. OnPayInfo payInfo = new OnPayInfo();
  73. payInfo.isSuccess = true;
  74. payInfo.goodsId = t.definition.id;
  75. payInfo.goodsType = ProductType2GoodsType(t.definition.type);
  76. payInfo.receipt = receipt;
  77. payInfo.storeName = storeName;
  78. PayCallBack(payInfo);
  79. }
  80. private void OnPurchaseFailed(Product t, PurchaseFailureReason t1)
  81. {
  82. Debug.Log("OnPurchaseFailed ! t:"+t+ " PurchaseFailureReason:"+t1);
  83. OnPayInfo payInfo = new OnPayInfo();
  84. payInfo.isSuccess = false;
  85. if (t != null)
  86. {
  87. Debug.Log("OnPurchaseFailed ! t.definition:" + t.definition);
  88. payInfo.goodsId = t.definition.id;
  89. payInfo.goodsType = ProductType2GoodsType(t.definition.type);
  90. }
  91. PayCallBack(payInfo);
  92. }
  93. private void OnPendingPurchase(string t)
  94. {
  95. //if (m_ConfirmCallBack != null)
  96. //{
  97. // m_ConfirmCallBack(storeName, t);
  98. //}
  99. }
  100. private void OnInitializeFailed(InitializationFailureReason t)
  101. {
  102. }
  103. private void OnInitialized()
  104. {
  105. }
  106. public override void Pay(string goodsID, string tag, FrameWork.SDKManager.GoodsType goodsType = FrameWork.SDKManager.GoodsType.NORMAL, string orderID = null)
  107. {
  108. if (Application.isEditor)
  109. {
  110. ConfirmPay(goodsID,tag);
  111. }
  112. else
  113. listener.PurchaseProduct(goodsID);
  114. }
  115. public override void ConfirmPay(string goodsID, string tag)
  116. {
  117. listener.ConfirmPendingPurchase(goodsID);
  118. }
  119. public override List<LocalizedGoodsInfo> GetAllGoodsInfo()
  120. {
  121. List<LocalizedGoodsInfo> infos = new List<LocalizedGoodsInfo>();
  122. foreach (var item in listener.m_StoreController.products.all)
  123. {
  124. if (item.definition == null)
  125. continue;
  126. if (item.metadata == null)
  127. continue;
  128. LocalizedGoodsInfo f = new LocalizedGoodsInfo();
  129. f.goodsID = item.definition.id;
  130. f.isoCurrencyCode = item.metadata.isoCurrencyCode;
  131. f.localizedDescription = item.metadata.localizedDescription;
  132. f.localizedPrice = (float)item.metadata.localizedPrice;
  133. f.localizedPriceString = item.metadata.localizedPriceString;
  134. f.localizedTitle = item.metadata.localizedTitle;
  135. infos.Add(f);
  136. }
  137. return infos;
  138. }
  139. public override LocalizedGoodsInfo GetGoodsInfo(string goodsID)
  140. {
  141. foreach (var item in listener.m_StoreController.products.all)
  142. {
  143. if (item.definition == null)
  144. continue;
  145. if (item.metadata == null)
  146. continue;
  147. if (item.definition.id != goodsID)
  148. continue;
  149. LocalizedGoodsInfo f = new LocalizedGoodsInfo();
  150. f.goodsID = goodsID;
  151. f.isoCurrencyCode = item.metadata.isoCurrencyCode;
  152. f.localizedDescription = item.metadata.localizedDescription;
  153. f.localizedPrice = (float)item.metadata.localizedPrice;
  154. f.localizedPriceString = item.metadata.localizedPriceString;
  155. f.localizedTitle = item.metadata.localizedTitle;
  156. return f;
  157. }
  158. return null;
  159. }
  160. #endif
  161. }