KMPurchaseManager.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // KMPurchaseManager.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/6/1.
  6. //
  7. import Cocoa
  8. enum KMPurchaseManagerState: String, CaseIterable {
  9. case unknow = "unknow"
  10. case trial = "Trial" //试用
  11. case trialExpired = "Trial Expired" //试用过期
  12. case subscription = "Subscription" //订阅
  13. case subscriptionExpired = "Subscription Expired" //订阅过期
  14. }
  15. #if VERSION_DMG
  16. #endif
  17. typealias KMPurchaseCompletion = (_ isSuccess: Bool, _ error: KMInAppPurchaseState) -> Void
  18. typealias KMPurchaseFetchProductCompletion = (_ isSuccess: Bool, _ products: Array<Any>, _ error: String?) -> Void
  19. typealias KMPurchaseRestoreCompletion = (_ isSuccess: Bool) -> Void
  20. class KMPurchaseManager: NSObject {
  21. public static let manager = KMPurchaseManager()
  22. var state: KMPurchaseManagerState {
  23. get {
  24. let info = KMLightMemberManager.manager.info.subscriptionInfoList
  25. var tempState: KMPurchaseManagerState = .unknow
  26. return .trialExpired
  27. if info.count > 0 {
  28. let isSubscription = false
  29. for item in info {
  30. switch item.status {
  31. case 0:
  32. tempState = .unknow
  33. case 1:
  34. tempState = .subscription
  35. case 2:
  36. tempState = .subscriptionExpired
  37. case 3:
  38. tempState = .trial
  39. case 4:
  40. tempState = .trialExpired
  41. default:
  42. tempState = .unknow
  43. }
  44. }
  45. }
  46. return tempState
  47. }
  48. //#if VERSION_FREE
  49. // return KMInAppPurchaseManager.manager.state
  50. //#endif
  51. //
  52. //#if VERSION_DMG
  53. // print("获取产品状态")
  54. // return .unknow
  55. //#endif
  56. // }
  57. }
  58. var availableProducts: [KMProduct] = []
  59. override init() {
  60. super.init()
  61. // self.fetchProducts { isSuccess, products, error in
  62. // if isSuccess {
  63. // print("获取产品成功")
  64. // } else {
  65. // print("获取产品失败")
  66. // }
  67. // print(products)
  68. // }
  69. }
  70. func fetchProducts(completeion: @escaping KMPurchaseFetchProductCompletion) {
  71. print("获取产品中")
  72. #if VERSION_FREE
  73. print("正在获取产品中AppStore")
  74. KMInAppPurchaseManager.manager.fetchProducts(completion: completeion)
  75. #endif
  76. #if VERSION_DMG
  77. print("正在获取产品中DMG")
  78. KMDMGPurchaseManager.manager.fetchProducts(completion: completeion)
  79. #endif
  80. }
  81. func purchaseProduct(productIdentifier: String, completion: @escaping KMPurchaseCompletion) {
  82. print("准备订阅中")
  83. #if VERSION_FREE
  84. print("正在订阅中AppStore")
  85. let userId: String = KMLightMemberManager.manager.info.id
  86. KMRequestServerManager.manager.createOrder(productId: "21", userId: userId) { success, orderId, result in
  87. if success {
  88. if orderId?.count != 0 {
  89. KMInAppPurchaseManager.manager.purchaseProduct(productIdentifier: PRODUCT_1, orderId: orderId!) { isSuccess, error in
  90. if isSuccess {
  91. print("购买成功")
  92. completion(true, error)
  93. } else {
  94. print("购买失败")
  95. completion(false, error)
  96. }
  97. }
  98. }
  99. }
  100. }
  101. #endif
  102. #if VERSION_DMG
  103. print("正在订阅中DMG")
  104. KMDMGPurchaseManager.manager.purchaseProduct(productIdentifier: productIdentifier, email: "350846486@qq.com", completion: completion)
  105. #endif
  106. }
  107. func restorePurchases(_ completion: @escaping KMPurchaseRestoreCompletion) {
  108. print("准备restore")
  109. #if VERSION_FREE
  110. print("正在restore")
  111. let userId: String = KMLightMemberManager.manager.info.id
  112. KMRequestServerManager.manager.restore(productId: "21", userId: userId) { success, orderId, result in
  113. if success {
  114. if orderId?.count != 0 {
  115. KMInAppPurchaseManager.manager.restorePurchases(orderId!) { isSuccess in
  116. if isSuccess {
  117. print("购买成功")
  118. completion(true)
  119. } else {
  120. print("购买失败")
  121. completion(false)
  122. }
  123. }
  124. }
  125. }
  126. }
  127. #endif
  128. #if VERSION_DMG
  129. KMDMGPurchaseManager.manager.restorePurchases()
  130. print("正在restore DMG")
  131. #endif
  132. }
  133. }