KMPurchaseManager.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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: KMInAppPurchaseState?) -> Void
  19. typealias KMPurchaseRestoreCompletion = (_ isSuccess: Bool, _ error: KMInAppPurchaseState) -> Void
  20. typealias KMPurchaseCheckSubscriptionStatusCompletion = (_ isSubscription: Bool) -> Void
  21. class KMPurchaseManager: NSObject {
  22. public static let manager = KMPurchaseManager()
  23. var state: KMPurchaseManagerState {
  24. get {
  25. #if DEBUG
  26. //方便调整订阅状态
  27. // return .subscriptionExpired
  28. #endif
  29. let info = KMLightMemberManager.manager.info.subscriptionInfoList
  30. var tempState: KMPurchaseManagerState = .unknow
  31. if info.count > 0 {
  32. let isSubscription = false
  33. for item in info {
  34. switch item.status {
  35. case 0:
  36. tempState = .unknow
  37. case 1:
  38. tempState = .subscription
  39. case 2:
  40. tempState = .subscriptionExpired
  41. case 3:
  42. tempState = .trial
  43. case 4:
  44. tempState = .trialExpired
  45. default:
  46. tempState = .unknow
  47. }
  48. }
  49. }
  50. return tempState
  51. }
  52. }
  53. var availableProducts: [KMProduct] = []
  54. override init() {
  55. super.init()
  56. }
  57. func fetchProducts(completeion: @escaping KMPurchaseFetchProductCompletion) {
  58. KMPrint("获取产品中")
  59. #if VERSION_FREE
  60. KMPrint("正在获取产品中AppStore")
  61. KMInAppPurchaseManager.manager.fetchProducts(completion: completeion)
  62. #endif
  63. #if VERSION_DMG
  64. KMPrint("正在获取产品中DMG")
  65. KMDMGPurchaseManager.manager.fetchProducts(completion: completeion)
  66. #endif
  67. }
  68. func purchaseProduct(productIdentifier: String, completion: @escaping KMPurchaseCompletion) {
  69. KMPrint("准备订阅中")
  70. #if VERSION_FREE
  71. KMPrint("正在订阅中AppStore")
  72. let userId: String = KMLightMemberManager.manager.info.id
  73. KMRequestServerManager.manager.createOrder(productId: "21", userId: userId) { success, orderId, result in
  74. if success {
  75. if orderId?.count != 0 {
  76. KMInAppPurchaseManager.manager.purchaseProduct(productIdentifier: PRODUCT_1, orderId: orderId!) { isSuccess, error in
  77. if isSuccess {
  78. completion(true, error)
  79. } else {
  80. completion(false, error)
  81. }
  82. }
  83. }
  84. } else {
  85. completion(false, .orderFailed)
  86. }
  87. }
  88. #endif
  89. #if VERSION_DMG
  90. KMPrint("正在订阅中DMG")
  91. var email: String = UserDefaults.standard.value(forKey: "kLoginEmail") as? String ?? ""
  92. KMDMGPurchaseManager.manager.purchaseProduct(productIdentifier: productIdentifier, email: email, completion: completion)
  93. #endif
  94. }
  95. func showStore() {
  96. #if VERSION_DMG
  97. NSWorkspace.shared.open(URL(string: "\(KMLightMemberManager.manager.config.kStoreServerURL)/store/master")!)
  98. #endif
  99. }
  100. func showPurchasesInfo() {
  101. #if VERSION_DMG
  102. if KMLightMemberManager.manager.isLogin() {
  103. let token: String = KMLightMemberManager.manager.token.access_token
  104. NSWorkspace.shared.open(URL(string: "\(KMLightMemberManager.manager.config.kStoreServerURL)/account/master-subscription?appid=16&token=\(token)")!)
  105. }
  106. #endif
  107. }
  108. func restorePurchases(_ completion: @escaping KMPurchaseRestoreCompletion) {
  109. KMPrint("准备restore")
  110. #if VERSION_FREE
  111. KMPrint("正在restore")
  112. KMInAppPurchaseManager.manager.restorePurchases("", completion)
  113. #endif
  114. #if VERSION_DMG
  115. KMDMGPurchaseManager.manager.restorePurchases()
  116. KMPrint("正在restore DMG")
  117. #endif
  118. }
  119. func checkSubscriptionStatus(_ completion: @escaping KMPurchaseCheckSubscriptionStatusCompletion) {
  120. #if VERSION_FREE
  121. KMInAppPurchaseManager.manager.checkSubscriptionStatus(completion)
  122. #endif
  123. #if VERSION_DMG
  124. #endif
  125. }
  126. }