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

@@ -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
}