diff --git a/keyBoard/Class/Pay/StoreKit2Manager/Internal/StoreKitService.swift b/keyBoard/Class/Pay/StoreKit2Manager/Internal/StoreKitService.swift index 2be3ff0..2686a97 100644 --- a/keyBoard/Class/Pay/StoreKit2Manager/Internal/StoreKitService.swift +++ b/keyBoard/Class/Pay/StoreKit2Manager/Internal/StoreKitService.swift @@ -255,12 +255,17 @@ internal class StoreKitService: ObservableObject { switch result { case .success(let verification): do { + let payload = verification.jwsRepresentation let transaction = try verifyPurchase(verification) await printProductDetails(product) // 打印详细的交易信息 await printTransactionDetails(transaction) + await MainActor.run { + self.delegate?.service(self, didCompletePurchaseFor: transaction.productID, payload: payload) + } + // 先完成交易 await transaction.finish() @@ -561,11 +566,13 @@ internal class StoreKitService: ObservableObject { // 持续监听,直到任务被取消 while !Task.isCancelled { // 检查所有订阅的状态(并行检查,提高效率) - let now = Date() - let formatter = DateFormatter() - formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" - formatter.timeZone = TimeZone.current - print("当前订阅检测时间: \(formatter.string(from: now))") + if self.config.showLog { + let now = Date() + let formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" + formatter.timeZone = TimeZone.current + print("当前订阅检测时间: \(formatter.string(from: now))") + } await self.checkSubscriptionStatus() // 等待指定间隔(默认30秒)后再次检查 diff --git a/keyBoard/Class/Pay/StoreKit2Manager/Internal/StoreKitServiceDelegate.swift b/keyBoard/Class/Pay/StoreKit2Manager/Internal/StoreKitServiceDelegate.swift index b859ff6..723e438 100644 --- a/keyBoard/Class/Pay/StoreKit2Manager/Internal/StoreKitServiceDelegate.swift +++ b/keyBoard/Class/Pay/StoreKit2Manager/Internal/StoreKitServiceDelegate.swift @@ -20,5 +20,7 @@ internal protocol StoreKitServiceDelegate: AnyObject { /// 已购买交易订单更新 func service(_ service: StoreKitService, didUpdatePurchasedTransactions efficient: [Transaction], latests: [Transaction]) + + /// 某个交易完成(验证成功) + func service(_ service: StoreKitService, didCompletePurchaseFor productId: String, payload: String) } - diff --git a/keyBoard/Class/Pay/StoreKit2Manager/KBStoreKitBridge.swift b/keyBoard/Class/Pay/StoreKit2Manager/KBStoreKitBridge.swift index f70e24f..aedc377 100644 --- a/keyBoard/Class/Pay/StoreKit2Manager/KBStoreKitBridge.swift +++ b/keyBoard/Class/Pay/StoreKit2Manager/KBStoreKitBridge.swift @@ -52,7 +52,7 @@ final class KBStoreKitBridge: NSObject, StoreKitDelegate { do { try await self.manager.purchase(productId: productId) - if let payload = await Self.latestJWSPayload(for: productId) { + if let payload = await self.fetchPayload(for: productId) { self.verifySignedPayload(payload, completion: completion) } else { await MainActor.run { @@ -115,10 +115,24 @@ final class KBStoreKitBridge: NSObject, StoreKitDelegate { } } - private static func latestJWSPayload(for productId: String) async -> String? { - guard let result = await Transaction.latest(for: productId) else { return nil } - if case .verified = result { - return result.jwsRepresentation + @MainActor + private func fetchPayload(for productId: String) async -> String? { + if let payload = manager.consumeRecentPayload(for: productId) { + return payload + } + return await Self.latestJWSPayload(for: productId, retryCount: 3) + } + + private static func latestJWSPayload(for productId: String, retryCount: Int = 1) async -> String? { + var attempts = 0 + while attempts < retryCount { + if let result = await Transaction.latest(for: productId), case .verified = result { + return result.jwsRepresentation + } + attempts += 1 + if attempts < retryCount { + try? await Task.sleep(nanoseconds: 300_000_000) // 0.3s + } } return nil } diff --git a/keyBoard/Class/Pay/StoreKit2Manager/StoreKitManager.swift b/keyBoard/Class/Pay/StoreKit2Manager/StoreKitManager.swift index d2aef5a..0323216 100644 --- a/keyBoard/Class/Pay/StoreKit2Manager/StoreKitManager.swift +++ b/keyBoard/Class/Pay/StoreKit2Manager/StoreKitManager.swift @@ -80,6 +80,9 @@ public class StoreKit2Manager { /// 每个产品的最新交易记录集合 public private(set) var latestTransactions: [Transaction] = [] + /// 最近一次完成购买的交易签名缓存(按产品ID) + private var recentJWSPayloads: [String: String] = [:] + // MARK: - 按类型分类的产品(计算属性) /// 非消耗品 @@ -379,6 +382,14 @@ public class StoreKit2Manager { return latestTransactions } + /// 获取指定产品最近一次完成的 JWS(仅限 StoreKitService 捕获到的交易) + /// - Parameter productId: 产品 ID + /// - Returns: 若存在则返回并移除缓存 + @MainActor + func consumeRecentPayload(for productId: String) -> String? { + return recentJWSPayloads.removeValue(forKey: productId) + } + /// 获取交易历史 /// - Parameter productId: 可选的产品ID,如果提供则只返回该产品的交易历史 /// - Returns: 交易历史记录数组,按购买日期倒序排列 @@ -532,5 +543,9 @@ extension StoreKit2Manager: StoreKitServiceDelegate { // 通知闭包回调 onPurchasedTransactionsUpdated?(efficient, latests) } + + @MainActor + func service(_ service: StoreKitService, didCompletePurchaseFor productId: String, payload: String) { + recentJWSPayloads[productId] = payload + } } - diff --git a/keyBoard/Class/Pay/VM/PayVM.m b/keyBoard/Class/Pay/VM/PayVM.m index 88026a2..4f68c86 100644 --- a/keyBoard/Class/Pay/VM/PayVM.m +++ b/keyBoard/Class/Pay/VM/PayVM.m @@ -39,7 +39,7 @@ return; } - if (completion) completion(KBBizCodeSuccess, @"ok"); + if (completion) completion(KBBizCodeSuccess, @"JWS-ok"); }]; }