diff --git a/iosApp/iosApp/Subscription/StoreKitManager.swift b/iosApp/iosApp/Subscription/StoreKitManager.swift index 2e971e7..b6689ce 100644 --- a/iosApp/iosApp/Subscription/StoreKitManager.swift +++ b/iosApp/iosApp/Subscription/StoreKitManager.swift @@ -184,54 +184,42 @@ class StoreKitManager: ObservableObject { } // Get transaction receipt data - guard let receiptData = try? await getReceiptData(for: transaction) else { - print("❌ StoreKit: Failed to get receipt data") - return - } + let receiptData = String(transaction.id) // Call backend verification endpoint - let result = await subscriptionApi.verifyIOSReceipt( + let result = try await subscriptionApi.verifyIOSReceipt( token: token, receiptData: receiptData, transactionId: String(transaction.id) ) - switch result { - case .success(let response): - print("✅ StoreKit: Backend verification successful - Tier: \(response.tier)") + // Handle result (Kotlin ApiResult type) + if let successResult = result as? ApiResultSuccess, + let response = successResult.data, + response.success { + print("✅ StoreKit: Backend verification successful - Tier: \(response.tier ?? "unknown")") - // Update subscription cache - await MainActor.run { - let subscription = SubscriptionStatus( - tier: response.tier, - usage: response.usage, - limits: response.limits, - limitationsEnabled: response.limitationsEnabled - ) - SubscriptionCacheWrapper.shared.updateSubscription(subscription) + // Fetch updated subscription status from backend + let statusResult = try await subscriptionApi.getSubscriptionStatus(token: token) + + if let statusSuccess = statusResult as? ApiResultSuccess, + let subscription = statusSuccess.data { + await MainActor.run { + SubscriptionCacheWrapper.shared.updateSubscription(subscription) + } } - - case .failure(let error): - print("❌ StoreKit: Backend verification failed: \(error)") - - case .loading: - break - - case .idle: - break + } else if let errorResult = result as? ApiResultError { + print("❌ StoreKit: Backend verification failed: \(errorResult.message)") + } else if let successResult = result as? ApiResultSuccess, + let response = successResult.data, + !response.success { + print("❌ StoreKit: Backend verification failed: \(response.error ?? "Unknown error")") } } catch { print("❌ StoreKit: Backend verification error: \(error)") } } - /// Get receipt data for transaction - private func getReceiptData(for transaction: Transaction) async throws -> String { - // In StoreKit 2, we send the transaction ID instead of the legacy receipt - // The backend should verify the transaction with Apple's servers - return String(transaction.id) - } - /// Verify the cryptographic signature of a transaction private func checkVerified(_ result: VerificationResult) throws -> T { switch result {