@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.
- package/README.md +333 -48
- package/android/build.gradle +2 -2
- package/android/src/main/java/ee/forgr/nativepurchases/NativePurchasesPlugin.java +199 -73
- package/dist/docs.json +60 -4
- package/dist/esm/definitions.d.ts +118 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +4 -0
- package/dist/esm/web.js +4 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +4 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +4 -0
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/NativePurchasesPlugin/NativePurchasesPlugin.swift +224 -304
- package/ios/Sources/NativePurchasesPlugin/Product+CapacitorPurchasesPlugin.swift +0 -1
- package/ios/Sources/NativePurchasesPlugin/TransactionHelpers.swift +85 -129
- package/package.json +1 -1
|
@@ -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.
|
|
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
|
-
|
|
38
|
-
if #available(iOS 15.0, *) {
|
|
39
|
-
startTransactionUpdatesListener()
|
|
40
|
-
}
|
|
34
|
+
startTransactionUpdatesListener()
|
|
41
35
|
}
|
|
42
36
|
|
|
43
37
|
deinit {
|
|
44
|
-
|
|
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
|
-
|
|
55
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
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
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
let
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
123
|
-
let result = try await product.purchase(options: purchaseOptions)
|
|
124
|
-
print("purchaseProduct result \(result)")
|
|
106
|
+
print("Auto-acknowledge enabled: \(autoAcknowledge)")
|
|
125
107
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
144
|
-
|
|
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(
|
|
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(
|
|
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
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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
|
-
|
|
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
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
255
|
+
guard let purchaseToken = call.getString("purchaseToken") else {
|
|
256
|
+
call.reject("purchaseToken is required")
|
|
257
|
+
return
|
|
258
|
+
}
|
|
324
259
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
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
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
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
|
-
|
|
353
|
-
await transaction.finish()
|
|
354
|
-
|
|
274
|
+
guard let transaction = foundTransaction else {
|
|
355
275
|
await MainActor.run {
|
|
356
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
462
|
-
|
|
463
|
-
private func
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
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
|
}
|