@capgo/native-purchases 7.18.0-alpha.0 → 7.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,10 +2,6 @@ import Foundation
2
2
  import Capacitor
3
3
  import StoreKit
4
4
 
5
- /**
6
- * Please read the Capacitor iOS Plugin Development Guide
7
- * here: https://capacitorjs.com/docs/plugins/ios
8
- */
9
5
  @objc(NativePurchasesPlugin)
10
6
  public class NativePurchasesPlugin: CAPPlugin, CAPBridgedPlugin {
11
7
  public let identifier = "NativePurchasesPlugin"
@@ -22,10 +18,11 @@ public class NativePurchasesPlugin: CAPPlugin, CAPBridgedPlugin {
22
18
  CAPPluginMethod(name: "acknowledgePurchase", returnType: CAPPluginReturnPromise),
23
19
  CAPPluginMethod(name: "consumePurchase", returnType: CAPPluginReturnPromise),
24
20
  CAPPluginMethod(name: "getAppTransaction", returnType: CAPPluginReturnPromise),
25
- CAPPluginMethod(name: "isEntitledToOldBusinessModel", returnType: CAPPluginReturnPromise)
21
+ CAPPluginMethod(name: "isEntitledToOldBusinessModel", returnType: CAPPluginReturnPromise),
22
+ CAPPluginMethod(name: "getStorefront", returnType: CAPPluginReturnPromise)
26
23
  ]
27
24
 
28
- private let pluginVersion: String = "7.18.0-alpha.0"
25
+ private let pluginVersion: String = "7.19.0"
29
26
  private var transactionUpdatesTask: Task<Void, Never>?
30
27
 
31
28
  @objc func getPluginVersion(_ call: CAPPluginCall) {
@@ -34,38 +31,28 @@ public class NativePurchasesPlugin: CAPPlugin, CAPBridgedPlugin {
34
31
 
35
32
  override public func load() {
36
33
  super.load()
37
- // Start listening to StoreKit transaction updates as early as possible
38
- if #available(iOS 15.0, *) {
39
- startTransactionUpdatesListener()
40
- }
34
+ startTransactionUpdatesListener()
41
35
  }
42
36
 
43
37
  deinit {
44
- if #available(iOS 15.0, *) { cancelTransactionUpdatesListener() }
45
- }
46
-
47
- private func cancelTransactionUpdatesListener() {
48
- self.transactionUpdatesTask?.cancel()
49
- self.transactionUpdatesTask = nil
38
+ transactionUpdatesTask?.cancel()
39
+ transactionUpdatesTask = nil
50
40
  }
51
41
 
52
- @available(iOS 15.0, *)
53
42
  private func startTransactionUpdatesListener() {
54
- // Ensure only one listener is running
55
- cancelTransactionUpdatesListener()
56
- let task = Task.detached { [weak self] in
43
+ transactionUpdatesTask?.cancel()
44
+ transactionUpdatesTask = Task.detached { [weak self] in
57
45
  for await result in Transaction.updates {
58
46
  guard !Task.isCancelled else { break }
59
47
  switch result {
60
48
  case .verified(let transaction):
61
- // Build payload similar to purchase response
62
- let payload = await TransactionHelpers.buildTransactionResponse(from: transaction, jwsRepresentation: result.jwsRepresentation, alwaysIncludeWillCancel: true)
63
-
64
- // Finish the transaction to avoid blocking future purchases
49
+ let payload = await TransactionHelpers.buildTransactionResponse(
50
+ from: transaction,
51
+ jwsRepresentation: result.jwsRepresentation,
52
+ alwaysIncludeWillCancel: true
53
+ )
65
54
  await transaction.finish()
66
-
67
- // Notify JS listeners on main thread, after slight delay
68
- try? await Task.sleep(nanoseconds: 500_000_000) // 0.5s delay
55
+ try? await Task.sleep(nanoseconds: 500_000_000)
69
56
  await MainActor.run {
70
57
  self?.notifyListeners("transactionUpdated", data: payload)
71
58
  }
@@ -79,93 +66,89 @@ public class NativePurchasesPlugin: CAPPlugin, CAPBridgedPlugin {
79
66
  }
80
67
  }
81
68
  }
82
- transactionUpdatesTask = task
83
69
  }
84
70
 
85
- // MARK: - Plugin Methods
86
-
87
71
  @objc func isBillingSupported(_ call: CAPPluginCall) {
88
- if #available(iOS 15, *) {
89
- call.resolve([
90
- "isBillingSupported": true
91
- ])
92
- } else {
93
- call.resolve([
94
- "isBillingSupported": false
95
- ])
96
- }
72
+ call.resolve(["isBillingSupported": true])
97
73
  }
98
74
 
99
- @objc func purchaseProduct(_ call: CAPPluginCall) {
100
- if #available(iOS 15, *) {
101
- print("purchaseProduct")
102
- let productIdentifier = call.getString("productIdentifier", "")
103
- let quantity = call.getInt("quantity", 1)
104
- let appAccountToken = call.getString("appAccountToken")
105
- let autoAcknowledge = call.getBool("autoAcknowledgePurchases") ?? true
106
-
107
- if productIdentifier.isEmpty {
108
- call.reject("productIdentifier is Empty, give an id")
109
- return
75
+ @objc func getStorefront(_ call: CAPPluginCall) {
76
+ print("getStorefront")
77
+ Task {
78
+ let storefront = await Storefront.current
79
+ await MainActor.run {
80
+ if let storefront = storefront {
81
+ call.resolve([
82
+ "countryCode": storefront.countryCode,
83
+ "storefrontId": storefront.id
84
+ ])
85
+ } else {
86
+ // No storefront (e.g. alternative distribution).
87
+ print("getStorefront: no storefront available")
88
+ call.resolve(["countryCode": ""])
89
+ }
110
90
  }
91
+ }
92
+ }
111
93
 
112
- print("Auto-acknowledge enabled: \(autoAcknowledge)")
113
-
114
- Task { @MainActor in
115
- do {
116
- let products = try await Product.products(for: [productIdentifier])
117
- guard let product = products.first else {
118
- call.reject("Cannot find product for id \(productIdentifier)")
119
- return
120
- }
94
+ @objc func purchaseProduct(_ call: CAPPluginCall) {
95
+ print("purchaseProduct")
96
+ let productIdentifier = call.getString("productIdentifier", "")
97
+ let quantity = call.getInt("quantity", 1)
98
+ let appAccountToken = call.getString("appAccountToken")
99
+ let autoAcknowledge = call.getBool("autoAcknowledgePurchases") ?? true
100
+
101
+ if productIdentifier.isEmpty {
102
+ call.reject("productIdentifier is Empty, give an id")
103
+ return
104
+ }
121
105
 
122
- let purchaseOptions = self.buildPurchaseOptions(quantity: quantity, appAccountToken: appAccountToken)
123
- let result = try await product.purchase(options: purchaseOptions)
124
- print("purchaseProduct result \(result)")
106
+ print("Auto-acknowledge enabled: \(autoAcknowledge)")
125
107
 
126
- await self.handlePurchaseResult(result, call: call, autoFinish: autoAcknowledge)
127
- } catch {
128
- print(error)
129
- call.reject(error.localizedDescription)
108
+ Task { @MainActor in
109
+ do {
110
+ let products = try await Product.products(for: [productIdentifier])
111
+ guard let product = products.first else {
112
+ call.reject("Cannot find product for id \(productIdentifier)")
113
+ return
130
114
  }
131
- }
132
- } else {
133
- print("Not implemented under ios 15")
134
- call.reject("Not implemented under ios 15")
135
- }
136
- }
137
115
 
138
- @available(iOS 15.0, *)
139
- private func buildPurchaseOptions(quantity: Int, appAccountToken: String?) -> Set<Product.PurchaseOption> {
140
- var purchaseOptions = Set<Product.PurchaseOption>()
141
- purchaseOptions.insert(Product.PurchaseOption.quantity(quantity))
116
+ var purchaseOptions = Set<Product.PurchaseOption>()
117
+ purchaseOptions.insert(.quantity(quantity))
118
+ if let token = appAccountToken, !token.isEmpty, let uuid = UUID(uuidString: token) {
119
+ purchaseOptions.insert(.appAccountToken(uuid))
120
+ }
142
121
 
143
- if let accountToken = appAccountToken, !accountToken.isEmpty, let tokenData = UUID(uuidString: accountToken) {
144
- purchaseOptions.insert(Product.PurchaseOption.appAccountToken(tokenData))
122
+ let result = try await product.purchase(options: purchaseOptions)
123
+ print("purchaseProduct result \(result)")
124
+ await self.handlePurchaseResult(result, call: call, autoFinish: autoAcknowledge)
125
+ } catch {
126
+ print(error)
127
+ call.reject(error.localizedDescription)
128
+ }
145
129
  }
146
-
147
- return purchaseOptions
148
130
  }
149
131
 
150
- @available(iOS 15.0, *)
151
132
  @MainActor
152
- private func handlePurchaseResult(_ result: Product.PurchaseResult, call: CAPPluginCall, autoFinish: Bool) async {
133
+ private func handlePurchaseResult(
134
+ _ result: Product.PurchaseResult,
135
+ call: CAPPluginCall,
136
+ autoFinish: Bool
137
+ ) async {
153
138
  switch result {
154
139
  case let .success(verificationResult):
155
140
  switch verificationResult {
156
141
  case .verified(let transaction):
157
- let response = await TransactionHelpers.buildTransactionResponse(from: transaction, jwsRepresentation: verificationResult.jwsRepresentation)
158
-
142
+ let response = await TransactionHelpers.buildTransactionResponse(
143
+ from: transaction,
144
+ jwsRepresentation: verificationResult.jwsRepresentation
145
+ )
159
146
  if autoFinish {
160
147
  print("Auto-finishing transaction: \(transaction.id)")
161
148
  await transaction.finish()
162
149
  } else {
163
150
  print("Manual finish required for transaction: \(transaction.id)")
164
- print("Transaction will remain unfinished until acknowledgePurchase() is called")
165
- // Don't finish - transaction remains in StoreKit's queue
166
- // Can be retrieved later via Transaction.all
167
151
  }
168
-
169
152
  call.resolve(response)
170
153
  case .unverified(_, let error):
171
154
  call.reject(error.localizedDescription)
@@ -180,185 +163,127 @@ public class NativePurchasesPlugin: CAPPlugin, CAPBridgedPlugin {
180
163
  }
181
164
 
182
165
  @objc func restorePurchases(_ call: CAPPluginCall) {
183
- if #available(iOS 15.0, *) {
184
- print("restorePurchases")
185
- Task {
186
- do {
187
- try await AppStore.sync()
188
- // make finish() calls for all transactions and consume all consumables
189
- for transaction in SKPaymentQueue.default().transactions {
190
- SKPaymentQueue.default().finishTransaction(transaction)
191
- }
192
- await MainActor.run {
193
- call.resolve()
194
- }
195
- } catch {
196
- await MainActor.run {
197
- call.reject(error.localizedDescription)
198
- }
166
+ print("restorePurchases")
167
+ Task {
168
+ do {
169
+ try await AppStore.sync()
170
+ for transaction in SKPaymentQueue.default().transactions {
171
+ SKPaymentQueue.default().finishTransaction(transaction)
199
172
  }
173
+ await MainActor.run { call.resolve() }
174
+ } catch {
175
+ await MainActor.run { call.reject(error.localizedDescription) }
200
176
  }
201
- } else {
202
- print("Not implemented under ios 15")
203
- call.reject("Not implemented under ios 15")
204
177
  }
205
178
  }
206
179
 
207
180
  @objc func getProducts(_ call: CAPPluginCall) {
208
- if #available(iOS 15.0, *) {
209
- let productIdentifiers = call.getArray("productIdentifiers", String.self) ?? []
210
- print("productIdentifiers \(productIdentifiers)")
211
- Task {
212
- do {
213
- let products = try await Product.products(for: productIdentifiers)
214
- print("products \(products)")
215
- let productsJson: [[String: Any]] = products.map { $0.dictionary }
216
- await MainActor.run {
217
- call.resolve([
218
- "products": productsJson
219
- ])
220
- }
221
- } catch {
222
- print("error \(error)")
223
- await MainActor.run {
224
- call.reject(error.localizedDescription)
225
- }
226
- }
181
+ let productIdentifiers = call.getArray("productIdentifiers", String.self) ?? []
182
+ let productType = call.getString("productType", "inapp")
183
+ print("productIdentifiers \(productIdentifiers)")
184
+ print("productType \(productType)")
185
+ Task {
186
+ do {
187
+ let products = try await Product.products(for: productIdentifiers)
188
+ print("products \(products)")
189
+ let productsJson: [[String: Any]] = products.map { $0.dictionary }
190
+ await MainActor.run { call.resolve(["products": productsJson]) }
191
+ } catch {
192
+ print("error \(error)")
193
+ await MainActor.run { call.reject(error.localizedDescription) }
227
194
  }
228
- } else {
229
- print("Not implemented under ios 15")
230
- call.reject("Not implemented under ios 15")
231
195
  }
232
196
  }
233
197
 
234
198
  @objc func getProduct(_ call: CAPPluginCall) {
235
- if #available(iOS 15.0, *) {
236
- let productIdentifier = call.getString("productIdentifier") ?? ""
237
- print("productIdentifier \(productIdentifier)")
238
- if productIdentifier.isEmpty {
239
- call.reject("productIdentifier is empty")
240
- return
241
- }
199
+ let productIdentifier = call.getString("productIdentifier") ?? ""
200
+ let productType = call.getString("productType", "inapp")
201
+ print("productIdentifier \(productIdentifier)")
202
+ print("productType \(productType)")
203
+ if productIdentifier.isEmpty {
204
+ call.reject("productIdentifier is empty")
205
+ return
206
+ }
242
207
 
243
- Task {
244
- do {
245
- let products = try await Product.products(for: [productIdentifier])
246
- print("products \(products)")
247
- if let product = products.first {
248
- let productJson = product.dictionary
249
- await MainActor.run {
250
- call.resolve(["product": productJson])
251
- }
252
- } else {
253
- await MainActor.run {
254
- call.reject("Product not found")
255
- }
256
- }
257
- } catch {
258
- print(error)
259
- await MainActor.run {
260
- call.reject(error.localizedDescription)
261
- }
208
+ Task {
209
+ do {
210
+ let products = try await Product.products(for: [productIdentifier])
211
+ print("products \(products)")
212
+ if let product = products.first {
213
+ await MainActor.run { call.resolve(["product": product.dictionary]) }
214
+ } else {
215
+ await MainActor.run { call.reject("Product not found") }
262
216
  }
217
+ } catch {
218
+ print(error)
219
+ await MainActor.run { call.reject(error.localizedDescription) }
263
220
  }
264
- } else {
265
- print("Not implemented under iOS 15")
266
- call.reject("Not implemented under iOS 15")
267
221
  }
268
222
  }
269
223
 
270
224
  @objc func getPurchases(_ call: CAPPluginCall) {
225
+ print("getPurchases")
271
226
  let appAccountTokenFilter = call.getString("appAccountToken")
272
- if #available(iOS 15.0, *) {
273
- print("getPurchases")
274
- Task {
275
- let allPurchases = await TransactionHelpers.collectAllPurchases(appAccountTokenFilter: appAccountTokenFilter)
276
- await MainActor.run {
277
- call.resolve(["purchases": allPurchases])
278
- }
279
- }
280
- } else {
281
- print("Not implemented under iOS 15")
282
- call.reject("Not implemented under iOS 15")
227
+ Task {
228
+ let allPurchases = await TransactionHelpers.collectAllPurchases(
229
+ appAccountTokenFilter: appAccountTokenFilter
230
+ )
231
+ await MainActor.run { call.resolve(["purchases": allPurchases]) }
283
232
  }
284
233
  }
285
234
 
286
235
  @objc func manageSubscriptions(_ call: CAPPluginCall) {
287
- if #available(iOS 15.0, *) {
288
- print("manageSubscriptions")
289
- Task { @MainActor in
290
- do {
291
- // Get the current window scene
292
- guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
293
- call.reject("Unable to get window scene")
294
- return
295
- }
296
- // Open the App Store subscription management page
297
- try await AppStore.showManageSubscriptions(in: windowScene)
298
- call.resolve()
299
- } catch {
300
- print("manageSubscriptions error: \(error)")
301
- call.reject(error.localizedDescription)
236
+ print("manageSubscriptions")
237
+ Task { @MainActor in
238
+ do {
239
+ guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene else {
240
+ call.reject("Unable to get window scene")
241
+ return
302
242
  }
243
+ try await AppStore.showManageSubscriptions(in: windowScene)
244
+ call.resolve()
245
+ } catch {
246
+ print("manageSubscriptions error: \(error)")
247
+ call.reject(error.localizedDescription)
303
248
  }
304
- } else {
305
- print("Not implemented under iOS 15")
306
- call.reject("Not implemented under iOS 15")
307
249
  }
308
250
  }
309
251
 
310
252
  @objc func acknowledgePurchase(_ call: CAPPluginCall) {
311
- if #available(iOS 15.0, *) {
312
- print("acknowledgePurchase called on iOS")
313
-
314
- guard let purchaseToken = call.getString("purchaseToken") else {
315
- call.reject("purchaseToken is required")
316
- return
317
- }
253
+ print("acknowledgePurchase called on iOS")
318
254
 
319
- // On iOS, purchaseToken is the transactionId (UInt64 as string)
320
- guard let transactionId = UInt64(purchaseToken) else {
321
- call.reject("Invalid purchaseToken format")
322
- return
323
- }
255
+ guard let purchaseToken = call.getString("purchaseToken") else {
256
+ call.reject("purchaseToken is required")
257
+ return
258
+ }
324
259
 
325
- Task {
326
- // Search for the transaction in StoreKit's unfinished transactions
327
- // This works even after app restart because StoreKit persists them
328
- var foundTransaction: Transaction?
329
-
330
- for await verificationResult in Transaction.all {
331
- switch verificationResult {
332
- case .verified(let transaction):
333
- if transaction.id == transactionId {
334
- foundTransaction = transaction
335
- break
336
- }
337
- case .unverified:
338
- continue
339
- }
340
- if foundTransaction != nil {
341
- break
342
- }
343
- }
260
+ guard let transactionId = UInt64(purchaseToken) else {
261
+ call.reject("Invalid purchaseToken format")
262
+ return
263
+ }
344
264
 
345
- guard let transaction = foundTransaction else {
346
- await MainActor.run {
347
- call.reject("Transaction not found or already finished. Transaction ID: \(transactionId)")
348
- }
349
- return
265
+ Task {
266
+ var foundTransaction: Transaction?
267
+ for await verificationResult in Transaction.all {
268
+ if case .verified(let transaction) = verificationResult, transaction.id == transactionId {
269
+ foundTransaction = transaction
270
+ break
350
271
  }
272
+ }
351
273
 
352
- print("Manually finishing transaction: \(transaction.id)")
353
- await transaction.finish()
354
-
274
+ guard let transaction = foundTransaction else {
355
275
  await MainActor.run {
356
- print("Transaction finished successfully")
357
- call.resolve()
276
+ call.reject("Transaction not found or already finished. Transaction ID: \(transactionId)")
358
277
  }
278
+ return
279
+ }
280
+
281
+ print("Manually finishing transaction: \(transaction.id)")
282
+ await transaction.finish()
283
+ await MainActor.run {
284
+ print("Transaction finished successfully")
285
+ call.resolve()
359
286
  }
360
- } else {
361
- call.reject("Not implemented under iOS 15")
362
287
  }
363
288
  }
364
289
 
@@ -366,55 +291,16 @@ public class NativePurchasesPlugin: CAPPlugin, CAPBridgedPlugin {
366
291
  call.reject("consumePurchase is only available on Android")
367
292
  }
368
293
 
294
+ }
295
+
296
+ // MARK: - iOS 16+ App Transaction Methods
297
+ extension NativePurchasesPlugin {
369
298
  @objc func getAppTransaction(_ call: CAPPluginCall) {
370
299
  if #available(iOS 16.0, *) {
371
- print("getAppTransaction called on iOS")
372
300
  Task { @MainActor in
373
- do {
374
- let verificationResult = try await AppTransaction.shared
375
- switch verificationResult {
376
- case .verified(let appTransaction):
377
- var response: [String: Any] = [:]
378
-
379
- // originalAppVersion is the CFBundleVersion (build number) at the time of original download
380
- response["originalAppVersion"] = appTransaction.originalAppVersion
381
-
382
- // Original purchase date
383
- response["originalPurchaseDate"] = ISO8601DateFormatter().string(from: appTransaction.originalPurchaseDate)
384
-
385
- // Bundle ID
386
- response["bundleId"] = appTransaction.bundleID
387
-
388
- // Current app version (build number)
389
- response["appVersion"] = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? ""
390
-
391
- // Environment
392
- switch appTransaction.environment {
393
- case .sandbox:
394
- response["environment"] = "Sandbox"
395
- case .production:
396
- response["environment"] = "Production"
397
- case .xcode:
398
- response["environment"] = "Xcode"
399
- default:
400
- response["environment"] = "Production"
401
- }
402
-
403
- // JWS representation for server-side verification
404
- response["jwsRepresentation"] = verificationResult.jwsRepresentation
405
-
406
- call.resolve(["appTransaction": response])
407
-
408
- case .unverified(_, let error):
409
- call.reject("App transaction verification failed: \(error.localizedDescription)")
410
- }
411
- } catch {
412
- print("getAppTransaction error: \(error)")
413
- call.reject("Failed to get app transaction: \(error.localizedDescription)")
414
- }
301
+ await self.handleGetAppTransaction(call)
415
302
  }
416
303
  } else {
417
- print("getAppTransaction not implemented under iOS 16")
418
304
  call.reject("App Transaction requires iOS 16.0 or later")
419
305
  }
420
306
  }
@@ -426,44 +312,78 @@ public class NativePurchasesPlugin: CAPPlugin, CAPBridgedPlugin {
426
312
  }
427
313
 
428
314
  if #available(iOS 16.0, *) {
429
- print("isEntitledToOldBusinessModel called with targetBuildNumber: \(targetBuildNumber)")
430
315
  Task { @MainActor in
431
- do {
432
- let verificationResult = try await AppTransaction.shared
433
- switch verificationResult {
434
- case .verified(let appTransaction):
435
- let originalBuildNumber = appTransaction.originalAppVersion
436
-
437
- // Compare build numbers (integers)
438
- let isOlder = self.compareVersions(originalBuildNumber, targetBuildNumber) < 0
439
-
440
- call.resolve([
441
- "isOlderVersion": isOlder,
442
- "originalAppVersion": originalBuildNumber
443
- ])
444
-
445
- case .unverified(_, let error):
446
- call.reject("App transaction verification failed: \(error.localizedDescription)")
447
- }
448
- } catch {
449
- print("isEntitledToOldBusinessModel error: \(error)")
450
- call.reject("Failed to get app transaction: \(error.localizedDescription)")
451
- }
316
+ await self.handleIsEntitledToOldBusinessModel(call, targetBuildNumber: targetBuildNumber)
452
317
  }
453
318
  } else {
454
- print("isEntitledToOldBusinessModel not implemented under iOS 16")
455
319
  call.reject("App Transaction requires iOS 16.0 or later")
456
320
  }
457
321
  }
458
322
 
459
- // MARK: - Version Comparison Helper
323
+ @available(iOS 16.0, *)
324
+ @MainActor
325
+ private func handleGetAppTransaction(_ call: CAPPluginCall) async {
326
+ print("getAppTransaction called on iOS")
327
+ do {
328
+ let verificationResult = try await AppTransaction.shared
329
+ switch verificationResult {
330
+ case .verified(let appTransaction):
331
+ let response: [String: Any] = [
332
+ "originalAppVersion": appTransaction.originalAppVersion,
333
+ "originalPurchaseDate": ISO8601DateFormatter().string(
334
+ from: appTransaction.originalPurchaseDate
335
+ ),
336
+ "bundleId": appTransaction.bundleID,
337
+ "appVersion": Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "",
338
+ "jwsRepresentation": verificationResult.jwsRepresentation,
339
+ "environment": appTransaction.environment.environmentString
340
+ ]
341
+ call.resolve(["appTransaction": response])
342
+ case .unverified(_, let error):
343
+ call.reject("App transaction verification failed: \(error.localizedDescription)")
344
+ }
345
+ } catch {
346
+ print("getAppTransaction error: \(error)")
347
+ call.reject("Failed to get app transaction: \(error.localizedDescription)")
348
+ }
349
+ }
460
350
 
461
- /// Compares two build numbers as integers.
462
- /// Returns: negative if v1 < v2, zero if v1 == v2, positive if v1 > v2
463
- private func compareVersions(_ version1: String, _ version2: String) -> Int {
464
- let v1Int = Int(version1) ?? 0
465
- let v2Int = Int(version2) ?? 0
466
- return v1Int - v2Int
351
+ @available(iOS 16.0, *)
352
+ @MainActor
353
+ private func handleIsEntitledToOldBusinessModel(
354
+ _ call: CAPPluginCall,
355
+ targetBuildNumber: String
356
+ ) async {
357
+ print("isEntitledToOldBusinessModel called with targetBuildNumber: \(targetBuildNumber)")
358
+ do {
359
+ let verificationResult = try await AppTransaction.shared
360
+ switch verificationResult {
361
+ case .verified(let appTransaction):
362
+ let originalBuildNumber = appTransaction.originalAppVersion
363
+ let originalInt = Int(originalBuildNumber) ?? 0
364
+ let targetInt = Int(targetBuildNumber) ?? 0
365
+ call.resolve([
366
+ "isOlderVersion": originalInt < targetInt,
367
+ "originalAppVersion": originalBuildNumber
368
+ ])
369
+ case .unverified(_, let error):
370
+ call.reject("App transaction verification failed: \(error.localizedDescription)")
371
+ }
372
+ } catch {
373
+ print("isEntitledToOldBusinessModel error: \(error)")
374
+ call.reject("Failed to get app transaction: \(error.localizedDescription)")
375
+ }
467
376
  }
377
+ }
468
378
 
379
+ @available(iOS 16.0, *)
380
+ private extension AppStore.Environment {
381
+ var environmentString: String {
382
+ switch self {
383
+ case .sandbox: return "Sandbox"
384
+ case .production: return "Production"
385
+ case .xcode: return "Xcode"
386
+ default: return "Production"
387
+ }
388
+ }
469
389
  }