KMPurchaseManager.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. KMInAppPurchaseManager.manager.purchaseProduct(productIdentifier: productIdentifier, completion: completion)
  84. #endif
  85. #if VERSION_DMG
  86. print("正在订阅中DMG")
  87. KMDMGPurchaseManager.manager.purchaseProduct(productIdentifier: productIdentifier, completion: completion)
  88. #endif
  89. }
  90. func restorePurchases() {
  91. print("准备restore")
  92. #if VERSION_FREE
  93. print("正在restore")
  94. KMInAppPurchaseManager.manager.restorePurchases()
  95. #endif
  96. #if VERSION_DMG
  97. KMDMGPurchaseManager.manager.restorePurchases()
  98. print("正在restore DMG")
  99. #endif
  100. }
  101. }