@rovenue/react-native-sdk 0.23.1 → 0.26.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.
@@ -13,6 +13,12 @@
13
13
 
14
14
  package dev.rovenue.sdkrn
15
15
 
16
+ import dev.rovenue.sdk.funnelui.presentation.FunnelPresentHandlers
17
+ import dev.rovenue.sdk.funnelui.presentation.FunnelPresentResult
18
+ import dev.rovenue.sdk.funnelui.presentation.FunnelPresentationEnter
19
+ import dev.rovenue.sdk.funnelui.presentation.FunnelPresentationOverride
20
+ import dev.rovenue.sdk.funnelui.presentation.FunnelPresentationStyle
21
+ import dev.rovenue.sdk.funnelui.presentation.FunnelSheetDetent
16
22
  import android.content.Context
17
23
  import android.content.pm.PackageManager
18
24
  import android.os.Build
@@ -169,8 +175,8 @@ class RovenueModule : Module() {
169
175
  val u = Rovenue.shared.currentUser()
170
176
  mapOf("rovenueId" to u.rovenueId, "appUserId" to u.appUserId)
171
177
  }
172
- AsyncFunction("identify") Coroutine { appUserId: String ->
173
- try { Rovenue.shared.identify(appUserId) } catch (e: Throwable) { throw codedError(e) }
178
+ AsyncFunction("identify") Coroutine { appUserId: String, identityToken: String ->
179
+ try { Rovenue.shared.identify(appUserId, identityToken) } catch (e: Throwable) { throw codedError(e) }
174
180
  }
175
181
  // Returns at once on Android: the façade posts in the background and
176
182
  // logs a failure rather than throwing.
@@ -248,6 +254,21 @@ class RovenueModule : Module() {
248
254
  throw codedError(e)
249
255
  }
250
256
  }
257
+ AsyncFunction("preloadPlacement") Coroutine { placementId: String, locale: String? ->
258
+ Rovenue.shared.preloadPlacement(placementId, locale)
259
+ }
260
+ AsyncFunction("presentFunnel") Coroutine { presentationId: String, placement: String, locale: String?, styleJson: String?, callbacks: Map<String, Boolean> ->
261
+ val activity = appContext.currentActivity
262
+ if (activity == null) {
263
+ mapOf("result" to "unavailable", "purchased" to false, "reason" to FunnelPresentResult.NO_PRESENTER)
264
+ } else {
265
+ presentFunnelFrom(activity, presentationId, placement, locale, styleJson, callbacks)
266
+ }
267
+ }
268
+ AsyncFunction("resolveFunnelPurchaseRequest") { requestId: String, purchased: Boolean ->
269
+ funnelPurchaseRequests.remove(requestId)?.complete(purchased)
270
+ Unit
271
+ }
251
272
  AsyncFunction("purchase") Coroutine { productId: String, productType: String, promotionalOfferId: String?, basePlanId: String?, offerId: String? ->
252
273
  // promotionalOfferId is iOS-only (ignored here). basePlanId/offerId select a Play subscription offer.
253
274
  // Play Billing needs the foreground Activity to launch the flow.
@@ -369,7 +390,7 @@ class RovenueModule : Module() {
369
390
  }
370
391
 
371
392
  // ---------------- Events ----------------
372
- Events("onChange", "onLog", "onFunnelClaimResolved")
393
+ Events("onChange", "onLog", "onFunnelClaimResolved", "onFunnelPresentEvent")
373
394
 
374
395
  OnStartObserving {
375
396
  changesJob = scope.launch {
@@ -403,6 +424,79 @@ class RovenueModule : Module() {
403
424
  }
404
425
  }
405
426
 
427
+ // ---- presentFunnel (spec 2026-11-03-funnel-presentation §2a)
428
+
429
+ /** Purchase / restore requests a presented funnel waits on, answered by `resolveFunnelPurchaseRequest`. */
430
+ private val funnelPurchaseRequests = java.util.concurrent.ConcurrentHashMap<String, kotlinx.coroutines.CompletableDeferred<Boolean>>()
431
+ private val funnelRequestSerial = java.util.concurrent.atomic.AtomicLong()
432
+
433
+ private suspend fun askJs(event: Map<String, Any?>): Boolean {
434
+ val requestId = "r${funnelRequestSerial.incrementAndGet()}"
435
+ val answer = kotlinx.coroutines.CompletableDeferred<Boolean>()
436
+ funnelPurchaseRequests[requestId] = answer
437
+ sendEvent("onFunnelPresentEvent", event + ("requestId" to requestId))
438
+ return answer.await()
439
+ }
440
+
441
+ private suspend fun presentFunnelFrom(
442
+ activity: android.app.Activity,
443
+ presentationId: String,
444
+ placement: String,
445
+ locale: String?,
446
+ styleJson: String?,
447
+ callbacks: Map<String, Boolean>,
448
+ ): Map<String, Any?> {
449
+ val emit: (Map<String, Any?>) -> Unit = { sendEvent("onFunnelPresentEvent", it + ("presentationId" to presentationId)) }
450
+ val handlers = FunnelPresentHandlers(
451
+ onPurchase = if (callbacks["onPurchase"] == true) {
452
+ { packageId -> askJs(mapOf("presentationId" to presentationId, "type" to "purchaseRequest", "packageId" to packageId)) }
453
+ } else null,
454
+ onRestore = if (callbacks["onRestore"] == true) {
455
+ { askJs(mapOf("presentationId" to presentationId, "type" to "restoreRequest")) }
456
+ } else null,
457
+ onPurchaseError = { error -> emit(mapOf("type" to "purchaseError", "error" to errorDto(error))) },
458
+ onUrl = if (callbacks["onUrl"] == true) { url -> emit(mapOf("type" to "url", "url" to url)) } else null,
459
+ onCustomAction = if (callbacks["onCustomAction"] == true) {
460
+ { id, params -> emit(mapOf("type" to "customAction", "actionId" to id, "paramsJson" to params)) }
461
+ } else null,
462
+ )
463
+ val result = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Main) {
464
+ Rovenue.shared.presentFunnel(activity, placement, locale, presentationOverride(styleJson), handlers)
465
+ }
466
+ return mapOf(
467
+ "result" to result.result.wire,
468
+ "purchased" to result.purchased,
469
+ "customerInfo" to result.customerInfo?.let(::dtoFromPurchaseResult),
470
+ "lastError" to result.lastError?.let(::errorDto),
471
+ "reason" to result.reason,
472
+ )
473
+ }
474
+
475
+ private fun errorDto(error: dev.rovenue.sdk.RovenueException): Map<String, Any?> = mapOf(
476
+ "kind" to error.kind.name,
477
+ "message" to error.message,
478
+ "serverCode" to error.serverCode,
479
+ "httpStatus" to error.httpStatus,
480
+ "retryable" to error.isRetryable,
481
+ ).filterValues { it != null }
482
+
483
+ /** The JS `style` object (a partial presentation, JSON). */
484
+ private fun presentationOverride(json: String?): FunnelPresentationOverride? {
485
+ val o = json?.let { runCatching { org.json.JSONObject(it) }.getOrNull() } ?: return null
486
+ fun str(key: String) = if (o.has(key) && !o.isNull(key)) o.getString(key) else null
487
+ fun bool(key: String) = if (o.has(key) && !o.isNull(key)) o.getBoolean(key) else null
488
+ val detents = o.optJSONArray("detents")?.let { array ->
489
+ (0 until array.length()).mapNotNull { i -> FunnelSheetDetent.entries.firstOrNull { it.name.equals(array.getString(i), ignoreCase = true) } }
490
+ }
491
+ return FunnelPresentationOverride(
492
+ style = str("style")?.let { wire -> FunnelPresentationStyle.entries.firstOrNull { it.name.replace("_", "").equals(wire, ignoreCase = true) } },
493
+ enter = str("enter")?.let { wire -> FunnelPresentationEnter.entries.firstOrNull { it.name.equals(wire, ignoreCase = true) } },
494
+ detents = detents,
495
+ dismissible = bool("dismissible"),
496
+ closeOnPurchase = bool("closeOnPurchase"),
497
+ )
498
+ }
499
+
406
500
  private fun dtoFromExperimentAssignment(a: ExperimentAssignment): Map<String, Any?> = mapOf(
407
501
  "experimentId" to a.experimentId,
408
502
  "key" to a.key,
@@ -0,0 +1,7 @@
1
+ // src/version.ts
2
+ var SDK_VERSION = "0.26.0";
3
+
4
+ export {
5
+ SDK_VERSION
6
+ };
7
+ //# sourceMappingURL=chunk-BORMJJTI.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/version.ts"],"sourcesContent":["/** This package's version, as published — the same string `package.json` carries. */\nexport const SDK_VERSION = \"0.23.1\";\n"],"mappings":";AACO,IAAM,cAAc;","names":[]}
1
+ {"version":3,"sources":["../src/version.ts"],"sourcesContent":["/** This package's version, as published — the same string `package.json` carries. */\nexport const SDK_VERSION = \"0.26.0\";\n"],"mappings":";AACO,IAAM,cAAc;","names":[]}
package/dist/index.cjs CHANGED
@@ -28,6 +28,7 @@ __export(index_exports, {
28
28
  SDK_VERSION: () => SDK_VERSION,
29
29
  extractFunnelToken: () => extractFunnelToken,
30
30
  mapNativeError: () => mapNativeError,
31
+ presentFunnel: () => presentFunnel,
31
32
  serializeEnvelope: () => serializeEnvelope,
32
33
  stripUndefined: () => stripUndefined,
33
34
  useCurrentUser: () => useCurrentUser,
@@ -42,7 +43,7 @@ __export(index_exports, {
42
43
  module.exports = __toCommonJS(index_exports);
43
44
 
44
45
  // src/version.ts
45
- var SDK_VERSION = "0.23.1";
46
+ var SDK_VERSION = "0.26.0";
46
47
 
47
48
  // src/events.ts
48
49
  var EVENT_WIRE_VERSION = 1;
@@ -93,6 +94,10 @@ var ERROR_KINDS = [
93
94
  // The server cannot resolve this device's subscriber (corrupt merge
94
95
  // chain, backend SUBSCRIBER_UNRESOLVABLE). Terminal: not retryable.
95
96
  "SubscriberUnresolvable",
97
+ // The server refused the identity token identify() sent (backend
98
+ // IDENTITY_TOKEN_INVALID, 401): expired, or minted for another user,
99
+ // project or environment. Not retryable: identify again with a fresh token.
100
+ "IdentityTokenInvalid",
96
101
  "PurchaseCanceled",
97
102
  "ProductNotAvailable",
98
103
  "AlreadyOwned",
@@ -565,8 +570,8 @@ async function call2(fn) {
565
570
  async function currentUser() {
566
571
  return call2(() => getNative().currentUser());
567
572
  }
568
- async function identify(appUserId) {
569
- return call2(() => getNative().identify(appUserId));
573
+ async function identify(appUserId, identityToken) {
574
+ return call2(() => getNative().identify(appUserId, identityToken));
570
575
  }
571
576
  async function logOut() {
572
577
  return call2(() => getNative().logOut());
@@ -709,10 +714,81 @@ async function call6(fn) {
709
714
  throw fromNativeError(e);
710
715
  }
711
716
  }
717
+ async function preloadPlacement(placementId, locale) {
718
+ try {
719
+ await getNative().preloadPlacement(placementId, locale ?? null);
720
+ } catch {
721
+ }
722
+ }
712
723
  async function setFallbackPlacements(json) {
713
724
  return call6(() => getNative().setFallbackPlacements(json));
714
725
  }
715
726
 
727
+ // src/api/present-funnel.ts
728
+ var FUNNEL_PRESENT_EVENT = "onFunnelPresentEvent";
729
+ var presentationSerial = 0;
730
+ function toError(dto) {
731
+ const mapped = fromNativeError(Object.assign(new Error(dto.message), { code: dto.kind }));
732
+ if (mapped instanceof RovenueError) {
733
+ return new RovenueError(mapped.kind, dto.message, {
734
+ serverCode: dto.serverCode ?? void 0,
735
+ httpStatus: dto.httpStatus ?? void 0,
736
+ retryable: dto.retryable
737
+ });
738
+ }
739
+ return new RovenueError("Internal", dto.message);
740
+ }
741
+ async function presentFunnel(options) {
742
+ const native = getNative();
743
+ presentationSerial += 1;
744
+ const presentationId = `p${Date.now().toString(36)}${presentationSerial}`;
745
+ const subscription3 = getEmitter().addListener(FUNNEL_PRESENT_EVENT, (event) => {
746
+ if (event.presentationId !== presentationId) return;
747
+ switch (event.type) {
748
+ case "purchaseRequest":
749
+ case "restoreRequest": {
750
+ const run = event.type === "purchaseRequest" ? options.onPurchase?.(event.packageId) : options.onRestore?.();
751
+ Promise.resolve(run).then((answer) => native.resolveFunnelPurchaseRequest(event.requestId, Boolean(answer?.purchased))).catch(() => native.resolveFunnelPurchaseRequest(event.requestId, false));
752
+ break;
753
+ }
754
+ case "purchaseError":
755
+ options.onPurchaseError?.(toError(event.error));
756
+ break;
757
+ case "url":
758
+ options.onUrl?.(event.url);
759
+ break;
760
+ case "customAction":
761
+ options.onCustomAction?.({ actionId: event.actionId, paramsJson: event.paramsJson });
762
+ break;
763
+ }
764
+ });
765
+ try {
766
+ const dto = await native.presentFunnel(
767
+ presentationId,
768
+ options.placement,
769
+ options.locale ?? null,
770
+ options.style ? JSON.stringify(options.style) : null,
771
+ {
772
+ onPurchase: Boolean(options.onPurchase),
773
+ onRestore: Boolean(options.onRestore),
774
+ onUrl: Boolean(options.onUrl),
775
+ onCustomAction: Boolean(options.onCustomAction)
776
+ }
777
+ );
778
+ return {
779
+ result: dto.result,
780
+ purchased: dto.purchased,
781
+ ...dto.customerInfo ? { customerInfo: dto.customerInfo } : {},
782
+ ...dto.lastError ? { lastError: toError(dto.lastError) } : {},
783
+ ...dto.reason ? { reason: dto.reason } : {}
784
+ };
785
+ } catch (e) {
786
+ throw fromNativeError(e);
787
+ } finally {
788
+ subscription3.remove();
789
+ }
790
+ }
791
+
716
792
  // src/api/events.ts
717
793
  function isoNow2() {
718
794
  return (/* @__PURE__ */ new Date()).toISOString();
@@ -1011,6 +1087,8 @@ var Rovenue = {
1011
1087
  purchase,
1012
1088
  restorePurchases,
1013
1089
  setFallbackPlacements,
1090
+ preloadPlacement,
1091
+ presentFunnel,
1014
1092
  track,
1015
1093
  refreshRemoteConfig,
1016
1094
  getRemoteConfig,
@@ -1064,6 +1142,7 @@ var Rovenue = {
1064
1142
  SDK_VERSION,
1065
1143
  extractFunnelToken,
1066
1144
  mapNativeError,
1145
+ presentFunnel,
1067
1146
  serializeEnvelope,
1068
1147
  stripUndefined,
1069
1148
  useCurrentUser,