|
@@ -20,6 +20,8 @@ public enum PremiumPeriod: String, CaseIterable {
|
|
|
switch self {
|
|
|
case .week:
|
|
|
return 30
|
|
|
+ case .month:
|
|
|
+ return 30
|
|
|
case .year:
|
|
|
return 60
|
|
|
case .none:
|
|
@@ -28,6 +30,38 @@ public enum PremiumPeriod: String, CaseIterable {
|
|
|
return 30
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /// 对应vip类型,可以免费使用次数
|
|
|
+ var saveString: String {
|
|
|
+ switch self {
|
|
|
+ case .none:
|
|
|
+ return "30%"
|
|
|
+ default:
|
|
|
+ return "80%"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ 1. 一年(非闰年)
|
|
|
+ 365 天 = 365 × 24 × 60 × 60 × 1000 = 31,536,000,000 毫秒
|
|
|
+ (若闰年 366 天 = 31,622,400,000 毫秒)
|
|
|
+ 2. 一个月(平均)
|
|
|
+ 30.44 天(按 365 天/12 个月计算)≈ 30.44 × 24 × 60 × 60 × 1000 ≈ 2,629,746,000 毫秒
|
|
|
+ (实际月份天数不同,如 28/30/31 天需单独计算)
|
|
|
+ 3. 一周
|
|
|
+ 7 天 = 7 × 24 × 60 × 60 × 1000 = 604,800,000 毫秒
|
|
|
+ */
|
|
|
+ var milliseconds:Int {
|
|
|
+ switch self {
|
|
|
+ case .year:
|
|
|
+ return 365 * 24 * 60 * 60 * 1000
|
|
|
+ case .month:
|
|
|
+ return 30 * 24 * 60 * 60 * 1000
|
|
|
+ case .week:
|
|
|
+ return 7 * 24 * 60 * 60 * 1000
|
|
|
+ default:
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public enum VipFreeNumType: String, CaseIterable {
|
|
@@ -89,10 +123,9 @@ public class PurchaseManager: NSObject {
|
|
|
// 商品信息
|
|
|
public lazy var purchaseProducts: [PurchaseProduct] = {
|
|
|
[
|
|
|
-// PurchaseProduct(productId: "101", period:.month),
|
|
|
+ PurchaseProduct(productId: "101", period: .month),
|
|
|
PurchaseProduct(productId: "102", period: .year),
|
|
|
- PurchaseProduct(productId: "103", period: .week),
|
|
|
- // PurchaseProduct(productId: "003", period: .lifetime),
|
|
|
+ PurchaseProduct(productId: "103", period: .week)
|
|
|
]
|
|
|
}()
|
|
|
|
|
@@ -167,9 +200,9 @@ public class PurchaseManager: NSObject {
|
|
|
}
|
|
|
|
|
|
@objc public var isVip: Bool {
|
|
|
-// #if DEBUG
|
|
|
-// return true
|
|
|
-// #endif
|
|
|
+//#if DEBUG
|
|
|
+// return vipType != .none
|
|
|
+//#endif
|
|
|
guard let expiresDate = expiredDate else {
|
|
|
return false
|
|
|
}
|
|
@@ -181,9 +214,13 @@ public class PurchaseManager: NSObject {
|
|
|
}
|
|
|
|
|
|
public var vipType: PremiumPeriod {
|
|
|
+//#if DEBUG
|
|
|
+// return PremiumPeriod.week
|
|
|
+//#endif
|
|
|
guard isVip, let type = vipInformation["type"] as? String else {
|
|
|
return .none
|
|
|
}
|
|
|
+ debugPrint("PurchaseManager get vipType = \(type)")
|
|
|
return PremiumPeriod(rawValue: type) ?? .none
|
|
|
}
|
|
|
|
|
@@ -194,10 +231,9 @@ public class PurchaseManager: NSObject {
|
|
|
vipInformation["expireTime"] = timeInterval
|
|
|
vipInformation["productId"] = productId
|
|
|
vipInformation["type"] = period(for: productId).rawValue
|
|
|
-
|
|
|
+ dePrint("vipInformation = \(vipInformation)")
|
|
|
UserDefaults.standard.set(vipInformation, forKey: kPremiumExpiredInfoKey)
|
|
|
UserDefaults.standard.synchronize()
|
|
|
-
|
|
|
NotificationCenter.default.post(name: .kPurchaseDidChanged, object: nil)
|
|
|
}
|
|
|
|
|
@@ -315,7 +351,7 @@ extension PurchaseManager {
|
|
|
public func restorePremium() {
|
|
|
purchase(self, didChaged: .restoreing, object: nil)
|
|
|
SKPaymentQueue.default().restoreCompletedTransactions()
|
|
|
- debugPrint("PurchaseManager restoreCompletedTransactions")
|
|
|
+ debugPrint("PurchaseManager restoreCompletedTransactions restorePremium")
|
|
|
|
|
|
subscriptionApple(type: .created, jsonString: "Payment restore")
|
|
|
}
|
|
@@ -329,12 +365,14 @@ extension PurchaseManager {
|
|
|
|
|
|
guard SKPaymentQueue.default().transactions.count <= 0 else {
|
|
|
purchase(self, didChaged: .payFail, object: "You have outstanding orders that must be paid for before a new subscription can be placed.")
|
|
|
+ debugPrint("PurchaseManager pay period restorePremium = \(period)")
|
|
|
restorePremium()
|
|
|
return
|
|
|
}
|
|
|
if let product = product(for: period) {
|
|
|
purchase(self, didChaged: .paying, object: nil)
|
|
|
let payment = SKPayment(product: product)
|
|
|
+ debugPrint("PurchaseManager pay product = \(product.localizedDescription)")
|
|
|
SKPaymentQueue.default().add(payment)
|
|
|
debugPrint("PurchaseManager pay period = \(period)")
|
|
|
|
|
@@ -536,11 +574,13 @@ extension PurchaseManager {
|
|
|
}
|
|
|
|
|
|
if !isLifetime {
|
|
|
- let info = resp["latest_receipt_info"] as? [[String: Any]]
|
|
|
- if let firstItem = info?.first,
|
|
|
- let expires_date_ms = firstItem["expires_date_ms"] as? String,
|
|
|
- let productId = firstItem["product_id"] as? String {
|
|
|
- updateExpireTime(expires_date_ms, for: productId)
|
|
|
+ if upgradePendingRenewalInfo(resp) == false {
|
|
|
+ let info = resp["latest_receipt_info"] as? [[String: Any]]
|
|
|
+ if let firstItem = info?.first,
|
|
|
+ let expires_date_ms = firstItem["expires_date_ms"] as? String,
|
|
|
+ let productId = firstItem["product_id"] as? String {
|
|
|
+ updateExpireTime(expires_date_ms, for: productId)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -554,6 +594,51 @@ extension PurchaseManager {
|
|
|
|
|
|
subscriptionApple(type: .result, jsonString: simplifyVerifyPayResult(resp: resp))
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ func upgradePendingRenewalInfo(_ resp: [String: Any]) -> Bool {
|
|
|
+ guard !resp.isEmpty else {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ pending_renewal_info={
|
|
|
+ "auto_renew_product_id" = 102;
|
|
|
+ "auto_renew_status" = 1;
|
|
|
+ "original_transaction_id" = 2000000929272571;
|
|
|
+ "product_id" = 101;
|
|
|
+ }*/
|
|
|
+ let info = resp["pending_renewal_info"] as? [[String: Any]]
|
|
|
+ if let firstItem = info?.first,
|
|
|
+ let auto_renew_product_id = firstItem["auto_renew_product_id"] as? String,
|
|
|
+ let auto_product_id = firstItem["product_id"] as? String {
|
|
|
+ if auto_renew_product_id != auto_product_id {//拿到待生效的和当前的对比不一样,以待生效的为主
|
|
|
+
|
|
|
+ //取当前的过期时间+加上待生效的会员过期时长
|
|
|
+ let info = resp["latest_receipt_info"] as? [[String: Any]]
|
|
|
+ if let firstItem = info?.first,
|
|
|
+ let expires_date_ms = firstItem["expires_date_ms"] as? String {
|
|
|
+ let expiresms = Int(expires_date_ms) ?? 0 + period(for: auto_renew_product_id).milliseconds
|
|
|
+ updateExpireTime(String(expiresms), for: auto_renew_product_id)
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ }
|
|
|
+// func findLatestSubscription(_ receipts: [[String: Any]]?) -> [String: Any]? {
|
|
|
+// guard let receiptInfo = receipts , !receiptInfo.isEmpty else {
|
|
|
+// return nil
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 使用 max(by:) 找出 expires_date_ms 最大的对象
|
|
|
+// let latestSubscription = receiptInfo.max { (first, second) -> Bool in
|
|
|
+// let firstExpiry = Int(first["expires_date_ms"] as? String ?? "0") ?? 0
|
|
|
+// let secondExpiry = Int(second["expires_date_ms"] as? String ?? "0") ?? 0
|
|
|
+// return firstExpiry < secondExpiry
|
|
|
+// }
|
|
|
+//
|
|
|
+// return latestSubscription
|
|
|
+// }
|
|
|
|
|
|
// 终生会员过期时间:100年
|
|
|
var lifetimeExpireTime: String {
|