@@ -43,27 +43,11 @@ import StoreKit
4343 let request = ProductRequest ( skus: skus, type: productType)
4444 let result = try await fetchProducts ( request)
4545
46- let encoder = JSONEncoder ( )
47- encoder. dateEncodingStrategy = . millisecondsSince1970
48-
49- switch result {
50- case . products( let products) :
51- // Cast to concrete type ProductIOS and convert to dictionaries
52- let productIOS = products? . compactMap { $0 as? ProductIOS }
53- let dictionaries = try productIOS? . compactMap { product -> [ String : Any ] ? in
54- let jsonData = try encoder. encode ( product)
55- return try JSONSerialization . jsonObject ( with: jsonData) as? [ String : Any ]
56- }
57- completion ( dictionaries, nil )
58- case . subscriptions( let subscriptions) :
59- // Cast to concrete type ProductSubscriptionIOS and convert to dictionaries
60- let subscriptionIOS = subscriptions? . compactMap { $0 as? ProductSubscriptionIOS }
61- let dictionaries = try subscriptionIOS? . compactMap { subscription -> [ String : Any ] ? in
62- let jsonData = try encoder. encode ( subscription)
63- return try JSONSerialization . jsonObject ( with: jsonData) as? [ String : Any ]
64- }
65- completion ( dictionaries, nil )
46+ // Use OpenIapSerialization to convert products to dictionaries
47+ let dictionaries = OpenIapSerialization . products ( result) { message in
48+ print ( " [OpenIAP] \( message) " )
6649 }
50+ completion ( dictionaries, nil )
6751 } catch {
6852 completion ( nil , error)
6953 }
@@ -74,7 +58,13 @@ import StoreKit
7458 Task {
7559 do {
7660 let product = try await getPromotedProductIOS ( )
77- completion ( product as Any ? , nil )
61+ if let productIOS = product {
62+ // Convert ProductIOS to dictionary
63+ let dictionary = OpenIapSerialization . encode ( productIOS)
64+ completion ( dictionary, nil )
65+ } else {
66+ completion ( nil , nil )
67+ }
7868 } catch {
7969 completion ( nil , error)
8070 }
@@ -110,9 +100,15 @@ import StoreKit
110100
111101 switch result {
112102 case . purchase( let purchase) :
113- completion ( purchase as Any , nil )
103+ let dictionary = OpenIapSerialization . purchase ( purchase)
104+ completion ( dictionary, nil )
114105 case . purchases( let purchases) :
115- completion ( purchases? . first as Any , nil )
106+ if let firstPurchase = purchases? . first {
107+ let dictionary = OpenIapSerialization . purchase ( firstPurchase)
108+ completion ( dictionary, nil )
109+ } else {
110+ completion ( nil , nil )
111+ }
116112 case . none:
117113 completion ( nil , nil )
118114 }
@@ -137,7 +133,8 @@ import StoreKit
137133 Task {
138134 do {
139135 let purchases = try await getAvailablePurchases ( nil )
140- completion ( purchases as [ Any ] , nil )
136+ let dictionaries = OpenIapSerialization . purchases ( purchases)
137+ completion ( dictionaries, nil )
141138 } catch {
142139 completion ( nil , error)
143140 }
@@ -177,7 +174,8 @@ import StoreKit
177174 Task {
178175 do {
179176 let transactions = try await getPendingTransactionsIOS ( )
180- completion ( transactions as [ Any ] , nil )
177+ let dictionaries = OpenIapSerialization . purchases ( transactions)
178+ completion ( dictionaries, nil )
181179 } catch {
182180 completion ( nil , error)
183181 }
@@ -227,7 +225,8 @@ import StoreKit
227225 Task {
228226 do {
229227 let subscriptions = try await getActiveSubscriptions ( nil )
230- completion ( subscriptions as [ Any ] , nil )
228+ let dictionaries = subscriptions. map { OpenIapSerialization . encode ( $0) }
229+ completion ( dictionaries, nil )
231230 } catch {
232231 completion ( nil , error)
233232 }
@@ -251,7 +250,8 @@ import StoreKit
251250 Task {
252251 do {
253252 let purchases = try await showManageSubscriptionsIOS ( )
254- completion ( purchases as [ Any ] , nil )
253+ let dictionaries = OpenIapSerialization . purchases ( purchases)
254+ completion ( dictionaries, nil )
255255 } catch {
256256 completion ( nil , error)
257257 }
0 commit comments