KMPurchaseManager.swift 4.8 KB

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