KMDMGPurchaseManager.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // KMDMGPurchaseManager.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/6/8.
  6. //
  7. import Cocoa
  8. class KMDMGPurchaseManager: NSObject {
  9. public static let manager = KMDMGPurchaseManager()
  10. var fetchProductCompletion: KMPurchaseFetchProductCompletion?
  11. var availableProducts: [KMProduct] = []
  12. var state: KMPurchaseManagerState {
  13. get {
  14. return self.updatePurchaseState()
  15. }
  16. }
  17. func updatePurchaseState() -> KMPurchaseManagerState {
  18. let info = KMLightMemberManager.manager.info.subscriptionInfoList
  19. var tempState: KMPurchaseManagerState = .unknow
  20. if info.count > 0 {
  21. for item in info {
  22. switch item.status {
  23. case 0:
  24. tempState = .unknow
  25. case 1:
  26. tempState = .subscription
  27. case 2:
  28. tempState = .subscriptionExpired
  29. case 3:
  30. tempState = .trial
  31. case 4:
  32. tempState = .trialExpired
  33. default:
  34. tempState = .unknow
  35. }
  36. }
  37. }
  38. return tempState
  39. }
  40. func purchaseProduct(productIdentifier: String, email: String, completion: KMPurchaseCompletion) {
  41. // NSWorkspace.shared.open(URL(string: "http://test-pdf-pro.kdan.cn:3021/master/checkout?email=\(email)")!)
  42. #if DEBUG
  43. NSWorkspace.shared.open(URL(string: "\(KMLightMemberManager.manager.config.kStoreServerURL)/store/master-subscription?email=\(email)")!)
  44. #else
  45. NSWorkspace.shared.open(URL(string: "\(KMLightMemberManager.manager.config.kStoreServerURL)/store/master-subscription?email=\(email)")!)
  46. #endif
  47. completion(false, .success)
  48. }
  49. func fetchProducts(completion: @escaping KMPurchaseFetchProductCompletion) {
  50. self.fetchProductCompletion = completion
  51. }
  52. func restorePurchases() {
  53. KMRequestServerManager.manager.getUserInfo { [unowned self] success, data, error, isLocal in
  54. if success {
  55. KMPrint("DMG刷新用户信息成功")
  56. if KMLightMemberManager.manager.purchaseState == .subscription {
  57. let controller = KMSubscribeWaterMarkWindowController.isSampleController()
  58. controller.closeWindow()
  59. }
  60. } else {
  61. KMPrint("DMG刷新用户信息失败")
  62. }
  63. }
  64. }
  65. }