|
| 1 | +/** |
| 2 | + * Copyright (c) 2016-present Invertase Limited & Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this library except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +import Foundation |
| 19 | +import FirebaseAnalytics |
| 20 | +import StoreKit |
| 21 | + |
| 22 | +/// Swift wrapper for logging a verified StoreKit 2 transaction to Firebase Analytics. |
| 23 | +/// Accessible from Objective-C; necessary because StoreKit 2 and Analytics.logTransaction use Swift async APIs. |
| 24 | +/// Call from ObjC only when @available(iOS 15.0, *) (see RNFBFunctionsStreamHandler pattern). |
| 25 | +@available(iOS 15.0, macOS 12.0, *) |
| 26 | +@objcMembers public class RNFBAnalyticsLogTransaction: NSObject { |
| 27 | + |
| 28 | + private static let kCode = "firebase_analytics" |
| 29 | + private var logTask: Task<Void, Never>? |
| 30 | + |
| 31 | + /// Resolve/reject types matching RCTPromiseResolveBlock / RCTPromiseRejectBlock for React Native bridge. |
| 32 | + @objc public func logTransaction( |
| 33 | + transactionId: String, |
| 34 | + resolve: @escaping (Any?) -> Void, |
| 35 | + reject: @escaping (String, String, NSError?) -> Void |
| 36 | + ) { |
| 37 | + logTask = Task { |
| 38 | + await performLogTransaction(transactionId: transactionId, resolve: resolve, reject: reject) |
| 39 | + logTask = nil |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private func performLogTransaction( |
| 44 | + transactionId: String, |
| 45 | + resolve: @escaping (Any?) -> Void, |
| 46 | + reject: @escaping (String, String, NSError?) -> Void |
| 47 | + ) async { |
| 48 | + guard let id = UInt64(transactionId) else { |
| 49 | + await MainActor.run { reject(Self.kCode, "Invalid transactionId", nil) } |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + var foundTransaction: StoreKit.Transaction? |
| 54 | + for await result in StoreKit.Transaction.all { |
| 55 | + switch result { |
| 56 | + case let .verified(transaction): |
| 57 | + if transaction.id == id { |
| 58 | + foundTransaction = transaction |
| 59 | + break |
| 60 | + } |
| 61 | + case .unverified: |
| 62 | + continue |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + guard let transaction = foundTransaction else { |
| 67 | + await MainActor.run { reject(Self.kCode, "Transaction not found", nil) } |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + Analytics.logTransaction(transaction) |
| 72 | + await MainActor.run { resolve(NSNull()) } |
| 73 | + } |
| 74 | +} |
0 commit comments