@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,371 @@
1
+ // packages/react-native-sdk/android/src/main/java/com/swissknife/purchases/SwissKnifeJson.kt
2
+ //
3
+ // Phase 7 - Kotlin-to-WritableMap encoder for the RN bridge. Converts our own
4
+ // [com.appswissknife.models.CustomerInfo] / [Offerings] / etc. instances
5
+ // (defined in `packages/swiss-knife-android/src/main/java/com/appswissknife/models/`)
6
+ // into the [WritableMap]/[WritableArray] graph that `packages/react-native-sdk/src/types.ts`
7
+ // declares. Field-by-field parity with the TS types is load-bearing.
8
+ package com.swissknife.purchases
9
+
10
+ import com.appswissknife.StoreTransactionResult
11
+ import com.appswissknife.models.CustomerInfo
12
+ import com.appswissknife.models.EntitlementInfo
13
+ import com.appswissknife.models.EntitlementInfos
14
+ import com.appswissknife.models.Offering
15
+ import com.appswissknife.models.Offerings
16
+ import com.appswissknife.models.Package
17
+ import com.appswissknife.models.PackageType
18
+ import com.appswissknife.models.OwnershipType
19
+ import com.appswissknife.models.PeriodType
20
+ import com.appswissknife.models.Store
21
+ import com.appswissknife.models.StoreProduct
22
+ import com.appswissknife.models.StoreProductDiscount
23
+ import com.appswissknife.models.StoreTransaction
24
+ import com.appswissknife.models.SubscriptionPeriod
25
+ import com.appswissknife.models.VerificationResult
26
+ import com.facebook.react.bridge.Arguments
27
+ import com.facebook.react.bridge.WritableArray
28
+ import com.facebook.react.bridge.WritableMap
29
+ import java.text.SimpleDateFormat
30
+ import java.util.Date
31
+ import java.util.Locale
32
+ import java.util.TimeZone
33
+
34
+ object SwissKnifeJson {
35
+
36
+ // ----- Top-level encoders -----
37
+
38
+ fun encode(info: CustomerInfo): WritableMap {
39
+ val map = Arguments.createMap()
40
+ map.putMap("entitlements", encodeEntitlements(info.entitlements))
41
+ map.putArray("activeSubscriptions", stringArray(info.activeSubscriptions.toList()))
42
+ map.putArray("allPurchasedProductIdentifiers", stringArray(info.allPurchasedProductIds.toList()))
43
+ map.putMap("allPurchaseDates", encodeDateMap(info.allPurchaseDates))
44
+ map.putMap("subscriptionsByProductIdentifier", Arguments.createMap())
45
+ map.putArray("nonSubscriptionTransactions", Arguments.createArray())
46
+ map.putString("firstSeen", iso(info.firstSeen))
47
+ map.putDouble("firstSeenMillis", millis(info.firstSeen))
48
+ map.putString("originalAppUserId", info.originalAppUserId)
49
+ map.putString("requestDate", iso(info.requestDate))
50
+ map.putDouble("requestDateMillis", millis(info.requestDate))
51
+ map.putNullableString("latestExpirationDate", info.latestExpirationDate?.let { iso(it) })
52
+ map.putNullableDouble("latestExpirationDateMillis", info.latestExpirationDate?.let { millis(it) })
53
+ map.putNullableString("originalPurchaseDate", info.originalPurchaseDate?.let { iso(it) })
54
+ map.putNullableDouble("originalPurchaseDateMillis", info.originalPurchaseDate?.let { millis(it) })
55
+ map.putNull("originalApplicationVersion")
56
+ map.putNullableString("managementURL", info.managementURL)
57
+ return map
58
+ }
59
+
60
+ fun encode(offerings: Offerings): WritableMap {
61
+ val map = Arguments.createMap()
62
+ val all = Arguments.createMap()
63
+ offerings.all.forEach { (k, v) -> all.putMap(k, encode(v)) }
64
+ map.putMap("all", all)
65
+ val current = offerings.current
66
+ if (current == null) {
67
+ map.putNull("current")
68
+ } else {
69
+ map.putMap("current", encode(current))
70
+ }
71
+ return map
72
+ }
73
+
74
+ fun encode(offering: Offering): WritableMap {
75
+ val map = Arguments.createMap()
76
+ map.putString("identifier", offering.identifier)
77
+ map.putString("serverDescription", offering.serverDescription)
78
+ map.putMap("metadata", anyMap(offering.metadata))
79
+ val packages = Arguments.createArray()
80
+ offering.availablePackages.forEach { packages.pushMap(encode(it)) }
81
+ map.putArray("availablePackages", packages)
82
+ map.putNullableMap("lifetime", offering.availablePackages.firstOrNull { it.packageType == PackageType.LIFETIME }?.let { encode(it) })
83
+ map.putNullableMap("annual", offering.availablePackages.firstOrNull { it.packageType == PackageType.ANNUAL }?.let { encode(it) })
84
+ map.putNullableMap("sixMonth", offering.availablePackages.firstOrNull { it.packageType == PackageType.SIX_MONTH }?.let { encode(it) })
85
+ map.putNullableMap("threeMonth", offering.availablePackages.firstOrNull { it.packageType == PackageType.THREE_MONTH }?.let { encode(it) })
86
+ map.putNullableMap("twoMonth", offering.availablePackages.firstOrNull { it.packageType == PackageType.TWO_MONTH }?.let { encode(it) })
87
+ map.putNullableMap("monthly", offering.availablePackages.firstOrNull { it.packageType == PackageType.MONTHLY }?.let { encode(it) })
88
+ map.putNullableMap("weekly", offering.availablePackages.firstOrNull { it.packageType == PackageType.WEEKLY }?.let { encode(it) })
89
+ map.putNull("paywall")
90
+ map.putNull("paywallComponents")
91
+ return map
92
+ }
93
+
94
+ fun encode(pkg: Package): WritableMap {
95
+ val map = Arguments.createMap()
96
+ map.putString("identifier", pkg.identifier)
97
+ map.putString("packageType", packageTypeString(pkg.packageType))
98
+ map.putMap("product", encode(pkg.product))
99
+ val context = Arguments.createMap()
100
+ context.putString("offeringIdentifier", pkg.offeringIdentifier)
101
+ context.putNull("placementIdentifier")
102
+ context.putNull("targetingContext")
103
+ map.putMap("presentedOfferingContext", context)
104
+ map.putString("offeringIdentifier", pkg.offeringIdentifier)
105
+ return map
106
+ }
107
+
108
+ fun encode(product: StoreProduct): WritableMap {
109
+ val map = Arguments.createMap()
110
+ map.putString("identifier", product.id)
111
+ map.putString("title", product.localizedTitle)
112
+ map.putString("description", product.localizedDescription)
113
+ map.putString("priceString", product.price.formatted)
114
+ map.putDouble("price", product.price.amountMicros / 1_000_000.0)
115
+ map.putString("currencyCode", product.price.currencyCode)
116
+ map.putString("productCategory", "SUBSCRIPTION")
117
+ map.putString("productType", "AUTO_RENEWABLE_SUBSCRIPTION")
118
+ map.putNullableString(
119
+ "subscriptionPeriod",
120
+ product.subscriptionPeriod?.let { periodToISOString(it) },
121
+ )
122
+ map.putNull("defaultOption")
123
+ map.putNull("subscriptionOptions")
124
+ map.putNull("presentedOfferingContext")
125
+ map.putNullableMap("introPrice", product.introductoryDiscount?.let { encode(it) })
126
+ val discounts = Arguments.createArray()
127
+ product.discounts.forEach { discounts.pushMap(encode(it)) }
128
+ map.putArray("discounts", discounts)
129
+ map.putNull("pricePerWeek")
130
+ map.putNull("pricePerMonth")
131
+ map.putNull("pricePerYear")
132
+ map.putNull("pricePerWeekString")
133
+ map.putNull("pricePerMonthString")
134
+ map.putNull("pricePerYearString")
135
+ return map
136
+ }
137
+
138
+ fun encodePurchaseResult(result: StoreTransactionResult, productIdentifier: String): WritableMap {
139
+ val map = Arguments.createMap()
140
+ map.putString("productIdentifier", productIdentifier)
141
+ map.putMap("customerInfo", encode(result.customerInfo))
142
+ val tx = result.transaction
143
+ if (tx == null) map.putNull("transaction") else map.putMap("transaction", encode(tx))
144
+ return map
145
+ }
146
+
147
+ // ----- Sub-encoders -----
148
+
149
+ private fun encodeEntitlements(infos: EntitlementInfos): WritableMap {
150
+ val map = Arguments.createMap()
151
+ val all = Arguments.createMap()
152
+ infos.all.forEach { (k, v) -> all.putMap(k, encode(v)) }
153
+ map.putMap("all", all)
154
+ val active = Arguments.createMap()
155
+ infos.active.forEach { (k, v) -> active.putMap(k, encode(v)) }
156
+ map.putMap("active", active)
157
+ map.putString("verification", verificationString(infos.verification))
158
+ return map
159
+ }
160
+
161
+ private fun encode(info: EntitlementInfo): WritableMap {
162
+ val map = Arguments.createMap()
163
+ map.putString("identifier", info.identifier)
164
+ map.putBoolean("isActive", info.isActive)
165
+ map.putBoolean("willRenew", info.willRenew)
166
+ map.putNullableString("latestPurchaseDate", info.latestPurchaseDate?.let { iso(it) })
167
+ map.putNullableDouble("latestPurchaseDateMillis", info.latestPurchaseDate?.let { millis(it) })
168
+ map.putNullableString("originalPurchaseDate", info.latestPurchaseDate?.let { iso(it) })
169
+ map.putNullableDouble("originalPurchaseDateMillis", info.latestPurchaseDate?.let { millis(it) })
170
+ map.putNullableString("expirationDate", info.expirationDate?.let { iso(it) })
171
+ map.putNullableDouble("expirationDateMillis", info.expirationDate?.let { millis(it) })
172
+ map.putString("productIdentifier", info.productIdentifier)
173
+ map.putNullableString("productPlanIdentifier", info.productPlanIdentifier)
174
+ map.putBoolean("isSandbox", info.isSandbox)
175
+ map.putNullableString("unsubscribeDetectedAt", info.unsubscribeDetectedAt?.let { iso(it) })
176
+ map.putNullableDouble("unsubscribeDetectedAtMillis", info.unsubscribeDetectedAt?.let { millis(it) })
177
+ map.putNullableString("billingIssueDetectedAt", info.billingIssueDetectedAt?.let { iso(it) })
178
+ map.putNullableDouble("billingIssueDetectedAtMillis", info.billingIssueDetectedAt?.let { millis(it) })
179
+ map.putString("periodType", periodTypeString(info.periodType))
180
+ map.putString("store", storeString(info.store))
181
+ map.putString("ownershipType", ownershipString(info.ownershipType))
182
+ map.putString("verification", "VERIFIED")
183
+ return map
184
+ }
185
+
186
+ private fun encode(discount: StoreProductDiscount): WritableMap {
187
+ val map = Arguments.createMap()
188
+ map.putNullableString("identifier", discount.identifier)
189
+ map.putDouble("price", discount.price.amountMicros / 1_000_000.0)
190
+ map.putString("priceString", discount.price.formatted)
191
+ map.putInt("cycles", 1)
192
+ map.putMap("period", periodMap(discount.subscriptionPeriod))
193
+ map.putString("periodUnit", periodUnitString(discount.subscriptionPeriod.unit))
194
+ map.putInt("periodNumberOfUnits", discount.subscriptionPeriod.value)
195
+ return map
196
+ }
197
+
198
+ private fun encode(tx: StoreTransaction): WritableMap {
199
+ val map = Arguments.createMap()
200
+ map.putString("productIdentifier", tx.productIdentifier)
201
+ map.putString("revenueCatId", tx.transactionIdentifier)
202
+ map.putString("purchaseDate", iso(tx.purchaseDate))
203
+ map.putString("storeTransactionId", tx.transactionIdentifier)
204
+ return map
205
+ }
206
+
207
+ // ----- Enum mappers -----
208
+
209
+ private fun verificationString(v: VerificationResult): String = when (v) {
210
+ VerificationResult.NOT_REQUESTED -> "NOT_REQUESTED"
211
+ VerificationResult.VERIFIED -> "VERIFIED"
212
+ VerificationResult.FAILED -> "FAILED"
213
+ VerificationResult.VERIFIED_ON_DEVICE -> "VERIFIED_ON_DEVICE"
214
+ }
215
+
216
+ private fun periodTypeString(p: PeriodType): String = when (p) {
217
+ PeriodType.NORMAL -> "NORMAL"
218
+ PeriodType.INTRO -> "INTRO"
219
+ PeriodType.TRIAL -> "TRIAL"
220
+ PeriodType.PREPAID -> "PREPAID"
221
+ }
222
+
223
+ private fun storeString(s: Store): String = when (s) {
224
+ Store.APP_STORE -> "APP_STORE"
225
+ Store.PLAY_STORE -> "PLAY_STORE"
226
+ Store.AMAZON -> "AMAZON"
227
+ Store.STRIPE -> "STRIPE"
228
+ Store.PROMOTIONAL -> "PROMOTIONAL"
229
+ Store.MAC_APP_STORE -> "MAC_APP_STORE"
230
+ Store.RUSTORE -> "RUSTORE"
231
+ Store.UNKNOWN_STORE -> "UNKNOWN_STORE"
232
+ }
233
+
234
+ private fun ownershipString(o: OwnershipType): String = when (o) {
235
+ OwnershipType.PURCHASED -> "PURCHASED"
236
+ OwnershipType.FAMILY_SHARED -> "FAMILY_SHARED"
237
+ OwnershipType.UNKNOWN -> "UNKNOWN"
238
+ }
239
+
240
+ private fun packageTypeString(p: PackageType): String = when (p) {
241
+ PackageType.UNKNOWN -> "UNKNOWN"
242
+ PackageType.CUSTOM -> "CUSTOM"
243
+ PackageType.LIFETIME -> "LIFETIME"
244
+ PackageType.ANNUAL -> "ANNUAL"
245
+ PackageType.SIX_MONTH -> "SIX_MONTH"
246
+ PackageType.THREE_MONTH -> "THREE_MONTH"
247
+ PackageType.TWO_MONTH -> "TWO_MONTH"
248
+ PackageType.MONTHLY -> "MONTHLY"
249
+ PackageType.WEEKLY -> "WEEKLY"
250
+ }
251
+
252
+ private fun periodUnitString(u: SubscriptionPeriod.Unit): String = when (u) {
253
+ SubscriptionPeriod.Unit.DAY -> "DAY"
254
+ SubscriptionPeriod.Unit.WEEK -> "WEEK"
255
+ SubscriptionPeriod.Unit.MONTH -> "MONTH"
256
+ SubscriptionPeriod.Unit.YEAR -> "YEAR"
257
+ }
258
+
259
+ private fun periodToISOString(p: SubscriptionPeriod): String {
260
+ val unit = when (p.unit) {
261
+ SubscriptionPeriod.Unit.DAY -> "D"
262
+ SubscriptionPeriod.Unit.WEEK -> "W"
263
+ SubscriptionPeriod.Unit.MONTH -> "M"
264
+ SubscriptionPeriod.Unit.YEAR -> "Y"
265
+ }
266
+ return "P${p.value}$unit"
267
+ }
268
+
269
+ private fun periodMap(p: SubscriptionPeriod): WritableMap {
270
+ val m = Arguments.createMap()
271
+ m.putString("unit", periodUnitString(p.unit))
272
+ m.putInt("value", p.value)
273
+ return m
274
+ }
275
+
276
+ // ----- Primitive helpers -----
277
+
278
+ private fun encodeDateMap(src: Map<String, Date>): WritableMap {
279
+ val m = Arguments.createMap()
280
+ for ((k, v) in src) m.putString(k, iso(v))
281
+ return m
282
+ }
283
+
284
+ private fun stringArray(items: List<String>): WritableArray {
285
+ val arr = Arguments.createArray()
286
+ items.forEach { arr.pushString(it) }
287
+ return arr
288
+ }
289
+
290
+ // Render an arbitrary `Map<String, Any?>` (free-form server-side offering
291
+ // metadata) into a [WritableMap]. We restrict supported types to JSON-
292
+ // serializable scalars + nested maps/arrays; anything else gets dropped to
293
+ // keep the bridge crash-free.
294
+ private fun anyMap(src: Map<String, Any?>): WritableMap {
295
+ val out = Arguments.createMap()
296
+ for ((k, v) in src) putAny(out, k, v)
297
+ return out
298
+ }
299
+
300
+ private fun putAny(map: WritableMap, key: String, value: Any?) {
301
+ when (value) {
302
+ null -> map.putNull(key)
303
+ is Boolean -> map.putBoolean(key, value)
304
+ is Int -> map.putInt(key, value)
305
+ is Long -> map.putDouble(key, value.toDouble())
306
+ is Float -> map.putDouble(key, value.toDouble())
307
+ is Double -> map.putDouble(key, value)
308
+ is String -> map.putString(key, value)
309
+ is Map<*, *> -> {
310
+ @Suppress("UNCHECKED_CAST")
311
+ map.putMap(key, anyMap(value as Map<String, Any?>))
312
+ }
313
+ is List<*> -> {
314
+ val arr = Arguments.createArray()
315
+ value.forEach { pushAny(arr, it) }
316
+ map.putArray(key, arr)
317
+ }
318
+ else -> map.putString(key, value.toString())
319
+ }
320
+ }
321
+
322
+ private fun pushAny(arr: WritableArray, value: Any?) {
323
+ when (value) {
324
+ null -> arr.pushNull()
325
+ is Boolean -> arr.pushBoolean(value)
326
+ is Int -> arr.pushInt(value)
327
+ is Long -> arr.pushDouble(value.toDouble())
328
+ is Double -> arr.pushDouble(value)
329
+ is Float -> arr.pushDouble(value.toDouble())
330
+ is String -> arr.pushString(value)
331
+ is Map<*, *> -> {
332
+ @Suppress("UNCHECKED_CAST")
333
+ arr.pushMap(anyMap(value as Map<String, Any?>))
334
+ }
335
+ is List<*> -> {
336
+ val nested = Arguments.createArray()
337
+ value.forEach { pushAny(nested, it) }
338
+ arr.pushArray(nested)
339
+ }
340
+ else -> arr.pushString(value.toString())
341
+ }
342
+ }
343
+
344
+ // ----- Date formatting -----
345
+ //
346
+ // RC's TS shape uses ISO-8601 with millisecond precision (`2024-01-15T12:34:56.789Z`).
347
+ // We match that exactly so JS consumers parsing via `new Date(...)` see no diff.
348
+ private val isoFormatter: SimpleDateFormat by lazy {
349
+ SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US).apply {
350
+ timeZone = TimeZone.getTimeZone("UTC")
351
+ }
352
+ }
353
+
354
+ private fun iso(date: Date): String = synchronized(isoFormatter) { isoFormatter.format(date) }
355
+
356
+ private fun millis(date: Date): Double = date.time.toDouble()
357
+
358
+ // ----- WritableMap nullable extensions -----
359
+
360
+ private fun WritableMap.putNullableString(key: String, value: String?) {
361
+ if (value == null) putNull(key) else putString(key, value)
362
+ }
363
+
364
+ private fun WritableMap.putNullableDouble(key: String, value: Double?) {
365
+ if (value == null) putNull(key) else putDouble(key, value)
366
+ }
367
+
368
+ private fun WritableMap.putNullableMap(key: String, value: WritableMap?) {
369
+ if (value == null) putNull(key) else putMap(key, value)
370
+ }
371
+ }