KMPurchaseManager.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // KMPurchaseManager.swift
  3. // PDF Reader Pro
  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 .subscription
  28. #endif
  29. if (IAPProductsManager.default().isAvailableAllFunction()) {
  30. return .subscription
  31. }
  32. if KMInAppPurchaseManager.manager.state == .subscription {
  33. return .subscription
  34. } else {
  35. return KMDMGPurchaseManager.manager.state
  36. }
  37. }
  38. }
  39. var availableProducts: [KMProduct] = []
  40. override init() {
  41. super.init()
  42. }
  43. func fetchProducts(completeion: @escaping KMPurchaseFetchProductCompletion) {
  44. KMPrint("获取产品中")
  45. #if VERSION_FREE
  46. KMPrint("正在获取产品中AppStore")
  47. KMInAppPurchaseManager.manager.fetchProducts(completion: completeion)
  48. #endif
  49. #if VERSION_DMG
  50. KMPrint("正在获取产品中DMG")
  51. KMDMGPurchaseManager.manager.fetchProducts(completion: completeion)
  52. #endif
  53. }
  54. func purchaseProduct(productIdentifier: String, completion: @escaping KMPurchaseCompletion) {
  55. KMPrint("准备订阅中")
  56. #if VERSION_FREE
  57. KMPrint("正在订阅中AppStore")
  58. let userId: String = KMLightMemberManager.manager.info.id
  59. // KMRequestServerManager.manager.createOrder(productId: "21", userId: userId) { success, orderId, result in
  60. // if success {
  61. // if orderId?.count != 0 {
  62. KMInAppPurchaseManager.manager.purchaseProduct(productIdentifier: PRODUCT_1) { isSuccess, error in
  63. if isSuccess {
  64. completion(true, error)
  65. } else {
  66. completion(false, error)
  67. }
  68. }
  69. // }
  70. // } else {
  71. // completion(false, .orderFailed)
  72. // }
  73. // }
  74. #endif
  75. #if VERSION_DMG
  76. KMPrint("正在订阅中DMG")
  77. var email: String = UserDefaults.standard.value(forKey: "kLoginEmail") as? String ?? ""
  78. KMDMGPurchaseManager.manager.purchaseProduct(productIdentifier: productIdentifier, email: email, completion: completion)
  79. #endif
  80. }
  81. func showStore() {
  82. #if VERSION_DMG
  83. let email: String = UserDefaults.standard.value(forKey: "kLoginEmail") as? String ?? ""
  84. NSWorkspace.shared.open(URL(string: "\(KMLightMemberManager.manager.config.kStoreServerURL)/store/master?email=\(email)")!)
  85. #endif
  86. }
  87. func showPurchasesInfo() {
  88. #if VERSION_DMG
  89. if KMLightMemberManager.manager.isLogin() {
  90. let token: String = KMLightMemberManager.manager.token.access_token
  91. NSWorkspace.shared.open(URL(string: "\(KMLightMemberManager.manager.config.kStoreServerURL)/account/master-subscription?appid=16&token=\(token)")!)
  92. }
  93. #endif
  94. }
  95. func restorePurchases(_ completion: @escaping KMPurchaseRestoreCompletion) {
  96. KMPrint("准备restore")
  97. #if VERSION_FREE
  98. KMPrint("正在restore")
  99. KMInAppPurchaseManager.manager.restorePurchases("", completion)
  100. #endif
  101. #if VERSION_DMG
  102. KMDMGPurchaseManager.manager.restorePurchases()
  103. KMPrint("正在restore DMG")
  104. #endif
  105. }
  106. func checkSubscriptionStatus(_ completion: @escaping KMPurchaseCheckSubscriptionStatusCompletion) {
  107. #if VERSION_FREE
  108. KMInAppPurchaseManager.manager.checkSubscriptionStatus(completion)
  109. #endif
  110. #if VERSION_DMG
  111. #endif
  112. }
  113. }