@capgo/native-purchases 8.3.7 → 8.3.9

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.
@@ -0,0 +1,374 @@
1
+ import Foundation
2
+ import StoreKit
3
+
4
+ // swiftlint:disable:next type_body_length
5
+ internal enum StoreKitPayloadHelpers {
6
+ static func jsonDictionary(from data: Data) -> [String: Any]? {
7
+ guard let value = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
8
+ return nil
9
+ }
10
+ return value
11
+ }
12
+
13
+ static func normalizedBillingPlanType(from value: Any?) -> String? {
14
+ guard let rawValue = value as? String else {
15
+ return nil
16
+ }
17
+
18
+ switch rawValue {
19
+ case "monthly", "MONTHLY":
20
+ return "monthly"
21
+ case "upFront", "UP_FRONT", "BILLED_UPFRONT":
22
+ return "upFront"
23
+ default:
24
+ return "unknown"
25
+ }
26
+ }
27
+
28
+ static func purchaseBillingPlanType(from value: String?) -> String? {
29
+ guard let value = value, !value.isEmpty else {
30
+ return nil
31
+ }
32
+
33
+ switch value {
34
+ case "monthly", "MONTHLY":
35
+ return "monthly"
36
+ case "upFront", "UP_FRONT", "BILLED_UPFRONT":
37
+ return "upFront"
38
+ default:
39
+ return nil
40
+ }
41
+ }
42
+
43
+ static func pricingTerms(from productJSON: [String: Any]) -> [[String: Any]]? {
44
+ guard let rawTerms = findArray(named: "pricingTerms", in: productJSON) else {
45
+ return nil
46
+ }
47
+
48
+ let terms = rawTerms.compactMap { $0 as? [String: Any] }.map(normalizePricingTerm)
49
+ return terms.isEmpty ? nil : terms
50
+ }
51
+
52
+ static func transactionCommitmentInfo(from value: Any?) -> [String: Any]? {
53
+ guard let rawInfo = value as? [String: Any] else {
54
+ return nil
55
+ }
56
+
57
+ var info: [String: Any] = [:]
58
+ copyNumber(named: "billingPeriodNumber", from: rawInfo, to: &info)
59
+ copyNumber(named: "totalBillingPeriods", from: rawInfo, to: &info)
60
+
61
+ if let expirationDate = isoDateString(fromMilliseconds: rawInfo["commitmentExpiresDate"] ?? rawInfo["expirationDate"]) {
62
+ info["expirationDate"] = expirationDate
63
+ }
64
+ if let price = decimalPrice(fromMilliunits: rawInfo["commitmentPrice"] ?? rawInfo["price"]) {
65
+ info["price"] = price
66
+ }
67
+
68
+ return info.isEmpty ? nil : info
69
+ }
70
+
71
+ static func renewalInfo(from value: Any?, fallbackWillAutoRenew: Bool?) -> [String: Any]? {
72
+ guard let rawInfo = value as? [String: Any] else {
73
+ if let fallbackWillAutoRenew = fallbackWillAutoRenew {
74
+ return ["willAutoRenew": fallbackWillAutoRenew]
75
+ }
76
+ return nil
77
+ }
78
+
79
+ var info: [String: Any] = [:]
80
+
81
+ if let fallbackWillAutoRenew = fallbackWillAutoRenew {
82
+ info["willAutoRenew"] = fallbackWillAutoRenew
83
+ } else if let autoRenewStatus = rawInfo["autoRenewStatus"] as? NSNumber {
84
+ info["willAutoRenew"] = autoRenewStatus.intValue == 1
85
+ }
86
+
87
+ if let autoRenewProductId = rawInfo["autoRenewProductId"] as? String {
88
+ info["autoRenewProductId"] = autoRenewProductId
89
+ }
90
+ if let renewalBillingPlanType = normalizedBillingPlanType(from: rawInfo["renewalBillingPlanType"]) {
91
+ info["renewalBillingPlanType"] = renewalBillingPlanType
92
+ }
93
+ if let renewalDate = isoDateString(fromMilliseconds: rawInfo["renewalDate"]) {
94
+ info["renewalDate"] = renewalDate
95
+ }
96
+ if let renewalPrice = decimalPrice(fromMilliunits: rawInfo["renewalPrice"]) {
97
+ info["renewalPrice"] = renewalPrice
98
+ }
99
+ if let currency = rawInfo["currency"] as? String {
100
+ info["currencyCode"] = currency
101
+ }
102
+ if let commitmentInfo = renewalCommitmentInfo(from: rawInfo["commitmentInfo"]) {
103
+ info["commitmentInfo"] = commitmentInfo
104
+ }
105
+
106
+ return info.isEmpty ? nil : info
107
+ }
108
+
109
+ static func periodDictionary(unit: Product.SubscriptionPeriod.Unit, value: Int) -> [String: Any] {
110
+ [
111
+ "numberOfUnits": value,
112
+ "unit": unit.integerValue,
113
+ "unitString": unit.stringValue
114
+ ]
115
+ }
116
+
117
+ static func subscriptionOfferDictionary(from offer: Product.SubscriptionOffer) -> [String: Any] {
118
+ var dictionary: [String: Any] = [
119
+ "type": offer.type.rawValue,
120
+ "price": double(from: offer.price),
121
+ "priceString": offer.displayPrice,
122
+ "period": offer.period.dictionary,
123
+ "periodCount": offer.periodCount,
124
+ "paymentMode": offer.paymentMode.rawValue
125
+ ]
126
+
127
+ if let id = offer.id {
128
+ dictionary["identifier"] = id
129
+ }
130
+
131
+ return dictionary
132
+ }
133
+
134
+ private static func normalizePricingTerm(_ rawTerm: [String: Any]) -> [String: Any] {
135
+ var term: [String: Any] = [:]
136
+
137
+ if let billingPlanType = normalizedBillingPlanType(from: rawTerm["billingPlanType"]) {
138
+ term["billingPlanType"] = billingPlanType
139
+ }
140
+ if let billingDisplayPrice = rawTerm["billingDisplayPrice"] as? String ?? rawTerm["displayPrice"] as? String {
141
+ term["billingDisplayPrice"] = billingDisplayPrice
142
+ }
143
+ if let billingPrice = number(from: rawTerm["billingPrice"] ?? rawTerm["price"]) {
144
+ term["billingPrice"] = billingPrice
145
+ }
146
+ if let billingPeriod = normalizePeriod(rawTerm["billingPeriod"] ?? rawTerm["period"]) {
147
+ term["billingPeriod"] = billingPeriod
148
+ }
149
+ if let commitmentInfo = productCommitmentInfo(from: rawTerm["commitmentInfo"]) {
150
+ term["commitmentInfo"] = commitmentInfo
151
+ }
152
+ if let rawOffers = rawTerm["subscriptionOffers"] as? [Any] {
153
+ let offers = rawOffers.compactMap { normalizeSubscriptionOffer($0) }
154
+ if !offers.isEmpty {
155
+ term["subscriptionOffers"] = offers
156
+ }
157
+ }
158
+
159
+ return term
160
+ }
161
+
162
+ private static func productCommitmentInfo(from value: Any?) -> [String: Any]? {
163
+ guard let rawInfo = value as? [String: Any] else {
164
+ return nil
165
+ }
166
+
167
+ var info: [String: Any] = [:]
168
+
169
+ if let displayPrice = rawInfo["displayPrice"] as? String ?? rawInfo["priceString"] as? String {
170
+ info["priceString"] = displayPrice
171
+ }
172
+ if let price = number(from: rawInfo["price"]) ?? decimalPrice(fromMilliunits: rawInfo["commitmentPrice"]) {
173
+ info["price"] = price
174
+ }
175
+ if let period = normalizePeriod(rawInfo["period"]) {
176
+ info["period"] = period
177
+ }
178
+
179
+ return info.isEmpty ? nil : info
180
+ }
181
+
182
+ private static func renewalCommitmentInfo(from value: Any?) -> [String: Any]? {
183
+ guard let rawInfo = value as? [String: Any] else {
184
+ return nil
185
+ }
186
+
187
+ var info: [String: Any] = [:]
188
+
189
+ if let autoRenewProductId = rawInfo["commitmentAutoRenewProductId"] as? String ?? rawInfo["autoRenewPreference"] as? String {
190
+ info["autoRenewProductId"] = autoRenewProductId
191
+ }
192
+ if let autoRenewStatus = rawInfo["commitmentAutoRenewStatus"] as? NSNumber {
193
+ info["willAutoRenew"] = autoRenewStatus.intValue == 1
194
+ } else if let willAutoRenew = rawInfo["willAutoRenew"] as? Bool {
195
+ info["willAutoRenew"] = willAutoRenew
196
+ }
197
+ if let billingPlanType = normalizedBillingPlanType(
198
+ from: rawInfo["commitmentRenewalBillingPlanType"] ?? rawInfo["renewalBillingPlanType"]
199
+ ) {
200
+ info["renewalBillingPlanType"] = billingPlanType
201
+ }
202
+ if let renewalDate = isoDateString(fromMilliseconds: rawInfo["commitmentRenewalDate"] ?? rawInfo["renewalDate"]) {
203
+ info["renewalDate"] = renewalDate
204
+ }
205
+ if let renewalPrice = decimalPrice(fromMilliunits: rawInfo["commitmentRenewalPrice"] ?? rawInfo["renewalPrice"]) {
206
+ info["renewalPrice"] = renewalPrice
207
+ }
208
+
209
+ return info.isEmpty ? nil : info
210
+ }
211
+
212
+ private static func normalizeSubscriptionOffer(_ value: Any) -> [String: Any]? {
213
+ guard let rawOffer = value as? [String: Any] else {
214
+ return nil
215
+ }
216
+
217
+ var offer: [String: Any] = [:]
218
+
219
+ if let identifier = rawOffer["id"] as? String ?? rawOffer["identifier"] as? String {
220
+ offer["identifier"] = identifier
221
+ }
222
+ if let type = rawOffer["type"] as? String {
223
+ offer["type"] = type
224
+ }
225
+ if let price = number(from: rawOffer["price"]) {
226
+ offer["price"] = price
227
+ }
228
+ if let priceString = rawOffer["displayPrice"] as? String ?? rawOffer["priceString"] as? String {
229
+ offer["priceString"] = priceString
230
+ }
231
+ if let period = normalizePeriod(rawOffer["period"]) {
232
+ offer["period"] = period
233
+ }
234
+ if let periodCount = rawOffer["periodCount"] as? NSNumber {
235
+ offer["periodCount"] = periodCount.intValue
236
+ }
237
+ if let paymentMode = rawOffer["paymentMode"] as? String {
238
+ offer["paymentMode"] = paymentMode
239
+ }
240
+
241
+ return offer.isEmpty ? nil : offer
242
+ }
243
+
244
+ private static func normalizePeriod(_ value: Any?) -> [String: Any]? {
245
+ if let rawPeriod = value as? [String: Any] {
246
+ var period: [String: Any] = [:]
247
+
248
+ if let numberOfUnits = rawPeriod["numberOfUnits"] as? NSNumber ?? rawPeriod["value"] as? NSNumber {
249
+ period["numberOfUnits"] = numberOfUnits.intValue
250
+ }
251
+ if let unit = rawPeriod["unit"] as? NSNumber {
252
+ period["unit"] = unit.intValue
253
+ }
254
+ if let unitString = rawPeriod["unitString"] as? String ?? rawPeriod["unit"] as? String {
255
+ period["unitString"] = unitString
256
+ period["unit"] = unitInteger(from: unitString)
257
+ }
258
+
259
+ return period.isEmpty ? nil : period
260
+ }
261
+
262
+ guard let isoPeriod = value as? String else {
263
+ return nil
264
+ }
265
+
266
+ return periodDictionary(fromISO8601: isoPeriod)
267
+ }
268
+
269
+ private static func periodDictionary(fromISO8601 value: String) -> [String: Any]? {
270
+ let pattern = #"^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?$"#
271
+ guard let regex = try? NSRegularExpression(pattern: pattern),
272
+ let match = regex.firstMatch(in: value, range: NSRange(value.startIndex..., in: value)) else {
273
+ return nil
274
+ }
275
+
276
+ let captures = ["year", "month", "week", "day"]
277
+ var foundPeriod: [String: Any]?
278
+
279
+ for index in 1...4 {
280
+ let range = match.range(at: index)
281
+ guard range.location != NSNotFound,
282
+ let swiftRange = Range(range, in: value),
283
+ let numberOfUnits = Int(value[swiftRange]) else {
284
+ continue
285
+ }
286
+
287
+ let unitString = captures[index - 1]
288
+ guard foundPeriod == nil else {
289
+ return nil
290
+ }
291
+
292
+ foundPeriod = [
293
+ "numberOfUnits": numberOfUnits,
294
+ "unit": unitInteger(from: unitString),
295
+ "unitString": unitString
296
+ ]
297
+ }
298
+
299
+ return foundPeriod
300
+ }
301
+
302
+ private static func findArray(named name: String, in value: Any) -> [Any]? {
303
+ if let dictionary = value as? [String: Any] {
304
+ if let array = dictionary[name] as? [Any] {
305
+ return array
306
+ }
307
+
308
+ for childValue in dictionary.values {
309
+ if let array = findArray(named: name, in: childValue) {
310
+ return array
311
+ }
312
+ }
313
+ } else if let array = value as? [Any] {
314
+ for childValue in array {
315
+ if let foundArray = findArray(named: name, in: childValue) {
316
+ return foundArray
317
+ }
318
+ }
319
+ }
320
+
321
+ return nil
322
+ }
323
+
324
+ private static func copyNumber(named name: String, from source: [String: Any], to destination: inout [String: Any]) {
325
+ if let value = source[name] as? NSNumber {
326
+ destination[name] = value.intValue
327
+ }
328
+ }
329
+
330
+ private static func number(from value: Any?) -> Double? {
331
+ if let value = value as? NSNumber {
332
+ return value.doubleValue
333
+ }
334
+ if let value = value as? String {
335
+ return Double(value)
336
+ }
337
+ return nil
338
+ }
339
+
340
+ private static func decimalPrice(fromMilliunits value: Any?) -> Double? {
341
+ guard let number = number(from: value) else {
342
+ return nil
343
+ }
344
+ return number / 1000
345
+ }
346
+
347
+ private static func isoDateString(fromMilliseconds value: Any?) -> String? {
348
+ guard let milliseconds = number(from: value) else {
349
+ return nil
350
+ }
351
+
352
+ let date = Date(timeIntervalSince1970: milliseconds / 1000)
353
+ return ISO8601DateFormatter().string(from: date)
354
+ }
355
+
356
+ private static func double(from decimal: Decimal) -> Double {
357
+ NSDecimalNumber(decimal: decimal).doubleValue
358
+ }
359
+
360
+ private static func unitInteger(from value: String) -> Int {
361
+ switch value {
362
+ case "day", "DAY", "days":
363
+ return 0
364
+ case "week", "WEEK", "weeks":
365
+ return 1
366
+ case "month", "MONTH", "months":
367
+ return 2
368
+ case "year", "YEAR", "years":
369
+ return 3
370
+ default:
371
+ return -1
372
+ }
373
+ }
374
+ }
@@ -56,6 +56,37 @@ internal class TransactionHelpers {
56
56
  if let token = transaction.appAccountToken {
57
57
  response["appAccountToken"] = token.uuidString
58
58
  }
59
+
60
+ addStoreKitJSONDetails(to: &response, transaction: transaction)
61
+ }
62
+
63
+ private static func addStoreKitJSONDetails(to response: inout [String: Any], transaction: Transaction) {
64
+ if let transactionJSON = StoreKitPayloadHelpers.jsonDictionary(from: transaction.jsonRepresentation) {
65
+ if let billingPlanType = StoreKitPayloadHelpers.normalizedBillingPlanType(
66
+ from: transactionJSON["billingPlanType"]
67
+ ) {
68
+ response["billingPlanType"] = billingPlanType
69
+ }
70
+ if let commitmentInfo = StoreKitPayloadHelpers.transactionCommitmentInfo(
71
+ from: transactionJSON["commitmentInfo"]
72
+ ) {
73
+ response["commitmentInfo"] = commitmentInfo
74
+ }
75
+ if let currency = transactionJSON["currency"] as? String {
76
+ response["currencyCode"] = currency
77
+ }
78
+ }
79
+
80
+ #if STOREKIT_26_5
81
+ if #available(iOS 26.4, *) {
82
+ if let billingPlanType = transaction.billingPlanType {
83
+ response["billingPlanType"] = billingPlanType.capacitorString
84
+ }
85
+ if let commitmentInfo = transaction.commitmentInfo {
86
+ response["commitmentInfo"] = commitmentInfo.dictionary
87
+ }
88
+ }
89
+ #endif
59
90
  }
60
91
 
61
92
  static func getReceiptData() -> String? {
@@ -88,6 +119,7 @@ internal class TransactionHelpers {
88
119
  if subscriptionStatus.state == .subscribed {
89
120
  if case .verified(let value) = subscriptionStatus.renewalInfo {
90
121
  response["willCancel"] = !value.willAutoRenew
122
+ response["renewalInfo"] = renewalInfoDictionary(from: value)
91
123
  } else {
92
124
  response["willCancel"] = NSNull()
93
125
  }
@@ -96,6 +128,24 @@ internal class TransactionHelpers {
96
128
  }
97
129
  }
98
130
 
131
+ private static func renewalInfoDictionary(
132
+ from renewalInfo: Product.SubscriptionInfo.RenewalInfo
133
+ ) -> [String: Any] {
134
+ let renewalJSON = StoreKitPayloadHelpers.jsonDictionary(from: renewalInfo.jsonRepresentation)
135
+ var dictionary = StoreKitPayloadHelpers.renewalInfo(
136
+ from: renewalJSON,
137
+ fallbackWillAutoRenew: renewalInfo.willAutoRenew
138
+ ) ?? ["willAutoRenew": renewalInfo.willAutoRenew]
139
+
140
+ #if STOREKIT_26_5
141
+ if #available(iOS 26.4, *), let commitmentInfo = renewalInfo.commitmentInfo {
142
+ dictionary["commitmentInfo"] = commitmentInfo.dictionary
143
+ }
144
+ #endif
145
+
146
+ return dictionary
147
+ }
148
+
99
149
  static func collectAllPurchases(appAccountTokenFilter: String?, onlyCurrentEntitlements: Bool = false) async throws -> [[String: Any]] {
100
150
  var allPurchases: [[String: Any]] = []
101
151
  if onlyCurrentEntitlements {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/native-purchases",
3
- "version": "8.3.7",
3
+ "version": "8.3.9",
4
4
  "description": "In-app Subscriptions Made Easy",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",