@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,497 @@
1
+ // packages/react-native-sdk/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt
2
+ //
3
+ // Phase 7 of the RC rip-and-replace: the RN bridge module now talks ONLY to
4
+ // our own [com.appswissknife.SwissKnife] facade (Play Billing v8 + our backend).
5
+ // PurchasesHybridCommon imports are gone — every callback maps the typed
6
+ // CustomerInfo / Offerings / etc. values into the [WritableMap] shape the
7
+ // JS-side `src/types.ts` declares via [SwissKnifeJson].
8
+ package com.swissknife.purchases
9
+
10
+ import com.appswissknife.SwissKnife
11
+ import com.appswissknife.models.CustomerInfo
12
+ import com.appswissknife.models.PurchasesError
13
+ import com.appswissknife.models.PurchasesErrorCode
14
+ import com.appswissknife.models.StoreProduct
15
+ import com.facebook.react.bridge.Promise
16
+ import com.facebook.react.bridge.ReactApplicationContext
17
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
18
+ import com.facebook.react.bridge.ReactMethod
19
+ import com.facebook.react.bridge.ReadableMap
20
+ import com.facebook.react.modules.core.DeviceEventManagerModule
21
+
22
+ class SwissKnifePurchasesModule(
23
+ private val reactContext: ReactApplicationContext
24
+ ) : ReactContextBaseJavaModule(reactContext) {
25
+
26
+ // Must match src/native/module.ts MODULE_NAME exactly.
27
+ override fun getName(): String = "SwissKnifePurchases"
28
+
29
+ // The single CustomerInfo bridge listener. Removed + re-added on every
30
+ // configure() so re-configuring does not stack duplicate listeners
31
+ // (which would emit duplicate CustomerInfo events to JS).
32
+ private var customerInfoListener: ((CustomerInfo) -> Unit)? = null
33
+
34
+ // ----- Lifecycle -----
35
+
36
+ @ReactMethod
37
+ fun configure(options: ReadableMap, promise: Promise) {
38
+ val apiKey = options.getString("apiKey")
39
+ if (apiKey.isNullOrEmpty()) {
40
+ promise.reject("CONFIGURATION", "configure requires a non-empty apiKey")
41
+ return
42
+ }
43
+ val appUserID = if (options.hasKey("appUserID")) options.getString("appUserID") else null
44
+ val proxyURL = if (options.hasKey("proxyURL")) options.getString("proxyURL") else null
45
+ val useAmazon = options.hasKey("useAmazon") && options.getBoolean("useAmazon")
46
+ val logLevel = if (options.hasKey("logLevel")) options.getString("logLevel") else null
47
+
48
+ SwissKnife.configure(
49
+ context = reactContext,
50
+ apiKey = apiKey,
51
+ appUserID = appUserID,
52
+ proxyURL = proxyURL,
53
+ useAmazon = useAmazon,
54
+ logLevel = logLevel,
55
+ )
56
+
57
+ // Bridge the native SDK's CustomerInfo stream to the JS event channel.
58
+ // Remove any prior listener first so re-configure does not stack duplicates.
59
+ customerInfoListener?.let { SwissKnife.removeCustomerInfoListener(it) }
60
+ val listener: (CustomerInfo) -> Unit = { info -> emitCustomerInfo(info) }
61
+ customerInfoListener = listener
62
+ SwissKnife.addCustomerInfoListener(listener)
63
+
64
+ promise.resolve(null)
65
+ }
66
+
67
+ @ReactMethod
68
+ fun isConfigured(promise: Promise) {
69
+ promise.resolve(SwissKnife.isConfigured)
70
+ }
71
+
72
+ // ----- CustomerInfo / Offerings -----
73
+
74
+ @ReactMethod
75
+ fun getCustomerInfo(promise: Promise) {
76
+ SwissKnife.getCustomerInfo(
77
+ onSuccess = { info -> promise.resolve(SwissKnifeJson.encode(info)) },
78
+ onError = { err -> rejectWithPurchasesError(promise, err) },
79
+ )
80
+ }
81
+
82
+ @ReactMethod
83
+ fun getOfferings(promise: Promise) {
84
+ SwissKnife.getOfferings(
85
+ onSuccess = { offerings -> promise.resolve(SwissKnifeJson.encode(offerings)) },
86
+ onError = { err -> rejectWithPurchasesError(promise, err) },
87
+ )
88
+ }
89
+
90
+ // ----- Purchasing -----
91
+
92
+ @ReactMethod
93
+ fun purchasePackage(
94
+ packageIdentifier: String,
95
+ presentedOfferingIdentifier: String,
96
+ promise: Promise,
97
+ ) {
98
+ // RN 0.80+: ReactContextBaseJavaModule is Kotlin, so getCurrentActivity()
99
+ // is no longer exposed as the `currentActivity` synthetic property. Use the
100
+ // ReactApplicationContext accessor (the deprecation's ReplaceWith target).
101
+ val activity = reactApplicationContext.currentActivity
102
+ if (activity == null) {
103
+ promise.reject("PURCHASE", "There is no current Activity to launch the purchase flow")
104
+ return
105
+ }
106
+ SwissKnife.getOfferings(
107
+ onSuccess = { offerings ->
108
+ val offering = offerings.all[presentedOfferingIdentifier]
109
+ val pkg = offering?.availablePackages?.firstOrNull { it.identifier == packageIdentifier }
110
+ if (pkg == null) {
111
+ promise.reject(
112
+ errorCodeToString(PurchasesErrorCode.ProductNotAvailableForPurchaseError),
113
+ "Package $packageIdentifier not found in offering $presentedOfferingIdentifier",
114
+ )
115
+ return@getOfferings
116
+ }
117
+ SwissKnife.purchasePackage(
118
+ activity = activity,
119
+ packageToPurchase = pkg,
120
+ onSuccess = { result ->
121
+ promise.resolve(SwissKnifeJson.encodePurchaseResult(result, pkg.product.id))
122
+ },
123
+ onError = { err, _ -> rejectWithPurchasesError(promise, err) },
124
+ )
125
+ },
126
+ onError = { err -> rejectWithPurchasesError(promise, err) },
127
+ )
128
+ }
129
+
130
+ @ReactMethod
131
+ fun purchaseStoreProduct(productIdentifier: String, promise: Promise) {
132
+ // RN 0.80+: ReactContextBaseJavaModule is Kotlin, so getCurrentActivity()
133
+ // is no longer exposed as the `currentActivity` synthetic property. Use the
134
+ // ReactApplicationContext accessor (the deprecation's ReplaceWith target).
135
+ val activity = reactApplicationContext.currentActivity
136
+ if (activity == null) {
137
+ promise.reject("PURCHASE", "There is no current Activity to launch the purchase flow")
138
+ return
139
+ }
140
+ // Synthesize a thin StoreProduct - the gateway resolves the full
141
+ // ProductDetails internally before launching the billing flow.
142
+ val product: StoreProduct = StubStoreProduct(productIdentifier)
143
+ SwissKnife.purchaseStoreProduct(
144
+ activity = activity,
145
+ product = product,
146
+ onSuccess = { result ->
147
+ promise.resolve(SwissKnifeJson.encodePurchaseResult(result, productIdentifier))
148
+ },
149
+ onError = { err, _ -> rejectWithPurchasesError(promise, err) },
150
+ )
151
+ }
152
+
153
+ @ReactMethod
154
+ fun restorePurchases(promise: Promise) {
155
+ SwissKnife.restorePurchases(
156
+ onSuccess = { info -> promise.resolve(SwissKnifeJson.encode(info)) },
157
+ onError = { err -> rejectWithPurchasesError(promise, err) },
158
+ )
159
+ }
160
+
161
+ // ----- Identity -----
162
+
163
+ @ReactMethod
164
+ fun logIn(appUserID: String, promise: Promise) {
165
+ SwissKnife.logIn(
166
+ appUserID = appUserID,
167
+ onSuccess = { info, created ->
168
+ val map = com.facebook.react.bridge.Arguments.createMap()
169
+ map.putMap("customerInfo", SwissKnifeJson.encode(info))
170
+ map.putBoolean("created", created)
171
+ promise.resolve(map)
172
+ },
173
+ onError = { err -> rejectWithPurchasesError(promise, err) },
174
+ )
175
+ }
176
+
177
+ @ReactMethod
178
+ fun logOut(promise: Promise) {
179
+ SwissKnife.logOut(
180
+ onSuccess = { info -> promise.resolve(SwissKnifeJson.encode(info)) },
181
+ onError = { err -> rejectWithPurchasesError(promise, err) },
182
+ )
183
+ }
184
+
185
+ @ReactMethod
186
+ fun getAppUserID(promise: Promise) = promise.resolve(SwissKnife.appUserID)
187
+
188
+ @ReactMethod
189
+ fun isAnonymous(promise: Promise) = promise.resolve(SwissKnife.isAnonymous())
190
+
191
+ // ----- Identity (Wave 1) -----
192
+ //
193
+ // Unified identity bridge. All five methods delegate to
194
+ // [com.appswissknife.identity.Identity] so iOS / Android / RN share
195
+ // the same source of truth and the bug-fixed
196
+ // `/api/sdk/users/identify` POST behaviour.
197
+
198
+ @ReactMethod
199
+ fun identityCurrent(promise: Promise) {
200
+ val snapshot = com.appswissknife.identity.Identity.current()
201
+ val map = com.facebook.react.bridge.Arguments.createMap()
202
+ map.putString("anonymousId", snapshot.anonymousId)
203
+ if (snapshot.userId != null) map.putString("userId", snapshot.userId) else map.putNull("userId")
204
+ map.putBoolean("isAnonymous", snapshot.isAnonymous)
205
+ promise.resolve(map)
206
+ }
207
+
208
+ @ReactMethod
209
+ fun identityLogIn(userId: String, promise: Promise) {
210
+ com.appswissknife.identity.Identity.logIn(
211
+ userId = userId,
212
+ onSuccess = { result ->
213
+ val map = com.facebook.react.bridge.Arguments.createMap()
214
+ map.putMap("customerInfo", SwissKnifeJson.encode(result.customerInfo))
215
+ map.putBoolean("created", result.created)
216
+ promise.resolve(map)
217
+ },
218
+ onError = { err -> rejectWithPurchasesError(promise, err) },
219
+ )
220
+ }
221
+
222
+ @ReactMethod
223
+ fun identityLogOut(promise: Promise) {
224
+ com.appswissknife.identity.Identity.logOut(
225
+ onSuccess = { promise.resolve(null) },
226
+ onError = { err -> rejectWithPurchasesError(promise, err) },
227
+ )
228
+ }
229
+
230
+ @ReactMethod
231
+ fun identityReset(promise: Promise) {
232
+ com.appswissknife.identity.Identity.reset()
233
+ promise.resolve(null)
234
+ }
235
+
236
+ @ReactMethod
237
+ fun identityWipe(promise: Promise) {
238
+ com.appswissknife.identity.Identity.wipe(reactApplicationContext)
239
+ promise.resolve(null)
240
+ }
241
+
242
+ /**
243
+ * Post-#136 audit: JS-side analytics used `Date.now()` for session_id which
244
+ * diverged from the envelope. This bridge surfaces the canonical
245
+ * `Session.shared(ctx).current` snapshot so JS' legacy `session_id` field
246
+ * agrees with the envelope.
247
+ */
248
+ @ReactMethod
249
+ fun sessionCurrent(promise: Promise) {
250
+ val info = com.appswissknife.SwissKnife.currentSession(reactApplicationContext)
251
+ val map = com.facebook.react.bridge.Arguments.createMap().apply {
252
+ putString("id", info.id)
253
+ putInt("sessionNumber", info.sessionNumber)
254
+ putDouble("startedAt", info.startedAtMs.toDouble())
255
+ }
256
+ promise.resolve(map)
257
+ }
258
+
259
+ // ----- Attribution (Wave 2) -----
260
+ //
261
+ // Thin passthroughs over `com.appswissknife.attribution.Attribution`.
262
+ // Native owns the disk-backed idempotency cache, the
263
+ // AdvertisingId/InstallReferrer collectors, and the install POST.
264
+
265
+ @ReactMethod
266
+ fun attributionResolved(promise: Promise) {
267
+ promise.resolve(encodeAttribution(com.appswissknife.attribution.Attribution.resolved()))
268
+ }
269
+
270
+ @ReactMethod
271
+ fun attributionRequestAndRegister(promise: Promise) {
272
+ com.appswissknife.attribution.Attribution.collectAndRegister(
273
+ context = reactApplicationContext,
274
+ onSuccess = { resolved -> promise.resolve(encodeAttribution(resolved)) },
275
+ onError = { err -> rejectWithPurchasesError(promise, err) },
276
+ )
277
+ }
278
+
279
+ @ReactMethod
280
+ fun attributionRegisterInstall(advertisingId: String?, promise: Promise) {
281
+ com.appswissknife.attribution.Attribution.registerInstall(
282
+ advertisingId = advertisingId?.takeIf { it.isNotEmpty() },
283
+ referrer = null,
284
+ onSuccess = { resolved -> promise.resolve(encodeAttribution(resolved)) },
285
+ onError = { err -> rejectWithPurchasesError(promise, err) },
286
+ )
287
+ }
288
+
289
+ /**
290
+ * Encode the ResolvedAttribution into the RN bridge WritableMap shape
291
+ * expected by `packages/react-native-sdk/src/types.ts.ResolvedAttribution`
292
+ * (`method`, `campaignId`, `resolvedAt` epoch-ms).
293
+ */
294
+ private fun encodeAttribution(
295
+ resolved: com.appswissknife.attribution.Attribution.ResolvedAttribution?,
296
+ ): com.facebook.react.bridge.WritableMap? {
297
+ if (resolved == null) return null
298
+ val map = com.facebook.react.bridge.Arguments.createMap()
299
+ map.putString("method", resolved.method)
300
+ if (resolved.campaignId != null) map.putString("campaignId", resolved.campaignId)
301
+ else map.putNull("campaignId")
302
+ map.putDouble("resolvedAt", resolved.resolvedAt.toDouble())
303
+ return map
304
+ }
305
+
306
+ // ----- Subscriber attributes -----
307
+ //
308
+ // Phase 3 of the RC rip-and-replace: delegate to com.appswissknife.SwissKnife,
309
+ // which routes through com.appswissknife.attributes.AttributesClient ->
310
+ // /api/sdk/users/attributes. Phase 7 leaves this path untouched.
311
+
312
+ @ReactMethod
313
+ fun setAttributes(attributes: ReadableMap, promise: Promise) {
314
+ try {
315
+ SwissKnife.setAttributes(attributes.toStringMap())
316
+ promise.resolve(null)
317
+ } catch (e: Throwable) {
318
+ promise.reject("SET_ATTRIBUTES_ERROR", e.message ?: "unknown", e)
319
+ }
320
+ }
321
+
322
+ @ReactMethod
323
+ fun setEmail(email: String?, promise: Promise) {
324
+ try {
325
+ SwissKnife.setEmail(email)
326
+ promise.resolve(null)
327
+ } catch (e: Throwable) {
328
+ promise.reject("SET_EMAIL_ERROR", e.message ?: "unknown", e)
329
+ }
330
+ }
331
+
332
+ @ReactMethod
333
+ fun setDisplayName(displayName: String?, promise: Promise) {
334
+ try {
335
+ SwissKnife.setDisplayName(displayName)
336
+ promise.resolve(null)
337
+ } catch (e: Throwable) {
338
+ promise.reject("SET_DISPLAY_NAME_ERROR", e.message ?: "unknown", e)
339
+ }
340
+ }
341
+
342
+ @ReactMethod
343
+ fun setPhoneNumber(phoneNumber: String?, promise: Promise) {
344
+ try {
345
+ SwissKnife.setPhoneNumber(phoneNumber)
346
+ promise.resolve(null)
347
+ } catch (e: Throwable) {
348
+ promise.reject("SET_PHONE_NUMBER_ERROR", e.message ?: "unknown", e)
349
+ }
350
+ }
351
+
352
+ @ReactMethod
353
+ fun setPushToken(pushToken: String?, promise: Promise) {
354
+ try {
355
+ SwissKnife.setPushToken(pushToken)
356
+ promise.resolve(null)
357
+ } catch (e: Throwable) {
358
+ promise.reject("SET_PUSH_TOKEN_ERROR", e.message ?: "unknown", e)
359
+ }
360
+ }
361
+
362
+ @ReactMethod
363
+ fun setLogLevel(level: String, promise: Promise) {
364
+ SwissKnife.setLogLevel(level)
365
+ promise.resolve(null)
366
+ }
367
+
368
+ // ----- Device identifier -----
369
+
370
+ // Delegates to swiss-knife-android's `SwissKnife.getDeviceId(context)` facade
371
+ // (EncryptedSharedPreferences-backed UUID). The native SDK owns persistence;
372
+ // JS must NOT generate this id - see src/modules/analytics.ts for rationale.
373
+ @ReactMethod
374
+ fun getDeviceId(promise: Promise) {
375
+ try {
376
+ promise.resolve(SwissKnife.getDeviceId(reactApplicationContext))
377
+ } catch (e: Throwable) {
378
+ promise.reject("DEVICE_ID_ERROR", e.message ?: "unknown", e)
379
+ }
380
+ }
381
+
382
+ // ----- Analytics persistent queue (Phase 2.3 - RN bridge -> native) -----
383
+
384
+ /**
385
+ * Wire the observability backend (analytics/crash/perf ingest) independently
386
+ * of Purchases.configure. Lets a non-SDK-store build (e.g. RuStore, where
387
+ * Purchases.configure is never called) still send analytics. Idempotent on
388
+ * the native side. Called from JS Analytics.init with the same serverURL.
389
+ */
390
+ @ReactMethod
391
+ fun configureObservability(apiKey: String, serverURL: String, promise: Promise) {
392
+ try {
393
+ SwissKnife.ensureObservabilityBackend(reactApplicationContext, apiKey, serverURL)
394
+ promise.resolve(null)
395
+ } catch (e: Throwable) {
396
+ promise.reject("OBSERVABILITY_CONFIG_ERROR", e.message ?: "unknown", e)
397
+ }
398
+ }
399
+
400
+ @ReactMethod
401
+ fun trackEvent(event: ReadableMap, promise: Promise) {
402
+ try {
403
+ SwissKnife.trackEvent(event.toHashMap())
404
+ promise.resolve(null)
405
+ } catch (e: Throwable) {
406
+ promise.reject("TRACK_ERROR", e.message ?: "unknown", e)
407
+ }
408
+ }
409
+
410
+ @ReactMethod
411
+ fun flushEvents(promise: Promise) {
412
+ try {
413
+ SwissKnife.flushEvents()
414
+ promise.resolve(null)
415
+ } catch (e: Throwable) {
416
+ promise.reject("FLUSH_ERROR", e.message ?: "unknown", e)
417
+ }
418
+ }
419
+
420
+ // ----- NativeEventEmitter book-keeping (no-op on Android; required by JS spec) -----
421
+
422
+ @ReactMethod
423
+ fun addListener(eventType: String) { /* RN built-in handles registration */ }
424
+
425
+ @ReactMethod
426
+ fun removeListeners(count: Double) { /* no-op */ }
427
+
428
+ // ----- CustomerInfo update stream -----
429
+
430
+ // The Phase 6 [com.appswissknife.SwissKnife] listener delivers a typed
431
+ // CustomerInfo; encode it into the WritableMap shape JS expects and re-emit.
432
+ private fun emitCustomerInfo(info: CustomerInfo) {
433
+ reactContext
434
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
435
+ .emit("Purchases-CustomerInfoUpdated", SwissKnifeJson.encode(info))
436
+ }
437
+
438
+ // ----- Error mapping -----
439
+
440
+ /**
441
+ * Map a typed [PurchasesError] (from swiss-knife-android) to the
442
+ * RN promise rejection shape JS expects. The numeric `code` string matches
443
+ * `src/types.ts` PURCHASES_ERROR_CODE constants so the JS PurchasesError
444
+ * surface keeps working unchanged.
445
+ */
446
+ internal fun rejectWithPurchasesError(promise: Promise, err: PurchasesError) {
447
+ promise.reject(errorCodeToString(err.code), err.message, err)
448
+ }
449
+
450
+ internal fun errorCodeToString(code: PurchasesErrorCode): String = when (code) {
451
+ PurchasesErrorCode.UnknownError -> "0"
452
+ PurchasesErrorCode.PurchaseCancelledError -> "1"
453
+ PurchasesErrorCode.StoreProblemError -> "2"
454
+ PurchasesErrorCode.PurchaseNotAllowedError -> "3"
455
+ PurchasesErrorCode.PurchaseInvalidError -> "4"
456
+ PurchasesErrorCode.ProductNotAvailableForPurchaseError -> "5"
457
+ PurchasesErrorCode.ProductAlreadyPurchasedError -> "6"
458
+ PurchasesErrorCode.ReceiptAlreadyInUseError -> "7"
459
+ PurchasesErrorCode.InvalidReceiptError -> "8"
460
+ PurchasesErrorCode.MissingReceiptFileError -> "9"
461
+ PurchasesErrorCode.NetworkError -> "10"
462
+ PurchasesErrorCode.InvalidCredentialsError -> "11"
463
+ PurchasesErrorCode.UnexpectedBackendResponseError -> "12"
464
+ PurchasesErrorCode.ReceiptInUseByOtherSubscriberError -> "13"
465
+ PurchasesErrorCode.InvalidAppUserIdError -> "14"
466
+ PurchasesErrorCode.OperationAlreadyInProgressError -> "15"
467
+ PurchasesErrorCode.UnknownBackendError -> "16"
468
+ PurchasesErrorCode.InvalidAppleSubscriptionKeyError -> "17"
469
+ PurchasesErrorCode.IneligibleError -> "18"
470
+ PurchasesErrorCode.InsufficientPermissionsError -> "19"
471
+ PurchasesErrorCode.PaymentPendingError -> "20"
472
+ PurchasesErrorCode.InvalidSubscriberAttributesError -> "21"
473
+ PurchasesErrorCode.LogOutAnonymousUserError -> "22"
474
+ PurchasesErrorCode.ConfigurationError -> "23"
475
+ PurchasesErrorCode.UnsupportedError -> "24"
476
+ PurchasesErrorCode.EmptySubscriberAttributesError -> "25"
477
+ PurchasesErrorCode.CustomerInfoError -> "28"
478
+ }
479
+ }
480
+
481
+ /**
482
+ * Minimal [StoreProduct] implementation used when JS calls `purchaseStoreProduct`
483
+ * with just a product identifier. The gateway resolves the live ProductDetails
484
+ * internally via the BillingClient; only the `id` field is consulted on this path.
485
+ */
486
+ private class StubStoreProduct(override val id: String) : StoreProduct {
487
+ override val localizedTitle: String = ""
488
+ override val localizedDescription: String = ""
489
+ override val price: com.appswissknife.models.Price = object : com.appswissknife.models.Price {
490
+ override val formatted: String = ""
491
+ override val currencyCode: String = ""
492
+ override val amountMicros: Long = 0L
493
+ }
494
+ override val subscriptionPeriod: com.appswissknife.models.SubscriptionPeriod? = null
495
+ override val introductoryDiscount: com.appswissknife.models.StoreProductDiscount? = null
496
+ override val discounts: List<com.appswissknife.models.StoreProductDiscount> = emptyList()
497
+ }
@@ -0,0 +1,25 @@
1
+ // packages/react-native-sdk/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesPackage.kt
2
+ package com.swissknife.purchases
3
+
4
+ import com.facebook.react.ReactPackage
5
+ import com.facebook.react.bridge.NativeModule
6
+ import com.facebook.react.bridge.ReactApplicationContext
7
+ import com.facebook.react.uimanager.ViewManager
8
+ import com.swissknife.crashreporting.SKCrashReportingModule
9
+
10
+ class SwissKnifePurchasesPackage : ReactPackage {
11
+ // Registers BOTH native modules through one ReactPackage. RN autolinking wires
12
+ // a single package per module; the crash-reporting module is folded in here so
13
+ // it isn't lost (its own SKCrashReportingPackage is not autolinked — see
14
+ // react-native.config.js, which pins this package by its correct FQN).
15
+ override fun createNativeModules(
16
+ reactContext: ReactApplicationContext
17
+ ): List<NativeModule> = listOf(
18
+ SwissKnifePurchasesModule(reactContext),
19
+ SKCrashReportingModule(reactContext),
20
+ )
21
+
22
+ override fun createViewManagers(
23
+ reactContext: ReactApplicationContext
24
+ ): List<ViewManager<*, *>> = emptyList()
25
+ }
@@ -0,0 +1,10 @@
1
+ GENERATED — DO NOT EDIT.
2
+
3
+ Copied verbatim by packages/react-native-sdk/scripts/vendor-native.mjs from the
4
+ standalone native SDK sources in the monorepo. Edit those instead:
5
+
6
+ packages/swiss-knife-ios/Sources/SwissKnifeiOS/**
7
+ packages/swiss-knife-android/src/main/{java,res}/**
8
+
9
+ This directory is gitignored and regenerated on every `build` / `prepack`.
10
+ It ships inside the npm tarball so the package is self-contained.