This commit is contained in:
2025-12-16 15:47:12 +08:00
parent c898d16688
commit 30f2e4f24f
5 changed files with 51 additions and 13 deletions

View File

@@ -255,12 +255,17 @@ internal class StoreKitService: ObservableObject {
switch result { switch result {
case .success(let verification): case .success(let verification):
do { do {
let payload = verification.jwsRepresentation
let transaction = try verifyPurchase(verification) let transaction = try verifyPurchase(verification)
await printProductDetails(product) await printProductDetails(product)
// //
await printTransactionDetails(transaction) await printTransactionDetails(transaction)
await MainActor.run {
self.delegate?.service(self, didCompletePurchaseFor: transaction.productID, payload: payload)
}
// //
await transaction.finish() await transaction.finish()
@@ -561,11 +566,13 @@ internal class StoreKitService: ObservableObject {
// //
while !Task.isCancelled { while !Task.isCancelled {
// //
if self.config.showLog {
let now = Date() let now = Date()
let formatter = DateFormatter() let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.timeZone = TimeZone.current formatter.timeZone = TimeZone.current
print("当前订阅检测时间: \(formatter.string(from: now))") print("当前订阅检测时间: \(formatter.string(from: now))")
}
await self.checkSubscriptionStatus() await self.checkSubscriptionStatus()
// 30 // 30

View File

@@ -20,5 +20,7 @@ internal protocol StoreKitServiceDelegate: AnyObject {
/// ///
func service(_ service: StoreKitService, didUpdatePurchasedTransactions efficient: [Transaction], latests: [Transaction]) func service(_ service: StoreKitService, didUpdatePurchasedTransactions efficient: [Transaction], latests: [Transaction])
}
///
func service(_ service: StoreKitService, didCompletePurchaseFor productId: String, payload: String)
}

View File

@@ -52,7 +52,7 @@ final class KBStoreKitBridge: NSObject, StoreKitDelegate {
do { do {
try await self.manager.purchase(productId: productId) 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) self.verifySignedPayload(payload, completion: completion)
} else { } else {
await MainActor.run { await MainActor.run {
@@ -115,11 +115,25 @@ final class KBStoreKitBridge: NSObject, StoreKitDelegate {
} }
} }
private static func latestJWSPayload(for productId: String) async -> String? { @MainActor
guard let result = await Transaction.latest(for: productId) else { return nil } private func fetchPayload(for productId: String) async -> String? {
if case .verified = result { 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 return result.jwsRepresentation
} }
attempts += 1
if attempts < retryCount {
try? await Task.sleep(nanoseconds: 300_000_000) // 0.3s
}
}
return nil return nil
} }
} }

View File

@@ -80,6 +80,9 @@ public class StoreKit2Manager {
/// ///
public private(set) var latestTransactions: [Transaction] = [] public private(set) var latestTransactions: [Transaction] = []
/// ID
private var recentJWSPayloads: [String: String] = [:]
// MARK: - // MARK: -
/// ///
@@ -379,6 +382,14 @@ public class StoreKit2Manager {
return latestTransactions return latestTransactions
} }
/// JWS StoreKitService
/// - Parameter productId: ID
/// - Returns:
@MainActor
func consumeRecentPayload(for productId: String) -> String? {
return recentJWSPayloads.removeValue(forKey: productId)
}
/// ///
/// - Parameter productId: ID /// - Parameter productId: ID
/// - Returns: /// - Returns:
@@ -532,5 +543,9 @@ extension StoreKit2Manager: StoreKitServiceDelegate {
// //
onPurchasedTransactionsUpdated?(efficient, latests) onPurchasedTransactionsUpdated?(efficient, latests)
} }
}
@MainActor
func service(_ service: StoreKitService, didCompletePurchaseFor productId: String, payload: String) {
recentJWSPayloads[productId] = payload
}
}

View File

@@ -39,7 +39,7 @@
return; return;
} }
if (completion) completion(KBBizCodeSuccess, @"ok"); if (completion) completion(KBBizCodeSuccess, @"JWS-ok");
}]; }];
} }