KMPurchaseManager.swift 4.3 KB

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