@app-swiss-knife/react-native-sdk 0.1.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.
Files changed (218) hide show
  1. package/README.md +182 -0
  2. package/SwissKnifePurchases.podspec +40 -0
  3. package/android/build.gradle +89 -0
  4. package/android/gradle.properties +3 -0
  5. package/android/src/main/AndroidManifest.xml +26 -0
  6. package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingModule.kt +216 -0
  7. package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingPackage.kt +17 -0
  8. package/android/src/main/java/com/swissknife/purchases/Conversions.kt +15 -0
  9. package/android/src/main/java/com/swissknife/purchases/SwissKnifeJson.kt +371 -0
  10. package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +497 -0
  11. package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesPackage.kt +25 -0
  12. package/android/vendor/GENERATED.md +10 -0
  13. package/android/vendor/java/com/appswissknife/SwissKnife.kt +533 -0
  14. package/android/vendor/java/com/appswissknife/analytics/EventQueueStore.kt +326 -0
  15. package/android/vendor/java/com/appswissknife/analytics/FlushEventsWorker.kt +296 -0
  16. package/android/vendor/java/com/appswissknife/analytics/SKAnalytics.kt +503 -0
  17. package/android/vendor/java/com/appswissknife/attributes/AttributesClient.kt +188 -0
  18. package/android/vendor/java/com/appswissknife/attributes/SetAttributesWorker.kt +204 -0
  19. package/android/vendor/java/com/appswissknife/attribution/Attribution.kt +447 -0
  20. package/android/vendor/java/com/appswissknife/context/AppLifecycle.kt +120 -0
  21. package/android/vendor/java/com/appswissknife/context/Clock.kt +70 -0
  22. package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +138 -0
  23. package/android/vendor/java/com/appswissknife/context/IDs.kt +55 -0
  24. package/android/vendor/java/com/appswissknife/context/Reachability.kt +145 -0
  25. package/android/vendor/java/com/appswissknife/context/Session.kt +192 -0
  26. package/android/vendor/java/com/appswissknife/crashreporting/SKCrashReporting.kt +945 -0
  27. package/android/vendor/java/com/appswissknife/crashreporting/SKScreenshot.kt +201 -0
  28. package/android/vendor/java/com/appswissknife/device/DeviceID.kt +172 -0
  29. package/android/vendor/java/com/appswissknife/diagnostics/SKLog.kt +97 -0
  30. package/android/vendor/java/com/appswissknife/entitlements/EntitlementsRepository.kt +495 -0
  31. package/android/vendor/java/com/appswissknife/identity/Identity.kt +522 -0
  32. package/android/vendor/java/com/appswissknife/models/CustomerInfo.kt +67 -0
  33. package/android/vendor/java/com/appswissknife/models/Enums.kt +35 -0
  34. package/android/vendor/java/com/appswissknife/models/Offerings.kt +80 -0
  35. package/android/vendor/java/com/appswissknife/models/PurchasesError.kt +89 -0
  36. package/android/vendor/java/com/appswissknife/models/StoreTransaction.kt +17 -0
  37. package/android/vendor/java/com/appswissknife/net/BackendHttpClient.kt +507 -0
  38. package/android/vendor/java/com/appswissknife/net/ExponentialBackoff.kt +69 -0
  39. package/android/vendor/java/com/appswissknife/net/IngestClient.kt +582 -0
  40. package/android/vendor/java/com/appswissknife/net/OfflineQueueStore.kt +320 -0
  41. package/android/vendor/java/com/appswissknife/offerings/OfferingsRepository.kt +219 -0
  42. package/android/vendor/java/com/appswissknife/performance/SKFrameTracker.kt +94 -0
  43. package/android/vendor/java/com/appswissknife/performance/SKPerformance.kt +428 -0
  44. package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +560 -0
  45. package/android/vendor/java/com/appswissknife/purchases/PlayPriceEnrichmentProvider.kt +100 -0
  46. package/android/vendor/java/com/appswissknife/purchases/PriceEnrichmentProvider.kt +36 -0
  47. package/android/vendor/java/com/appswissknife/purchases/PurchasesGateway.kt +120 -0
  48. package/android/vendor/res/xml/swissknife_data_extraction_rules.xml +20 -0
  49. package/ios/SwissKnifeCrashReporting.m +32 -0
  50. package/ios/SwissKnifeCrashReporting.swift +186 -0
  51. package/ios/SwissKnifeJSON.swift +347 -0
  52. package/ios/SwissKnifePurchases.m +124 -0
  53. package/ios/SwissKnifePurchases.swift +523 -0
  54. package/ios/vendor/GENERATED.md +10 -0
  55. package/ios/vendor/SwissKnifeiOS/Analytics/SKAnalytics.swift +488 -0
  56. package/ios/vendor/SwissKnifeiOS/AttributesClient.swift +460 -0
  57. package/ios/vendor/SwissKnifeiOS/Attribution/Attribution.swift +307 -0
  58. package/ios/vendor/SwissKnifeiOS/Attribution/AttributionNetwork.swift +92 -0
  59. package/ios/vendor/SwissKnifeiOS/Attribution/AttributionState.swift +141 -0
  60. package/ios/vendor/SwissKnifeiOS/Context/AppLifecycle.swift +130 -0
  61. package/ios/vendor/SwissKnifeiOS/Context/Clock.swift +100 -0
  62. package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +190 -0
  63. package/ios/vendor/SwissKnifeiOS/Context/IDs.swift +64 -0
  64. package/ios/vendor/SwissKnifeiOS/Context/Reachability.swift +121 -0
  65. package/ios/vendor/SwissKnifeiOS/Context/Session.swift +223 -0
  66. package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +1287 -0
  67. package/ios/vendor/SwissKnifeiOS/CrashReporting/SKScreenshot.swift +175 -0
  68. package/ios/vendor/SwissKnifeiOS/DeviceID.swift +161 -0
  69. package/ios/vendor/SwissKnifeiOS/Diagnostics/SKLog.swift +128 -0
  70. package/ios/vendor/SwissKnifeiOS/Identity/Identity.swift +323 -0
  71. package/ios/vendor/SwissKnifeiOS/Identity/IdentityNetwork.swift +70 -0
  72. package/ios/vendor/SwissKnifeiOS/Net/BackendHttpClient.swift +329 -0
  73. package/ios/vendor/SwissKnifeiOS/Net/ExponentialBackoff.swift +87 -0
  74. package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +681 -0
  75. package/ios/vendor/SwissKnifeiOS/Net/OfflineQueueStore.swift +409 -0
  76. package/ios/vendor/SwissKnifeiOS/Performance/SKFrameTracker.swift +89 -0
  77. package/ios/vendor/SwissKnifeiOS/Performance/SKPerformance.swift +440 -0
  78. package/ios/vendor/SwissKnifeiOS/Performance/SKURLSessionDelegate.swift +87 -0
  79. package/ios/vendor/SwissKnifeiOS/Services/EntitlementsService.swift +646 -0
  80. package/ios/vendor/SwissKnifeiOS/Services/OfferingsService.swift +458 -0
  81. package/ios/vendor/SwissKnifeiOS/Services/PurchaseService.swift +374 -0
  82. package/ios/vendor/SwissKnifeiOS/SwissKnife+Analytics.swift +46 -0
  83. package/ios/vendor/SwissKnifeiOS/SwissKnife+DeviceID.swift +30 -0
  84. package/ios/vendor/SwissKnifeiOS/SwissKnife+Session.swift +47 -0
  85. package/ios/vendor/SwissKnifeiOS/SwissKnife.swift +417 -0
  86. package/ios/vendor/SwissKnifeiOS/Types.swift +496 -0
  87. package/lib/commonjs/attribution.js +55 -0
  88. package/lib/commonjs/attribution.js.map +1 -0
  89. package/lib/commonjs/identity.js +126 -0
  90. package/lib/commonjs/identity.js.map +1 -0
  91. package/lib/commonjs/index.js +118 -0
  92. package/lib/commonjs/index.js.map +1 -0
  93. package/lib/commonjs/modules/analytics.js +646 -0
  94. package/lib/commonjs/modules/analytics.js.map +1 -0
  95. package/lib/commonjs/modules/attachments.js +169 -0
  96. package/lib/commonjs/modules/attachments.js.map +1 -0
  97. package/lib/commonjs/modules/crash-reporting.js +1327 -0
  98. package/lib/commonjs/modules/crash-reporting.js.map +1 -0
  99. package/lib/commonjs/modules/ids.js +50 -0
  100. package/lib/commonjs/modules/ids.js.map +1 -0
  101. package/lib/commonjs/modules/log.js +59 -0
  102. package/lib/commonjs/modules/log.js.map +1 -0
  103. package/lib/commonjs/modules/offline-store.js +44 -0
  104. package/lib/commonjs/modules/offline-store.js.map +1 -0
  105. package/lib/commonjs/modules/performance.js +494 -0
  106. package/lib/commonjs/modules/performance.js.map +1 -0
  107. package/lib/commonjs/native/module.js +41 -0
  108. package/lib/commonjs/native/module.js.map +1 -0
  109. package/lib/commonjs/native/spec.js +26 -0
  110. package/lib/commonjs/native/spec.js.map +1 -0
  111. package/lib/commonjs/package.json +1 -0
  112. package/lib/commonjs/purchases.js +194 -0
  113. package/lib/commonjs/purchases.js.map +1 -0
  114. package/lib/commonjs/types.js +236 -0
  115. package/lib/commonjs/types.js.map +1 -0
  116. package/lib/module/attribution.js +51 -0
  117. package/lib/module/attribution.js.map +1 -0
  118. package/lib/module/identity.js +122 -0
  119. package/lib/module/identity.js.map +1 -0
  120. package/lib/module/index.js +36 -0
  121. package/lib/module/index.js.map +1 -0
  122. package/lib/module/modules/analytics.js +642 -0
  123. package/lib/module/modules/analytics.js.map +1 -0
  124. package/lib/module/modules/attachments.js +162 -0
  125. package/lib/module/modules/attachments.js.map +1 -0
  126. package/lib/module/modules/crash-reporting.js +1320 -0
  127. package/lib/module/modules/crash-reporting.js.map +1 -0
  128. package/lib/module/modules/ids.js +44 -0
  129. package/lib/module/modules/ids.js.map +1 -0
  130. package/lib/module/modules/log.js +54 -0
  131. package/lib/module/modules/log.js.map +1 -0
  132. package/lib/module/modules/offline-store.js +39 -0
  133. package/lib/module/modules/offline-store.js.map +1 -0
  134. package/lib/module/modules/performance.js +491 -0
  135. package/lib/module/modules/performance.js.map +1 -0
  136. package/lib/module/native/module.js +34 -0
  137. package/lib/module/native/module.js.map +1 -0
  138. package/lib/module/native/spec.js +22 -0
  139. package/lib/module/native/spec.js.map +1 -0
  140. package/lib/module/package.json +1 -0
  141. package/lib/module/purchases.js +187 -0
  142. package/lib/module/purchases.js.map +1 -0
  143. package/lib/module/types.js +230 -0
  144. package/lib/module/types.js.map +1 -0
  145. package/lib/typescript/commonjs/attribution.d.ts +24 -0
  146. package/lib/typescript/commonjs/attribution.d.ts.map +1 -0
  147. package/lib/typescript/commonjs/identity.d.ts +37 -0
  148. package/lib/typescript/commonjs/identity.d.ts.map +1 -0
  149. package/lib/typescript/commonjs/index.d.ts +15 -0
  150. package/lib/typescript/commonjs/index.d.ts.map +1 -0
  151. package/lib/typescript/commonjs/modules/analytics.d.ts +127 -0
  152. package/lib/typescript/commonjs/modules/analytics.d.ts.map +1 -0
  153. package/lib/typescript/commonjs/modules/attachments.d.ts +50 -0
  154. package/lib/typescript/commonjs/modules/attachments.d.ts.map +1 -0
  155. package/lib/typescript/commonjs/modules/crash-reporting.d.ts +175 -0
  156. package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -0
  157. package/lib/typescript/commonjs/modules/ids.d.ts +17 -0
  158. package/lib/typescript/commonjs/modules/ids.d.ts.map +1 -0
  159. package/lib/typescript/commonjs/modules/log.d.ts +12 -0
  160. package/lib/typescript/commonjs/modules/log.d.ts.map +1 -0
  161. package/lib/typescript/commonjs/modules/offline-store.d.ts +9 -0
  162. package/lib/typescript/commonjs/modules/offline-store.d.ts.map +1 -0
  163. package/lib/typescript/commonjs/modules/performance.d.ts +75 -0
  164. package/lib/typescript/commonjs/modules/performance.d.ts.map +1 -0
  165. package/lib/typescript/commonjs/native/module.d.ts +6 -0
  166. package/lib/typescript/commonjs/native/module.d.ts.map +1 -0
  167. package/lib/typescript/commonjs/native/spec.d.ts +119 -0
  168. package/lib/typescript/commonjs/native/spec.d.ts.map +1 -0
  169. package/lib/typescript/commonjs/package.json +1 -0
  170. package/lib/typescript/commonjs/purchases.d.ts +68 -0
  171. package/lib/typescript/commonjs/purchases.d.ts.map +1 -0
  172. package/lib/typescript/commonjs/types.d.ts +384 -0
  173. package/lib/typescript/commonjs/types.d.ts.map +1 -0
  174. package/lib/typescript/module/attribution.d.ts +24 -0
  175. package/lib/typescript/module/attribution.d.ts.map +1 -0
  176. package/lib/typescript/module/identity.d.ts +37 -0
  177. package/lib/typescript/module/identity.d.ts.map +1 -0
  178. package/lib/typescript/module/index.d.ts +15 -0
  179. package/lib/typescript/module/index.d.ts.map +1 -0
  180. package/lib/typescript/module/modules/analytics.d.ts +127 -0
  181. package/lib/typescript/module/modules/analytics.d.ts.map +1 -0
  182. package/lib/typescript/module/modules/attachments.d.ts +50 -0
  183. package/lib/typescript/module/modules/attachments.d.ts.map +1 -0
  184. package/lib/typescript/module/modules/crash-reporting.d.ts +175 -0
  185. package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -0
  186. package/lib/typescript/module/modules/ids.d.ts +17 -0
  187. package/lib/typescript/module/modules/ids.d.ts.map +1 -0
  188. package/lib/typescript/module/modules/log.d.ts +12 -0
  189. package/lib/typescript/module/modules/log.d.ts.map +1 -0
  190. package/lib/typescript/module/modules/offline-store.d.ts +9 -0
  191. package/lib/typescript/module/modules/offline-store.d.ts.map +1 -0
  192. package/lib/typescript/module/modules/performance.d.ts +75 -0
  193. package/lib/typescript/module/modules/performance.d.ts.map +1 -0
  194. package/lib/typescript/module/native/module.d.ts +6 -0
  195. package/lib/typescript/module/native/module.d.ts.map +1 -0
  196. package/lib/typescript/module/native/spec.d.ts +119 -0
  197. package/lib/typescript/module/native/spec.d.ts.map +1 -0
  198. package/lib/typescript/module/package.json +1 -0
  199. package/lib/typescript/module/purchases.d.ts +68 -0
  200. package/lib/typescript/module/purchases.d.ts.map +1 -0
  201. package/lib/typescript/module/types.d.ts +384 -0
  202. package/lib/typescript/module/types.d.ts.map +1 -0
  203. package/package.json +115 -0
  204. package/react-native.config.js +23 -0
  205. package/src/attribution.ts +56 -0
  206. package/src/identity.ts +133 -0
  207. package/src/index.ts +54 -0
  208. package/src/modules/analytics.ts +689 -0
  209. package/src/modules/attachments.ts +222 -0
  210. package/src/modules/crash-reporting.ts +1577 -0
  211. package/src/modules/ids.ts +41 -0
  212. package/src/modules/log.ts +56 -0
  213. package/src/modules/offline-store.ts +51 -0
  214. package/src/modules/performance.ts +589 -0
  215. package/src/native/module.ts +47 -0
  216. package/src/native/spec.ts +183 -0
  217. package/src/purchases.ts +214 -0
  218. package/src/types.ts +470 -0
@@ -0,0 +1,347 @@
1
+ // packages/react-native-sdk/ios/SwissKnifeJSON.swift
2
+ //
3
+ // Phase 7 — Swift-to-JSON encoder for the RN bridge. The RN bridge marshals
4
+ // `[String: Any]` / arrays / scalars across to JS via the standard RCT plumbing
5
+ // (NSDictionary → NSObject graph → JS object). We convert our own
6
+ // `CustomerInfo` / `Offerings` / etc. structs (defined in
7
+ // `packages/swiss-knife-ios/Sources/SwissKnifeiOS/Types.swift`) into that wire
8
+ // shape here, matching the TypeScript types in
9
+ // `packages/react-native-sdk/src/types.ts`.
10
+ //
11
+ // Why hand-rolled, not `JSONEncoder`?
12
+ // - The TS types use ISO-8601 strings for Date and parallel "*Millis" Number
13
+ // fields; standard Codable would only give us one.
14
+ // - The TS types are flat-ish `[String: Any]` dictionaries — RN's bridge
15
+ // hands these straight to JS without an intermediate JSON parse step.
16
+ // - We need to be lenient about partially-populated values (e.g. an empty
17
+ // synthesized `StoreProduct` used for purchaseStoreProduct path).
18
+ //
19
+ // Field-by-field parity with src/types.ts is load-bearing: a typo here means
20
+ // JS reads `undefined` where it expects a string.
21
+
22
+ import Foundation
23
+ // See SwissKnifePurchases.swift — vendored into this module by the pod, imported
24
+ // as a module by native/SPM consumers.
25
+ #if canImport(SwissKnifeiOS)
26
+ import SwissKnifeiOS
27
+ #endif
28
+
29
+ enum SwissKnifeJSON {
30
+
31
+ // MARK: - Top-level encoders
32
+
33
+ static func encode(customerInfo info: CustomerInfo) -> [String: Any] {
34
+ let activeProductIds = Array(Set(info.entitlements.active.values.map { $0.productIdentifier }))
35
+ return [
36
+ "entitlements": encode(entitlements: info.entitlements),
37
+ "activeSubscriptions": activeProductIds,
38
+ "allPurchasedProductIdentifiers": info.allPurchasedProductIdentifiers,
39
+ "allPurchaseDates": encode(datesByKey: info.allPurchaseDates),
40
+ "subscriptionsByProductIdentifier": [String: Any](), // populated by future phases
41
+ "nonSubscriptionTransactions": [Any](), // populated by future phases
42
+ "firstSeen": iso8601(info.firstSeen),
43
+ "firstSeenMillis": millis(info.firstSeen),
44
+ "originalAppUserId": info.originalAppUserId,
45
+ "requestDate": iso8601(Date()),
46
+ "requestDateMillis": millis(Date()),
47
+ "latestExpirationDate": optionalISO(info.latestExpirationDate),
48
+ "latestExpirationDateMillis": optionalMillis(info.latestExpirationDate),
49
+ "originalPurchaseDate": optionalISO(info.originalPurchaseDate),
50
+ "originalPurchaseDateMillis": optionalMillis(info.originalPurchaseDate),
51
+ "originalApplicationVersion": NSNull(), // not surfaced by our backend yet
52
+ "managementURL": optionalString(info.managementURL?.absoluteString),
53
+ ]
54
+ }
55
+
56
+ static func encode(offerings: Offerings) -> [String: Any] {
57
+ return [
58
+ "all": offerings.all.mapValues { encode(offering: $0) },
59
+ "current": offerings.current.map { encode(offering: $0) } ?? NSNull(),
60
+ ]
61
+ }
62
+
63
+ static func encode(offering: Offering) -> [String: Any] {
64
+ let packages = offering.availablePackages.map { encode(package: $0) }
65
+ return [
66
+ "identifier": offering.identifier,
67
+ "serverDescription": offering.serverDescription,
68
+ "metadata": offering.metadata,
69
+ "availablePackages": packages,
70
+ "lifetime": optionalPackage(offering.lifetime),
71
+ "annual": optionalPackage(offering.annual),
72
+ "sixMonth": optionalPackage(offering.sixMonth),
73
+ "threeMonth": optionalPackage(offering.threeMonth),
74
+ "twoMonth": optionalPackage(offering.twoMonth),
75
+ "monthly": optionalPackage(offering.monthly),
76
+ "weekly": optionalPackage(offering.weekly),
77
+ "paywall": NSNull(),
78
+ "paywallComponents": NSNull(),
79
+ ]
80
+ }
81
+
82
+ static func encode(package: Package) -> [String: Any] {
83
+ let context: [String: Any] = [
84
+ "offeringIdentifier": package.offeringIdentifier,
85
+ "placementIdentifier": NSNull(),
86
+ "targetingContext": NSNull(),
87
+ ]
88
+ return [
89
+ "identifier": package.identifier,
90
+ "packageType": packageTypeString(package.packageType),
91
+ "product": encode(product: package.storeProduct),
92
+ "presentedOfferingContext": context,
93
+ "offeringIdentifier": package.offeringIdentifier,
94
+ ]
95
+ }
96
+
97
+ static func encode(product: StoreProduct) -> [String: Any] {
98
+ return [
99
+ "identifier": product.productIdentifier,
100
+ "title": product.localizedTitle,
101
+ "description": product.localizedDescription,
102
+ "priceString": product.displayPrice,
103
+ "price": NSDecimalNumber(decimal: product.price).doubleValue,
104
+ "currencyCode": product.currencyCode,
105
+ "productCategory": "SUBSCRIPTION",
106
+ "productType": "AUTO_RENEWABLE_SUBSCRIPTION",
107
+ "subscriptionPeriod": product.subscriptionPeriod.map { periodString($0) } ?? NSNull(),
108
+ "defaultOption": NSNull(),
109
+ "subscriptionOptions": NSNull(),
110
+ "presentedOfferingContext": NSNull(),
111
+ "introPrice": product.introductoryDiscount.map { encode(discount: $0) } ?? NSNull(),
112
+ "discounts": product.discounts.map { encode(discount: $0) },
113
+ "pricePerWeek": NSNull(),
114
+ "pricePerMonth": NSNull(),
115
+ "pricePerYear": NSNull(),
116
+ "pricePerWeekString": NSNull(),
117
+ "pricePerMonthString": NSNull(),
118
+ "pricePerYearString": NSNull(),
119
+ ]
120
+ }
121
+
122
+ static func encode(purchaseResult result: SwissKnife.PurchaseResult,
123
+ productIdentifier: String) -> [String: Any] {
124
+ return [
125
+ "productIdentifier": productIdentifier,
126
+ "customerInfo": encode(customerInfo: result.customerInfo),
127
+ "transaction": result.transaction.map { encode(transaction: $0) } ?? NSNull(),
128
+ ]
129
+ }
130
+
131
+ // MARK: - Sub-encoders
132
+
133
+ private static func encode(entitlements infos: EntitlementInfos) -> [String: Any] {
134
+ return [
135
+ "all": infos.all.mapValues { encode(entitlement: $0) },
136
+ "active": infos.active.mapValues { encode(entitlement: $0) },
137
+ "verification": verificationString(infos.verification),
138
+ ]
139
+ }
140
+
141
+ private static func encode(entitlement info: EntitlementInfo) -> [String: Any] {
142
+ return [
143
+ "identifier": info.identifier,
144
+ "isActive": info.isActive,
145
+ "willRenew": info.willRenew,
146
+ "latestPurchaseDate": iso8601(info.latestPurchaseDate),
147
+ "latestPurchaseDateMillis": millis(info.latestPurchaseDate),
148
+ "originalPurchaseDate": optionalISO(info.latestPurchaseDate),
149
+ "originalPurchaseDateMillis": optionalMillis(info.latestPurchaseDate),
150
+ "expirationDate": optionalISO(info.expirationDate),
151
+ "expirationDateMillis": optionalMillis(info.expirationDate),
152
+ "productIdentifier": info.productIdentifier,
153
+ "productPlanIdentifier": optionalString(info.productPlanIdentifier),
154
+ "isSandbox": info.isSandbox,
155
+ "unsubscribeDetectedAt": optionalISO(info.unsubscribeDetectedAt),
156
+ "unsubscribeDetectedAtMillis": optionalMillis(info.unsubscribeDetectedAt),
157
+ "billingIssueDetectedAt": optionalISO(info.billingIssueDetectedAt),
158
+ "billingIssueDetectedAtMillis": optionalMillis(info.billingIssueDetectedAt),
159
+ "periodType": periodTypeString(info.periodType),
160
+ "store": storeString(info.store),
161
+ "ownershipType": ownershipString(info.ownershipType),
162
+ "verification": "VERIFIED",
163
+ ]
164
+ }
165
+
166
+ private static func encode(discount: StoreProductDiscount) -> [String: Any] {
167
+ return [
168
+ "identifier": optionalString(discount.offerIdentifier),
169
+ "price": NSDecimalNumber(decimal: discount.price).doubleValue,
170
+ "priceString": "",
171
+ "cycles": discount.numberOfPeriods,
172
+ "period": periodMap(discount.subscriptionPeriod),
173
+ "periodUnit": periodUnitString(discount.subscriptionPeriod.unit),
174
+ "periodNumberOfUnits": discount.subscriptionPeriod.value,
175
+ ]
176
+ }
177
+
178
+ private static func encode(transaction: StoreTransaction) -> [String: Any] {
179
+ return [
180
+ "productIdentifier": transaction.productIdentifier,
181
+ "revenueCatId": transaction.transactionIdentifier,
182
+ "purchaseDate": iso8601(transaction.purchaseDate),
183
+ "storeTransactionId": transaction.transactionIdentifier,
184
+ ]
185
+ }
186
+
187
+ // MARK: - Enum mappers
188
+
189
+ private static func verificationString(_ v: VerificationResult) -> String {
190
+ switch v {
191
+ case .verified: return "VERIFIED"
192
+ case .failed: return "FAILED"
193
+ case .notRequested: return "NOT_REQUESTED"
194
+ }
195
+ }
196
+
197
+ private static func periodTypeString(_ p: PeriodType) -> String {
198
+ switch p {
199
+ case .normal: return "NORMAL"
200
+ case .intro: return "INTRO"
201
+ case .trial: return "TRIAL"
202
+ }
203
+ }
204
+
205
+ private static func storeString(_ s: Store) -> String {
206
+ switch s {
207
+ case .appStore: return "APP_STORE"
208
+ case .playStore: return "PLAY_STORE"
209
+ case .unknownStore: return "UNKNOWN_STORE"
210
+ }
211
+ }
212
+
213
+ private static func ownershipString(_ o: OwnershipType) -> String {
214
+ switch o {
215
+ case .purchased: return "PURCHASED"
216
+ case .familyShared: return "FAMILY_SHARED"
217
+ }
218
+ }
219
+
220
+ private static func packageTypeString(_ p: PackageType) -> String {
221
+ switch p {
222
+ case .unknown: return "UNKNOWN"
223
+ case .custom: return "CUSTOM"
224
+ case .lifetime: return "LIFETIME"
225
+ case .annual: return "ANNUAL"
226
+ case .sixMonth: return "SIX_MONTH"
227
+ case .threeMonth: return "THREE_MONTH"
228
+ case .twoMonth: return "TWO_MONTH"
229
+ case .monthly: return "MONTHLY"
230
+ case .weekly: return "WEEKLY"
231
+ }
232
+ }
233
+
234
+ private static func periodUnitString(_ u: SubscriptionPeriod.Unit) -> String {
235
+ switch u {
236
+ case .day: return "DAY"
237
+ case .week: return "WEEK"
238
+ case .month: return "MONTH"
239
+ case .year: return "YEAR"
240
+ }
241
+ }
242
+
243
+ private static func periodString(_ p: SubscriptionPeriod) -> String {
244
+ // ISO-8601 duration (P1M, P1Y, …) is what RC's wire format used.
245
+ let unit: String = {
246
+ switch p.unit {
247
+ case .day: return "D"
248
+ case .week: return "W"
249
+ case .month: return "M"
250
+ case .year: return "Y"
251
+ }
252
+ }()
253
+ return "P\(p.value)\(unit)"
254
+ }
255
+
256
+ private static func periodMap(_ p: SubscriptionPeriod) -> [String: Any] {
257
+ return [
258
+ "unit": periodUnitString(p.unit),
259
+ "value": p.value,
260
+ ]
261
+ }
262
+
263
+ // MARK: - Primitive helpers
264
+
265
+ private static func optionalPackage(_ pkg: Package?) -> Any {
266
+ if let p = pkg { return encode(package: p) }
267
+ return NSNull()
268
+ }
269
+
270
+ private static func optionalString(_ s: String?) -> Any {
271
+ if let s = s { return s }
272
+ return NSNull()
273
+ }
274
+
275
+ private static func optionalISO(_ d: Date?) -> Any {
276
+ if let d = d { return iso8601(d) }
277
+ return NSNull()
278
+ }
279
+
280
+ private static func optionalMillis(_ d: Date?) -> Any {
281
+ if let d = d { return millis(d) }
282
+ return NSNull()
283
+ }
284
+
285
+ private static func encode(datesByKey: [String: Date]) -> [String: Any] {
286
+ var out: [String: Any] = [:]
287
+ for (k, v) in datesByKey { out[k] = iso8601(v) }
288
+ return out
289
+ }
290
+
291
+ private static let isoFormatter: ISO8601DateFormatter = {
292
+ let f = ISO8601DateFormatter()
293
+ f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
294
+ return f
295
+ }()
296
+
297
+ private static func iso8601(_ d: Date) -> String {
298
+ return isoFormatter.string(from: d)
299
+ }
300
+
301
+ private static func millis(_ d: Date) -> Double {
302
+ return d.timeIntervalSince1970 * 1000.0
303
+ }
304
+ }
305
+
306
+ // MARK: - Error code bridge
307
+ //
308
+ // Map our typed `PurchasesError` cases onto the string-numeric codes the JS
309
+ // PURCHASES_ERROR_CODE enum uses. Keep in sync with
310
+ // `packages/react-native-sdk/src/types.ts`.
311
+
312
+ enum PurchasesErrorCodeBridge {
313
+ static let unknown = "0"
314
+ static let purchaseCancelled = "1"
315
+ static let storeProblem = "2"
316
+ static let productNotAvailable = "5"
317
+ static let receiptAlreadyInUse = "7"
318
+ static let networkError = "10"
319
+ static let paymentPending = "20"
320
+ static let configurationError = "23"
321
+
322
+ /// Resolve the (code, message) pair to hand to `RCTPromiseRejectBlock`.
323
+ static func toJSCodeAndMessage(_ error: PurchasesError) -> (code: String, message: String) {
324
+ switch error {
325
+ case .purchaseCancelled:
326
+ return (purchaseCancelled, "Purchase cancelled by the user")
327
+ case .paymentPending:
328
+ return (paymentPending, "Payment is pending")
329
+ case .productNotAvailable:
330
+ return (productNotAvailable, "Product not available")
331
+ case .networkError(let underlying):
332
+ return (networkError, "Network error: \(underlying)")
333
+ case .receiptAlreadyInUse:
334
+ return (receiptAlreadyInUse, "Receipt already in use")
335
+ case .verificationFailed(let reason):
336
+ return (storeProblem, "Verification failed: \(reason)")
337
+ case .backendRejected(_, let message):
338
+ return (storeProblem, message ?? "Backend rejected the receipt")
339
+ case .backendUnavailable(_, let message):
340
+ return (networkError, message ?? "Backend unavailable")
341
+ case .notConfigured:
342
+ return (configurationError, "SDK is not configured")
343
+ case .unknown(let message):
344
+ return (unknown, message)
345
+ }
346
+ }
347
+ }
@@ -0,0 +1,124 @@
1
+ // packages/react-native-sdk/ios/SwissKnifePurchases.m
2
+ #import <React/RCTBridgeModule.h>
3
+ #import <React/RCTEventEmitter.h>
4
+
5
+ // Registers the Swift `SwissKnifePurchases` class under the JS module name
6
+ // "SwissKnifePurchases" (must match src/native/module.ts MODULE_NAME).
7
+ @interface RCT_EXTERN_MODULE(SwissKnifePurchases, RCTEventEmitter)
8
+
9
+ RCT_EXTERN_METHOD(configure:(NSDictionary *)options
10
+ resolver:(RCTPromiseResolveBlock)resolve
11
+ rejecter:(RCTPromiseRejectBlock)reject)
12
+
13
+ RCT_EXTERN_METHOD(isConfigured:(RCTPromiseResolveBlock)resolve
14
+ rejecter:(RCTPromiseRejectBlock)reject)
15
+
16
+ RCT_EXTERN_METHOD(getCustomerInfo:(RCTPromiseResolveBlock)resolve
17
+ rejecter:(RCTPromiseRejectBlock)reject)
18
+
19
+ RCT_EXTERN_METHOD(getOfferings:(RCTPromiseResolveBlock)resolve
20
+ rejecter:(RCTPromiseRejectBlock)reject)
21
+
22
+ RCT_EXTERN_METHOD(purchasePackage:(NSString *)packageIdentifier
23
+ presentedOfferingIdentifier:(NSString *)presentedOfferingIdentifier
24
+ resolver:(RCTPromiseResolveBlock)resolve
25
+ rejecter:(RCTPromiseRejectBlock)reject)
26
+
27
+ RCT_EXTERN_METHOD(purchaseStoreProduct:(NSString *)productIdentifier
28
+ resolver:(RCTPromiseResolveBlock)resolve
29
+ rejecter:(RCTPromiseRejectBlock)reject)
30
+
31
+ RCT_EXTERN_METHOD(restorePurchases:(RCTPromiseResolveBlock)resolve
32
+ rejecter:(RCTPromiseRejectBlock)reject)
33
+
34
+ RCT_EXTERN_METHOD(logIn:(NSString *)appUserID
35
+ resolver:(RCTPromiseResolveBlock)resolve
36
+ rejecter:(RCTPromiseRejectBlock)reject)
37
+
38
+ RCT_EXTERN_METHOD(logOut:(RCTPromiseResolveBlock)resolve
39
+ rejecter:(RCTPromiseRejectBlock)reject)
40
+
41
+ RCT_EXTERN_METHOD(getAppUserID:(RCTPromiseResolveBlock)resolve
42
+ rejecter:(RCTPromiseRejectBlock)reject)
43
+
44
+ RCT_EXTERN_METHOD(isAnonymous:(RCTPromiseResolveBlock)resolve
45
+ rejecter:(RCTPromiseRejectBlock)reject)
46
+
47
+ // Wave 1 — unified Identity bridge.
48
+ RCT_EXTERN_METHOD(identityCurrent:(RCTPromiseResolveBlock)resolve
49
+ rejecter:(RCTPromiseRejectBlock)reject)
50
+
51
+ RCT_EXTERN_METHOD(identityLogIn:(NSString *)userId
52
+ resolver:(RCTPromiseResolveBlock)resolve
53
+ rejecter:(RCTPromiseRejectBlock)reject)
54
+
55
+ RCT_EXTERN_METHOD(identityLogOut:(RCTPromiseResolveBlock)resolve
56
+ rejecter:(RCTPromiseRejectBlock)reject)
57
+
58
+ RCT_EXTERN_METHOD(identityReset:(RCTPromiseResolveBlock)resolve
59
+ rejecter:(RCTPromiseRejectBlock)reject)
60
+
61
+ RCT_EXTERN_METHOD(identityWipe:(RCTPromiseResolveBlock)resolve
62
+ rejecter:(RCTPromiseRejectBlock)reject)
63
+
64
+ RCT_EXTERN_METHOD(sessionCurrent:(RCTPromiseResolveBlock)resolve
65
+ rejecter:(RCTPromiseRejectBlock)reject)
66
+
67
+ // Wave 2 — Attribution bridge.
68
+ RCT_EXTERN_METHOD(attributionResolved:(RCTPromiseResolveBlock)resolve
69
+ rejecter:(RCTPromiseRejectBlock)reject)
70
+
71
+ RCT_EXTERN_METHOD(attributionRequestAndRegister:(RCTPromiseResolveBlock)resolve
72
+ rejecter:(RCTPromiseRejectBlock)reject)
73
+
74
+ RCT_EXTERN_METHOD(attributionRegisterInstall:(nullable NSString *)advertisingId
75
+ resolver:(RCTPromiseResolveBlock)resolve
76
+ rejecter:(RCTPromiseRejectBlock)reject)
77
+
78
+ RCT_EXTERN_METHOD(setAttributes:(NSDictionary *)attributes
79
+ resolver:(RCTPromiseResolveBlock)resolve
80
+ rejecter:(RCTPromiseRejectBlock)reject)
81
+
82
+ RCT_EXTERN_METHOD(setEmail:(nullable NSString *)email
83
+ resolver:(RCTPromiseResolveBlock)resolve
84
+ rejecter:(RCTPromiseRejectBlock)reject)
85
+
86
+ RCT_EXTERN_METHOD(setDisplayName:(nullable NSString *)displayName
87
+ resolver:(RCTPromiseResolveBlock)resolve
88
+ rejecter:(RCTPromiseRejectBlock)reject)
89
+
90
+ RCT_EXTERN_METHOD(setPhoneNumber:(nullable NSString *)phoneNumber
91
+ resolver:(RCTPromiseResolveBlock)resolve
92
+ rejecter:(RCTPromiseRejectBlock)reject)
93
+
94
+ RCT_EXTERN_METHOD(setPushToken:(nullable NSString *)pushToken
95
+ resolver:(RCTPromiseResolveBlock)resolve
96
+ rejecter:(RCTPromiseRejectBlock)reject)
97
+
98
+ RCT_EXTERN_METHOD(setLogLevel:(NSString *)level
99
+ resolver:(RCTPromiseResolveBlock)resolve
100
+ rejecter:(RCTPromiseRejectBlock)reject)
101
+
102
+ RCT_EXTERN_METHOD(getDeviceId:(RCTPromiseResolveBlock)resolve
103
+ rejecter:(RCTPromiseRejectBlock)reject)
104
+
105
+ // Phase 2.3: RN bridge → native persistent event queue.
106
+ RCT_EXTERN_METHOD(trackEvent:(NSDictionary *)event
107
+ resolver:(RCTPromiseResolveBlock)resolve
108
+ rejecter:(RCTPromiseRejectBlock)reject)
109
+
110
+ RCT_EXTERN_METHOD(flushEvents:(RCTPromiseResolveBlock)resolve
111
+ rejecter:(RCTPromiseRejectBlock)reject)
112
+
113
+ // RCTEventEmitter requires addListener/removeListeners to be declared; the base
114
+ // class implements them. Declaring here keeps the NativeEventEmitter book-keeping
115
+ // (spec.ts addListener/removeListeners) from warning.
116
+ RCT_EXTERN_METHOD(addListener:(NSString *)eventType)
117
+ RCT_EXTERN_METHOD(removeListeners:(double)count)
118
+
119
+ + (BOOL)requiresMainQueueSetup
120
+ {
121
+ return YES;
122
+ }
123
+
124
+ @end