|
@@ -32,6 +32,10 @@ public class PurchaseManager: NSObject {
|
|
|
PurchaseProduct(productId: "102", period: .year),
|
|
|
PurchaseProduct(productId: "103", period: .week(.week)),
|
|
|
PurchaseProduct(productId: "113", period: .week(.weekPromotional1)),
|
|
|
+
|
|
|
+ PurchaseProduct(productId: "201", period: .purchase(.videoNum1)),
|
|
|
+ PurchaseProduct(productId: "202", period: .purchase(.videoNum2)),
|
|
|
+ PurchaseProduct(productId: "203", period: .purchase(.videoNum3)),
|
|
|
]
|
|
|
}()
|
|
|
|
|
@@ -53,7 +57,15 @@ public class PurchaseManager: NSObject {
|
|
|
// 原始订单交易id dict
|
|
|
var originalTransactionIdentifierDict: [String: String] = [:]
|
|
|
|
|
|
-
|
|
|
+ private lazy var onceVerifyPayResult: Void = {
|
|
|
+ dePrint("这段代码只会执行一次")
|
|
|
+#if DEBUG
|
|
|
+ verifyPayResult(transaction: SKPaymentTransaction(), useSandBox: true,uiBlock:nil)
|
|
|
+#else
|
|
|
+ verifyPayResult(transaction: SKPaymentTransaction(), useSandBox: false,uiBlock:nil)
|
|
|
+#endif
|
|
|
+ }()
|
|
|
+
|
|
|
|
|
|
override init() {
|
|
|
super.init()
|
|
@@ -65,13 +77,7 @@ public class PurchaseManager: NSObject {
|
|
|
}
|
|
|
|
|
|
initializeForFree()
|
|
|
-
|
|
|
-//#if DEBUG
|
|
|
-// verifyPayResult(transaction: SKPaymentTransaction(), useSandBox: true)
|
|
|
-//#else
|
|
|
-// verifyPayResult(transaction: SKPaymentTransaction(), useSandBox: false)
|
|
|
-//#endif
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public var expiredDate: Date? {
|
|
@@ -331,27 +337,31 @@ extension PurchaseManager: SKProductsRequestDelegate {
|
|
|
self.products = products
|
|
|
purchase(self, didChaged: .loadSuccess, object: nil,transaction: nil)
|
|
|
NotificationCenter.default.post(name: .kPurchasePrepared, object: nil)
|
|
|
- debugPrint("PurchaseManager productsRequest didReceive = \(products)")
|
|
|
+ dePrint("PurchaseManager productsRequest didReceive = \(products)")
|
|
|
|
|
|
for product in products {
|
|
|
- print("请求到商品ID: \(product.productIdentifier)")
|
|
|
+ dePrint("请求到商品ID: \(product.productIdentifier)")
|
|
|
// 获取促销价格
|
|
|
if let introductoryPrice = product.introductoryPrice {
|
|
|
- print("新用户促销价格: \(introductoryPrice.price) \(introductoryPrice.priceLocale.currencySymbol ?? "")")
|
|
|
- print("新用户促销周期: \(introductoryPrice.subscriptionPeriod.numberOfUnits) \(introductoryPrice.subscriptionPeriod.unit)")
|
|
|
+ dePrint("新用户促销价格: \(introductoryPrice.price) \(introductoryPrice.priceLocale.currencySymbol ?? "")")
|
|
|
+ dePrint("新用户促销周期: \(introductoryPrice.subscriptionPeriod.numberOfUnits) \(introductoryPrice.subscriptionPeriod.unit)")
|
|
|
}
|
|
|
|
|
|
// 获取促销价格
|
|
|
for discounts in product.discounts {
|
|
|
- print("老用户促销价格: \(discounts.price) \(discounts.priceLocale.currencySymbol ?? "")")
|
|
|
- print("老用户促销周期: \(discounts.subscriptionPeriod.numberOfUnits)")
|
|
|
+ dePrint("老用户促销价格: \(discounts.price) \(discounts.priceLocale.currencySymbol ?? "")")
|
|
|
+ dePrint("老用户促销周期: \(discounts.subscriptionPeriod.numberOfUnits)")
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ //拿到商品后,主动拉取一次苹果订单,处理会员
|
|
|
+ _ = onceVerifyPayResult
|
|
|
+ dePrint("PurchaseManager onceVerifyPayResult")
|
|
|
}
|
|
|
|
|
|
public func request(_ request: SKRequest, didFailWithError error: Error) {
|
|
|
- debugPrint("PurchaseManager productsRequest error = \(error)")
|
|
|
+ dePrint("PurchaseManager productsRequest error = \(error)")
|
|
|
purchase(self, didChaged: .loadFail, object: error.localizedDescription,transaction: nil)
|
|
|
}
|
|
|
}
|
|
@@ -384,9 +394,9 @@ extension PurchaseManager: SKPaymentTransactionObserver {
|
|
|
|
|
|
// Transaction is in queue, user has been charged. Client should complete the transaction.
|
|
|
#if DEBUG
|
|
|
- verifyPayResult(transaction: transaction, useSandBox: true)
|
|
|
+ verifyPayResult(transaction: transaction, useSandBox: true,uiBlock: purchase(_:didChaged:object:transaction:))
|
|
|
#else
|
|
|
- verifyPayResult(transaction: transaction, useSandBox: false)
|
|
|
+ verifyPayResult(transaction: transaction, useSandBox: false,uiBlock: purchase(_:didChaged:object:transaction:))
|
|
|
#endif
|
|
|
|
|
|
case .failed:
|
|
@@ -440,9 +450,9 @@ extension PurchaseManager: SKPaymentTransactionObserver {
|
|
|
if let original = transaction.original,
|
|
|
original.transactionState == .purchased {
|
|
|
#if DEBUG
|
|
|
- verifyPayResult(transaction: transaction, useSandBox: true)
|
|
|
+ verifyPayResult(transaction: transaction, useSandBox: true,uiBlock: purchase(_:didChaged:object:transaction:))
|
|
|
#else
|
|
|
- verifyPayResult(transaction: transaction, useSandBox: false)
|
|
|
+ verifyPayResult(transaction: transaction, useSandBox: false,uiBlock: purchase(_:didChaged:object:transaction:))
|
|
|
#endif
|
|
|
} else {
|
|
|
purchase(self, didChaged: .restoreFail, object: "Failed to restore subscribe, please try again",transaction: transaction)
|
|
@@ -463,7 +473,7 @@ extension PurchaseManager: SKPaymentTransactionObserver {
|
|
|
|
|
|
public func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
|
|
|
if let trans = queue.transactions.first(where: { $0.transactionState == .purchased }) {
|
|
|
- verifyPayResult(transaction: trans, useSandBox: false)
|
|
|
+ verifyPayResult(transaction: trans, useSandBox: false,uiBlock: purchase(_:didChaged:object:transaction:))
|
|
|
} else if queue.transactions.isEmpty {
|
|
|
purchase(self, didChaged: .restoreFail, object: "You don't have an active subscription",transaction: nil)
|
|
|
}
|
|
@@ -482,12 +492,12 @@ extension PurchaseManager: SKPaymentTransactionObserver {
|
|
|
}
|
|
|
|
|
|
extension PurchaseManager {
|
|
|
- func verifyPayResult(transaction: SKPaymentTransaction, useSandBox: Bool) {
|
|
|
- purchase(self, didChaged: .verifying, object: nil,transaction: transaction)
|
|
|
|
|
|
+ func verifyPayResult(transaction: SKPaymentTransaction, useSandBox: Bool, uiBlock:((PurchaseManager,PremiumRequestState,Any?,SKPaymentTransaction?)->Void)?) {
|
|
|
+
|
|
|
guard let url = Bundle.main.appStoreReceiptURL,
|
|
|
let receiptData = try? Data(contentsOf: url) else {
|
|
|
- purchase(self, didChaged: .verifyFail, object: "凭证文件为空",transaction: transaction)
|
|
|
+ uiBlock?(self,.verifyFail,"凭证文件为空",transaction)
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -496,7 +506,7 @@ extension PurchaseManager {
|
|
|
"password": AppleSharedKey,
|
|
|
]
|
|
|
guard let requestData = try? JSONSerialization.data(withJSONObject: requestContents) else {
|
|
|
- purchase(self, didChaged: .verifyFail, object: "凭证文件为空",transaction: transaction)
|
|
|
+ uiBlock?(self,.verifyFail,"凭证文件为空",transaction)
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -509,18 +519,18 @@ extension PurchaseManager {
|
|
|
debugPrint("PurchaseManager verifyPayResult = \(jsonResponse)")
|
|
|
let status = jsonResponse["status"]
|
|
|
if let status = status as? String, status == "21007" {
|
|
|
- self.verifyPayResult(transaction: transaction, useSandBox: true)
|
|
|
+ self.verifyPayResult(transaction: transaction, useSandBox: true,uiBlock: uiBlock)
|
|
|
} else if let status = status as? Int, status == 21007 {
|
|
|
- self.verifyPayResult(transaction: transaction, useSandBox: true)
|
|
|
+ self.verifyPayResult(transaction: transaction, useSandBox: true,uiBlock: uiBlock)
|
|
|
} else if let status = status as? String, status == "0" {
|
|
|
- self.handlerPayResult(transaction: transaction, resp: jsonResponse)
|
|
|
+ self.handlerPayResult(transaction: transaction, resp: jsonResponse,uiBlock: uiBlock)
|
|
|
} else if let status = status as? Int, status == 0 {
|
|
|
- self.handlerPayResult(transaction: transaction, resp: jsonResponse)
|
|
|
+ self.handlerPayResult(transaction: transaction, resp: jsonResponse,uiBlock: uiBlock)
|
|
|
} else {
|
|
|
- self.purchase(self, didChaged: .verifyFail, object: "验证结果状态码错误:\(status.debugDescription)",transaction: transaction)
|
|
|
+ uiBlock?(self,.verifyFail,"验证结果状态码错误:\(status.debugDescription)",transaction)
|
|
|
}
|
|
|
} else {
|
|
|
- self.purchase(self, didChaged: .verifyFail, object: "验证结果为空",transaction: transaction)
|
|
|
+ uiBlock?(self,.verifyFail,"验证结果为空",transaction)
|
|
|
debugPrint("PurchaseManager 验证结果为空")
|
|
|
}
|
|
|
}
|
|
@@ -537,7 +547,7 @@ extension PurchaseManager {
|
|
|
*/
|
|
|
}
|
|
|
|
|
|
- func handlerPayResult(transaction: SKPaymentTransaction, resp: [String: Any]) {
|
|
|
+ func handlerPayResult(transaction: SKPaymentTransaction, resp: [String: Any], uiBlock:((PurchaseManager,PremiumRequestState,Any?,SKPaymentTransaction?)->Void)?) {
|
|
|
|
|
|
//购买视频次数相关
|
|
|
//分析处理购买消耗品的逻辑处理
|
|
@@ -579,9 +589,9 @@ extension PurchaseManager {
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
if transaction.transactionState == .restored {
|
|
|
- self.purchase(self, didChaged: .restoreSuccess, object: nil,transaction: transaction)
|
|
|
+ uiBlock?(self, .restoreSuccess, nil, transaction)
|
|
|
} else {
|
|
|
- self.purchase(self, didChaged: .paySuccess, object: nil,transaction: transaction)
|
|
|
+ uiBlock?(self, .paySuccess, nil, transaction)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -593,8 +603,8 @@ extension PurchaseManager {
|
|
|
guard !resp.isEmpty else {
|
|
|
return false
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
guard let pendingRenewalInfoArray = resp["pending_renewal_info"] as? [[String: Any]]
|
|
|
else { return false}
|
|
|
|
|
@@ -604,21 +614,44 @@ extension PurchaseManager {
|
|
|
"auto_renew_status" = 1;
|
|
|
"original_transaction_id" = 2000000929272571;
|
|
|
"product_id" = 101;
|
|
|
- }*/
|
|
|
+ }
|
|
|
+ "latest_receipt_info": <__NSArrayI 0x10e290500>(
|
|
|
+ {
|
|
|
+ "expires_date" = "2025-08-11 07:54:13 Etc/GMT";
|
|
|
+ "expires_date_ms" = 1754898853000;
|
|
|
+ "product_id" = 101;
|
|
|
+ "transaction_id" = 2000000980915817;
|
|
|
+ "web_order_line_item_id" = 2000000108151289;
|
|
|
+ }
|
|
|
+ */
|
|
|
|
|
|
if let firstItem = pendingRenewalInfoArray.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 auto_renew_product_id = firstItem["auto_renew_product_id"] as? String,//待生效的
|
|
|
+ let product_id = firstItem["product_id"] as? String //当前的
|
|
|
+ {
|
|
|
+ if auto_renew_product_id != 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 expires_date_ms = firstItem["expires_date_ms"] as? String { //拿到当前会员的过期时间错
|
|
|
+ //当前的过期时间戳
|
|
|
let expiresms = Int(expires_date_ms) ?? 0
|
|
|
- let milliseconds = period(for: auto_renew_product_id).milliseconds
|
|
|
+ //待生效会员的时间戳
|
|
|
+ let autoRenewPeriod = period(for: auto_renew_product_id)
|
|
|
+ let milliseconds = autoRenewPeriod.milliseconds
|
|
|
+
|
|
|
+ //当前的过期时间+加上待生效的会员过期时长
|
|
|
let totalExpiresms = expiresms + milliseconds
|
|
|
-
|
|
|
- updateExpireTime(String(totalExpiresms), for: auto_renew_product_id)
|
|
|
+
|
|
|
+ //先用待生效的会员商品id
|
|
|
+ var newProductId = auto_renew_product_id
|
|
|
+ //如果当前的权重大于待生效的,则用当前的
|
|
|
+ let currentProductPeriod = period(for: product_id)
|
|
|
+ if currentProductPeriod.weight > autoRenewPeriod.weight {
|
|
|
+ newProductId = product_id
|
|
|
+ }
|
|
|
+
|
|
|
+ updateExpireTime(String(totalExpiresms), for: newProductId)
|
|
|
return true
|
|
|
}
|
|
|
}
|
|
@@ -679,8 +712,6 @@ public extension PurchaseManager {
|
|
|
}
|
|
|
|
|
|
func purchase(_ manager: PurchaseManager, didChaged state: PremiumRequestState, object: Any?,transaction:SKPaymentTransaction?) {
|
|
|
- onPurchaseStateChanged?(manager, state, object)
|
|
|
-
|
|
|
if transaction == nil{
|
|
|
onPurchaseStateChanged?(manager, state, object)
|
|
|
}else if transaction?.transactionState == .restored,paymentProductIdentifier == "restoreCompletedTransactions"{
|