KMPurchaseManager.swift 4.6 KB

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