KMPurchaseManager.swift 4.0 KB

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