123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- //
- // KMPurchaseManager.swift
- // PDF Master
- //
- // Created by lizhe on 2023/6/1.
- //
- import Cocoa
-
- @objc enum KMPurchaseManagerState: Int, CaseIterable {
- case unknow = 0//"unknow"
- case trial = 1//"Trial" //试用
- case trialExpired = 2//"Trial Expired" //试用过期
- case subscription = 3//"Subscription" //订阅
- case subscriptionExpired = 4//"Subscription Expired" //订阅过期
- }
- #if VERSION_DMG
- #endif
- typealias KMPurchaseCompletion = (_ isSuccess: Bool, _ error: KMInAppPurchaseState) -> Void
- typealias KMPurchaseFetchProductCompletion = (_ isSuccess: Bool, _ products: Array<Any>, _ error: String?) -> Void
- typealias KMPurchaseRestoreCompletion = (_ isSuccess: Bool) -> Void
- class KMPurchaseManager: NSObject {
- public static let manager = KMPurchaseManager()
-
- var state: KMPurchaseManagerState {
- get {
- let info = KMLightMemberManager.manager.info.subscriptionInfoList
- var tempState: KMPurchaseManagerState = .unknow
- if info.count > 0 {
- let isSubscription = false
- for item in info {
- switch item.status {
- case 0:
- tempState = .unknow
- case 1:
- tempState = .subscription
- case 2:
- tempState = .subscriptionExpired
- case 3:
- tempState = .trial
- case 4:
- tempState = .trialExpired
- default:
- tempState = .unknow
- }
- }
- }
- return tempState
- }
- //#if VERSION_FREE
- // return KMInAppPurchaseManager.manager.state
- //#endif
- //
- //#if VERSION_DMG
- // print("获取产品状态")
- // return .unknow
- //#endif
- // }
- }
- var availableProducts: [KMProduct] = []
-
- override init() {
- super.init()
-
- // self.fetchProducts { isSuccess, products, error in
- // if isSuccess {
- // print("获取产品成功")
- // } else {
- // print("获取产品失败")
- // }
- // print(products)
- // }
- }
-
- func fetchProducts(completeion: @escaping KMPurchaseFetchProductCompletion) {
- print("获取产品中")
- #if VERSION_FREE
- print("正在获取产品中AppStore")
- KMInAppPurchaseManager.manager.fetchProducts(completion: completeion)
- #endif
-
- #if VERSION_DMG
- print("正在获取产品中DMG")
- KMDMGPurchaseManager.manager.fetchProducts(completion: completeion)
- #endif
- }
-
- func purchaseProduct(productIdentifier: String, completion: @escaping KMPurchaseCompletion) {
- print("准备订阅中")
- #if VERSION_FREE
- print("正在订阅中AppStore")
-
- let userId: String = KMLightMemberManager.manager.info.id
- KMRequestServerManager.manager.createOrder(productId: "21", userId: userId) { success, orderId, result in
- if success {
- if orderId?.count != 0 {
- KMInAppPurchaseManager.manager.purchaseProduct(productIdentifier: PRODUCT_1, orderId: orderId!) { isSuccess, error in
- if isSuccess {
- print("购买成功")
- completion(true, error)
- } else {
- print("购买失败")
- completion(false, error)
- }
- }
- }
- }
- }
- #endif
-
- #if VERSION_DMG
- print("正在订阅中DMG")
- KMDMGPurchaseManager.manager.purchaseProduct(productIdentifier: productIdentifier, email: "350846486@qq.com", completion: completion)
- #endif
- }
-
- func restorePurchases(_ completion: @escaping KMPurchaseRestoreCompletion) {
- print("准备restore")
- #if VERSION_FREE
- print("正在restore")
- let userId: String = KMLightMemberManager.manager.info.id
- KMRequestServerManager.manager.restore(productId: "21", userId: userId) { success, orderId, result in
- if success {
- if orderId?.count != 0 {
- KMInAppPurchaseManager.manager.restorePurchases(orderId!) { isSuccess in
- if isSuccess {
- print("购买成功")
- completion(true)
- } else {
- print("购买失败")
- completion(false)
- }
- }
- }
- }
- }
- #endif
-
- #if VERSION_DMG
- KMDMGPurchaseManager.manager.restorePurchases()
- print("正在restore DMG")
- #endif
- }
- }
|