@rocapine/react-native-onboarding 1.69.0 → 1.71.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 (59) hide show
  1. package/dist/OnboardingStudioClient.d.ts +4 -4
  2. package/dist/OnboardingStudioClient.js +7 -7
  3. package/dist/OnboardingStudioClient.js.map +1 -1
  4. package/dist/paywalls/PaywallProvider.d.ts +24 -7
  5. package/dist/paywalls/PaywallProvider.d.ts.map +1 -1
  6. package/dist/paywalls/PaywallProvider.js +80 -42
  7. package/dist/paywalls/PaywallProvider.js.map +1 -1
  8. package/dist/paywalls/present.d.ts +60 -14
  9. package/dist/paywalls/present.d.ts.map +1 -1
  10. package/dist/paywalls/present.js +67 -18
  11. package/dist/paywalls/present.js.map +1 -1
  12. package/dist/paywalls/types.d.ts +48 -23
  13. package/dist/paywalls/types.d.ts.map +1 -1
  14. package/dist/paywalls/types.js +0 -7
  15. package/dist/paywalls/types.js.map +1 -1
  16. package/dist/paywalls/usePaywall.d.ts +5 -5
  17. package/dist/paywalls/usePaywall.d.ts.map +1 -1
  18. package/dist/products/adapters/expoIap.d.ts.map +1 -1
  19. package/dist/products/adapters/expoIap.js +118 -11
  20. package/dist/products/adapters/expoIap.js.map +1 -1
  21. package/dist/products/adapters/stripeLink.d.ts +57 -0
  22. package/dist/products/adapters/stripeLink.d.ts.map +1 -0
  23. package/dist/products/adapters/stripeLink.js +158 -0
  24. package/dist/products/adapters/stripeLink.js.map +1 -0
  25. package/dist/products/index.d.ts +4 -1
  26. package/dist/products/index.d.ts.map +1 -1
  27. package/dist/products/index.js +5 -1
  28. package/dist/products/index.js.map +1 -1
  29. package/dist/products/refIdentity.d.ts +3 -0
  30. package/dist/products/refIdentity.d.ts.map +1 -0
  31. package/dist/products/refIdentity.js +37 -0
  32. package/dist/products/refIdentity.js.map +1 -0
  33. package/dist/products/types.d.ts +29 -1
  34. package/dist/products/types.d.ts.map +1 -1
  35. package/dist/products/useProducts.d.ts.map +1 -1
  36. package/dist/products/useProducts.js +7 -1
  37. package/dist/products/useProducts.js.map +1 -1
  38. package/dist/steps/common.types.d.ts +2 -0
  39. package/dist/steps/common.types.d.ts.map +1 -1
  40. package/dist/steps/common.types.js +1 -0
  41. package/dist/steps/common.types.js.map +1 -1
  42. package/package.json +1 -1
  43. package/src/OnboardingStudioClient.ts +7 -7
  44. package/src/paywalls/PaywallProvider.tsx +109 -46
  45. package/src/paywalls/__tests__/getPaywalls.test.ts +15 -12
  46. package/src/paywalls/__tests__/present.test.ts +149 -28
  47. package/src/paywalls/present.ts +74 -21
  48. package/src/paywalls/types.ts +49 -23
  49. package/src/paywalls/usePaywall.ts +5 -5
  50. package/src/products/__tests__/refIdentity.test.ts +44 -0
  51. package/src/products/adapters/__tests__/expoIap.test.ts +210 -0
  52. package/src/products/adapters/__tests__/stripeLink.test.ts +179 -0
  53. package/src/products/adapters/expoIap.ts +121 -5
  54. package/src/products/adapters/stripeLink.ts +245 -0
  55. package/src/products/index.ts +4 -0
  56. package/src/products/refIdentity.ts +33 -0
  57. package/src/products/types.ts +30 -1
  58. package/src/products/useProducts.ts +7 -4
  59. package/src/steps/common.types.ts +3 -0
@@ -6,23 +6,27 @@ import {
6
6
  purchaseOutcomeFromResult,
7
7
  resolvePresentDecision,
8
8
  resolvePresentedOutcome,
9
+ selectActiveProductRuntime,
9
10
  shouldBreakPresentationWedge,
10
11
  shouldRecordPurchaseOutcome,
11
12
  } from "../present";
12
13
  import type { Paywall, PaywallCatalog } from "../types";
13
- import type { PurchaseResult } from "../../products/types";
14
+ import type { ProductRuntime, PurchaseResult } from "../../products/types";
14
15
 
15
- const makePaywall = (overrides: Partial<Paywall> & Pick<Paywall, "id" | "placement">): Paywall => ({
16
+ const makePaywall = (overrides: Partial<Paywall> & Pick<Paywall, "id" | "moment">): Paywall => ({
16
17
  name: overrides.name ?? "Paywall",
18
+ audienceId: overrides.audienceId ?? null,
19
+ audienceName: overrides.audienceName ?? null,
17
20
  elements: overrides.elements ?? [],
21
+ billing: overrides.billing ?? "store",
18
22
  products: overrides.products ?? [],
19
23
  configuration: overrides.configuration ?? null,
20
24
  ...overrides,
21
25
  });
22
26
 
23
27
  const makeCatalog = (paywalls: Paywall[]): PaywallCatalog => ({
24
- metadata: { audienceId: null, audienceName: null, locale: "en", draft: false },
25
- paywalls: Object.fromEntries(paywalls.map((p) => [p.placement, p])),
28
+ metadata: { locale: "en", draft: false },
29
+ paywalls: Object.fromEntries(paywalls.map((p) => [p.moment, p])),
26
30
  fonts: null,
27
31
  });
28
32
 
@@ -32,14 +36,14 @@ describe("collectProductRefs", () => {
32
36
  });
33
37
 
34
38
  it("returns an empty array when no paywall declares any products", () => {
35
- const catalog = makeCatalog([makePaywall({ id: "1", placement: "a", products: [] })]);
39
+ const catalog = makeCatalog([makePaywall({ id: "1", moment: "a", products: [] })]);
36
40
  expect(collectProductRefs(catalog)).toEqual([]);
37
41
  });
38
42
 
39
- it("flattens products from every placement into one array", () => {
43
+ it("flattens products from every moment into one array", () => {
40
44
  const catalog = makeCatalog([
41
- makePaywall({ id: "1", placement: "a", products: [{ key: "yearly", ios: "com.app.yr" }] }),
42
- makePaywall({ id: "2", placement: "b", products: [{ key: "monthly", ios: "com.app.mo" }] }),
45
+ makePaywall({ id: "1", moment: "a", products: [{ key: "yearly", ios: "com.app.yr" }] }),
46
+ makePaywall({ id: "2", moment: "b", products: [{ key: "monthly", ios: "com.app.mo" }] }),
43
47
  ]);
44
48
  expect(collectProductRefs(catalog)).toEqual([
45
49
  { key: "yearly", ios: "com.app.yr" },
@@ -47,11 +51,11 @@ describe("collectProductRefs", () => {
47
51
  ]);
48
52
  });
49
53
 
50
- it("dedupes identical refs (same key + ios + android + compareTo) across placements", () => {
54
+ it("dedupes identical refs (same key + ios + android + compareTo) across moments", () => {
51
55
  const shared = { key: "yearly", ios: "com.app.yr", android: "com.app.yr:p1y" };
52
56
  const catalog = makeCatalog([
53
- makePaywall({ id: "1", placement: "a", products: [shared] }),
54
- makePaywall({ id: "2", placement: "b", products: [shared, { key: "monthly", ios: "com.app.mo" }] }),
57
+ makePaywall({ id: "1", moment: "a", products: [shared] }),
58
+ makePaywall({ id: "2", moment: "b", products: [shared, { key: "monthly", ios: "com.app.mo" }] }),
55
59
  ]);
56
60
  expect(collectProductRefs(catalog)).toEqual([
57
61
  shared,
@@ -63,7 +67,7 @@ describe("collectProductRefs", () => {
63
67
  const catalog = makeCatalog([
64
68
  makePaywall({
65
69
  id: "1",
66
- placement: "a",
70
+ moment: "a",
67
71
  products: [
68
72
  { key: "yearly", ios: "com.app.yr" },
69
73
  { key: "yearly", ios: "com.app.yr", compareTo: "monthly" },
@@ -75,10 +79,10 @@ describe("collectProductRefs", () => {
75
79
  });
76
80
 
77
81
  describe("resolvePresentDecision", () => {
78
- const paywall = makePaywall({ id: "1", placement: "hard_paywall", products: [] });
82
+ const paywall = makePaywall({ id: "1", moment: "hard_paywall", products: [] });
79
83
  const catalog = makeCatalog([paywall]);
80
84
 
81
- it("starts when the placement exists and nothing is currently showing", () => {
85
+ it("starts when the moment exists and nothing is currently showing", () => {
82
86
  const decision = resolvePresentDecision(catalog, null, "hard_paywall");
83
87
  expect(decision).toEqual({ type: "start", paywall });
84
88
  });
@@ -87,24 +91,32 @@ describe("resolvePresentDecision", () => {
87
91
  // used to assert is what made two conditions with OPPOSITE correct responses
88
92
  // — "retry later, the catalog may still arrive" vs "do not retry, something
89
93
  // is stuck" — indistinguishable to the caller.
90
- it("resolves 'unknown-placement' when the placement is absent from the catalog", () => {
94
+ it("resolves 'unknown-moment' when the moment is absent from the catalog", () => {
91
95
  const decision = resolvePresentDecision(catalog, null, "does_not_exist");
92
96
  expect(decision).toEqual({
93
97
  type: "immediate",
94
- result: { status: "error", reason: "unknown-placement" },
98
+ result: { status: "error", reason: "unknown-moment" },
95
99
  });
96
100
  });
97
101
 
98
- it("resolves 'unknown-placement' when the catalog has not resolved yet (null)", () => {
102
+ it("resolves 'unknown-moment' when the catalog has not resolved yet (null)", () => {
99
103
  const decision = resolvePresentDecision(null, null, "hard_paywall");
100
104
  expect(decision).toEqual({
101
105
  type: "immediate",
102
- result: { status: "error", reason: "unknown-placement" },
106
+ result: { status: "error", reason: "unknown-moment" },
103
107
  });
104
108
  });
105
109
 
106
- it("resolves 'already-presenting' AND names the placement that is showing", () => {
107
- // The placement is the difference between "I double-called for the same
110
+ it("reports unknown-moment when the key is absent from the catalog", () => {
111
+ const decision = resolvePresentDecision(catalog, null, "not_a_moment");
112
+ expect(decision).toEqual({
113
+ type: "immediate",
114
+ result: { status: "error", reason: "unknown-moment" },
115
+ });
116
+ });
117
+
118
+ it("resolves 'already-presenting' AND names the moment that is showing", () => {
119
+ // The moment is the difference between "I double-called for the same
108
120
  // one" (caller adds a guard) and "something else is stuck" (caller cannot
109
121
  // fix it) — different diagnoses, so the bare status could serve neither.
110
122
  const decision = resolvePresentDecision(catalog, "already_showing", "hard_paywall");
@@ -113,24 +125,24 @@ describe("resolvePresentDecision", () => {
113
125
  result: {
114
126
  status: "error",
115
127
  reason: "already-presenting",
116
- activePlacement: "already_showing",
128
+ activeMoment: "already_showing",
117
129
  },
118
130
  });
119
131
  });
120
132
 
121
- it("reports 'already-presenting' when the caller re-presents the SAME placement", () => {
133
+ it("reports 'already-presenting' when the caller re-presents the SAME moment", () => {
122
134
  const decision = resolvePresentDecision(catalog, "hard_paywall", "hard_paywall");
123
135
  expect(decision).toEqual({
124
136
  type: "immediate",
125
137
  result: {
126
138
  status: "error",
127
139
  reason: "already-presenting",
128
- activePlacement: "hard_paywall",
140
+ activeMoment: "hard_paywall",
129
141
  },
130
142
  });
131
143
  });
132
144
 
133
- it("the concurrent-present check takes priority over the unknown-placement check", () => {
145
+ it("the concurrent-present check takes priority over the unknown-moment check", () => {
134
146
  // Both conditions are true at once: prove the function still resolves
135
147
  // rather than e.g. throwing on the first branch it happens to hit, and
136
148
  // that the reason reflects the branch actually taken.
@@ -140,7 +152,7 @@ describe("resolvePresentDecision", () => {
140
152
  result: {
141
153
  status: "error",
142
154
  reason: "already-presenting",
143
- activePlacement: "already_showing",
155
+ activeMoment: "already_showing",
144
156
  },
145
157
  });
146
158
  });
@@ -236,7 +248,7 @@ describe("resolvePresentedOutcome", () => {
236
248
  });
237
249
 
238
250
  it("never upgrades a status other than 'dismissed', even if a purchase occurred", () => {
239
- // "error" here means "unknown placement / already showing" (resolvePresentDecision) —
251
+ // "error" here means "unknown moment / already showing" (resolvePresentDecision) —
240
252
  // a purchase outcome must never be allowed to clobber that different meaning.
241
253
  expect(resolvePresentedOutcome({ status: "error" }, "purchased")).toEqual({ status: "error" });
242
254
  });
@@ -260,7 +272,7 @@ describe("shouldRecordPurchaseOutcome", () => {
260
272
  });
261
273
 
262
274
  describe("computeCatalogStatus", () => {
263
- const catalog = makeCatalog([makePaywall({ id: "1", placement: "hard_paywall" })]);
275
+ const catalog = makeCatalog([makePaywall({ id: "1", moment: "hard_paywall" })]);
264
276
 
265
277
  it("is 'loading' before anything arrives", () => {
266
278
  expect(computeCatalogStatus(null, null, true)).toBe("loading");
@@ -279,7 +291,7 @@ describe("computeCatalogStatus", () => {
279
291
  // The state this type exists for. In production the catalog is served
280
292
  // cache-first from a disk key that is NOT scoped by customAudienceParams,
281
293
  // so this catalog may have been resolved under DIFFERENT params and be
282
- // superseded in a moment. A host must be able to tell that a placement
294
+ // superseded in a moment. A host must be able to tell that a moment
283
295
  // missing HERE might simply not have arrived yet.
284
296
  expect(computeCatalogStatus(catalog, null, true)).toBe("revalidating");
285
297
  });
@@ -302,3 +314,112 @@ describe("computeCatalogStatus", () => {
302
314
  expect(new Set([loading, errored, productsPending]).size).toBe(3);
303
315
  });
304
316
  });
317
+
318
+ const runtime = (over: Partial<ProductRuntime> = {}): ProductRuntime => ({
319
+ products: {},
320
+ status: "ready",
321
+ purchasing: false,
322
+ purchase: async () => ({ status: "cancelled" }),
323
+ restore: async () => ({ status: "nothing_to_restore" }),
324
+ ...over,
325
+ });
326
+
327
+ describe("selectActiveProductRuntime", () => {
328
+ const storeRuntime = runtime({ status: "ready" });
329
+ const stripeRuntime = runtime({ status: "loading" });
330
+
331
+ it("publishes the store runtime when billing is 'store'", () => {
332
+ expect(
333
+ selectActiveProductRuntime({
334
+ storeRuntime,
335
+ stripeRuntime,
336
+ billing: "store",
337
+ hasStripeProvider: true,
338
+ hasStoreProvider: true,
339
+ }),
340
+ ).toBe(storeRuntime);
341
+ });
342
+
343
+ it("publishes the store runtime when no paywall is active and both providers exist", () => {
344
+ // Neither is more likely to be "the" right one just because nothing is
345
+ // active yet, so this keeps the pre-existing store-first default.
346
+ expect(
347
+ selectActiveProductRuntime({
348
+ storeRuntime,
349
+ stripeRuntime,
350
+ billing: undefined,
351
+ hasStripeProvider: true,
352
+ hasStoreProvider: true,
353
+ }),
354
+ ).toBe(storeRuntime);
355
+ });
356
+
357
+ it("publishes the store runtime when no paywall is active and neither provider exists", () => {
358
+ expect(
359
+ selectActiveProductRuntime({
360
+ storeRuntime,
361
+ stripeRuntime,
362
+ billing: undefined,
363
+ hasStripeProvider: false,
364
+ hasStoreProvider: false,
365
+ }),
366
+ ).toBe(storeRuntime);
367
+ });
368
+
369
+ it("publishes the stripe runtime when no paywall is active and only a stripe provider was passed", () => {
370
+ // A stripe-only host (no `productProvider` at all) must not sit on an
371
+ // "idle" store runtime forever — see this function's doc for why that
372
+ // used to keep `products.loaded` at "false" for every screen before the
373
+ // first paywall presents.
374
+ expect(
375
+ selectActiveProductRuntime({
376
+ storeRuntime,
377
+ stripeRuntime,
378
+ billing: undefined,
379
+ hasStripeProvider: true,
380
+ hasStoreProvider: false,
381
+ }),
382
+ ).toBe(stripeRuntime);
383
+ });
384
+
385
+ it("publishes the stripe runtime when billing is 'stripe'", () => {
386
+ expect(
387
+ selectActiveProductRuntime({
388
+ storeRuntime,
389
+ stripeRuntime,
390
+ billing: "stripe",
391
+ hasStripeProvider: true,
392
+ hasStoreProvider: true,
393
+ }),
394
+ ).toBe(stripeRuntime);
395
+ });
396
+
397
+ it("falls back to the store runtime when billing is 'stripe' but no stripe provider was passed", () => {
398
+ // Without this, `useProducts` leaves the stripe runtime at "idle" forever
399
+ // (it bails before calling a provider it does not have), `computeIsReady`
400
+ // never turns true, and every paywall silently stops presenting — the same
401
+ // trap `mergeProductRuntimes` documents for its own status merge.
402
+ const idleStripe = runtime({ status: "idle" });
403
+ expect(
404
+ selectActiveProductRuntime({
405
+ storeRuntime,
406
+ stripeRuntime: idleStripe,
407
+ billing: "stripe",
408
+ hasStripeProvider: false,
409
+ hasStoreProvider: true,
410
+ }),
411
+ ).toBe(storeRuntime);
412
+ });
413
+
414
+ it("never merges the two statuses", () => {
415
+ // A store paywall must not be held un-ready by the stripe pass.
416
+ const selected = selectActiveProductRuntime({
417
+ storeRuntime: runtime({ status: "ready" }),
418
+ stripeRuntime: runtime({ status: "loading" }),
419
+ billing: "store",
420
+ hasStripeProvider: true,
421
+ hasStoreProvider: true,
422
+ });
423
+ expect(selected.status).toBe("ready");
424
+ });
425
+ });
@@ -1,12 +1,13 @@
1
1
  import type { Paywall, PaywallCatalog, PresentResult } from "./types";
2
- import type { ProductRef, ProductStatus, PurchaseResult } from "../products/types";
2
+ import type { ProductRef, ProductRuntime, ProductStatus, PurchaseResult } from "../products/types";
3
+ import { productRefIdentity } from "../products/refIdentity";
3
4
 
4
5
  /**
5
6
  * Union of every `paywall.products[]` across the WHOLE catalog, deduplicated
6
7
  * by content — the same identity notion `useProducts`' `refsKey` uses
7
- * (`products/useProducts.ts:41-45`: keyed on ref CONTENT, not array
8
- * identity), so this array is exactly as cheap to pass as a hand-written one:
9
- * `useProducts` resolves it once and does not refetch on every render.
8
+ * (`products/refIdentity.ts`: keyed on ref CONTENT, not array identity), so
9
+ * this array is exactly as cheap to pass as a hand-written one: `useProducts`
10
+ * resolves it once and does not refetch on every render.
10
11
  *
11
12
  * Resolving the whole catalog's union ONCE at load — rather than re-keying
12
13
  * `useProducts` per presented paywall — is the deliberate design decision:
@@ -20,7 +21,7 @@ export const collectProductRefs = (catalog: PaywallCatalog | null): ProductRef[]
20
21
  const refs: ProductRef[] = [];
21
22
  for (const paywall of Object.values(catalog.paywalls)) {
22
23
  for (const ref of paywall.products) {
23
- const identity = `${ref.key}|${ref.ios ?? ""}|${ref.android ?? ""}|${ref.compareTo ?? ""}`;
24
+ const identity = productRefIdentity(ref);
24
25
  if (seen.has(identity)) continue;
25
26
  seen.add(identity);
26
27
  refs.push(ref);
@@ -35,13 +36,13 @@ export type PresentDecision =
35
36
  | { type: "immediate"; result: PresentResult };
36
37
 
37
38
  /**
38
- * `present(placement)`'s edge-case decision, extracted so both documented
39
+ * `present(moment)`'s edge-case decision, extracted so both documented
39
40
  * edge cases are covered by an importable test rather than only inspection:
40
41
  *
41
- * - **Unknown placement** (absent from the catalog, or the catalog hasn't
42
- * resolved yet — both look identical here: `catalog?.paywalls[placement]`
42
+ * - **Unknown moment** (absent from the catalog, or the catalog hasn't
43
+ * resolved yet — both look identical here: `catalog?.paywalls[moment]`
43
44
  * is `undefined` either way) → resolves `"error"`, never throws. A missing
44
- * placement must not crash a host app mid-flow.
45
+ * moment must not crash a host app mid-flow.
45
46
  * - **`present()` called while another paywall is already showing** → also
46
47
  * resolves `"error"` immediately, leaving the in-progress presentation
47
48
  * untouched. This SDK shows one paywall at a time. Silently replacing the
@@ -49,23 +50,23 @@ export type PresentDecision =
49
50
  * resolved — that caller hangs forever); queueing the new request would
50
51
  * need its own cancellation/timeout story that nothing here asks for.
51
52
  * Resolving the new call immediately applies the same "resolve, don't
52
- * throw" contract as the unknown-placement case, consistently.
53
+ * throw" contract as the unknown-moment case, consistently.
53
54
  */
54
55
  export const resolvePresentDecision = (
55
56
  catalog: PaywallCatalog | null,
56
- activePlacement: string | null,
57
- placement: string
57
+ activeMoment: string | null,
58
+ moment: string
58
59
  ): PresentDecision => {
59
- if (activePlacement !== null) {
60
- // `activePlacement` is echoed back deliberately — see PresentErrorReason.
60
+ if (activeMoment !== null) {
61
+ // `activeMoment` is echoed back deliberately — see PresentErrorReason.
61
62
  return {
62
63
  type: "immediate",
63
- result: { status: "error", reason: "already-presenting", activePlacement },
64
+ result: { status: "error", reason: "already-presenting", activeMoment },
64
65
  };
65
66
  }
66
- const paywall = catalog?.paywalls[placement];
67
+ const paywall = catalog?.paywalls[moment];
67
68
  if (!paywall) {
68
- return { type: "immediate", result: { status: "error", reason: "unknown-placement" } };
69
+ return { type: "immediate", result: { status: "error", reason: "unknown-moment" } };
69
70
  }
70
71
  return { type: "start", paywall };
71
72
  };
@@ -116,7 +117,7 @@ export const shouldBreakPresentationWedge = (
116
117
  * sending volatile params gets an instantly-available catalog that was resolved
117
118
  * under DIFFERENT params, with a fresh fetch in flight behind it. That catalog
118
119
  * is present and non-null, so it reads as ready — and a host gating on
119
- * `catalog.paywalls[placement]` can conclude the placement does not exist and
120
+ * `catalog.paywalls[moment]` can conclude the moment does not exist and
120
121
  * route away, milliseconds before the correct catalog arrives.
121
122
  *
122
123
  * Observed consequence, reported from a production pilot: for an audience gated
@@ -124,7 +125,7 @@ export const shouldBreakPresentationWedge = (
124
125
  * first becomes eligible is served the PRE-threshold catalog, so the arm under
125
126
  * test loses exactly the launch that matters. `"revalidating"` is how a host
126
127
  * distinguishes "this catalog is final" from "this catalog may be superseded in
127
- * a moment", and therefore whether a missing placement means absent or not-yet.
128
+ * a moment", and therefore whether a missing moment means absent or not-yet.
128
129
  *
129
130
  * A present catalog outranks an error deliberately: if a background
130
131
  * revalidation fails, react-query keeps the cached `data` AND sets `error`, and
@@ -166,7 +167,7 @@ export const computeIsReady = (
166
167
  * `"error"` are not: they don't describe what happened to the STORE PURCHASE
167
168
  * in a way that should override how the paywall itself reports closing (an
168
169
  * `"error"` here would collide with `PresentResult`'s existing `"error"`,
169
- * which means something structurally different — an unknown placement or a
170
+ * which means something structurally different — an unknown moment or a
170
171
  * mistimed `present()` call, not a failed purchase attempt).
171
172
  */
172
173
  export type PurchaseOutcomeDuringPresentation = "purchased" | "cancelled" | null;
@@ -207,7 +208,7 @@ export const resolvePresentedOutcome = (
207
208
  * generation again after — only an unchanged generation means the write is
208
209
  * still for the presentation that started it.
209
210
  *
210
- * A monotonic counter, not the placement string: the same placement can
211
+ * A monotonic counter, not the moment string: the same moment can
211
212
  * legitimately be presented twice in a row, and a string comparison would
212
213
  * let a stale write from the FIRST of those two land in the second.
213
214
  */
@@ -215,3 +216,55 @@ export const shouldRecordPurchaseOutcome = (
215
216
  startedInGeneration: number,
216
217
  currentGeneration: number
217
218
  ): boolean => startedInGeneration === currentGeneration;
219
+
220
+ /**
221
+ * Which of the two resolved product runtimes to publish.
222
+ *
223
+ * `PaywallProvider` resolves the catalog's product union TWICE — once through
224
+ * the host's store adapter, once through its Stripe adapter — because the
225
+ * runtime is published as one map keyed by product key, and a `store` paywall
226
+ * and a `stripe` paywall declaring the same key would otherwise fight over
227
+ * `product.<key>.price`. Only one paywall is ever presented at a time
228
+ * (`activeMoment`), so "the active one" is unambiguous.
229
+ *
230
+ * This is cheap rather than wasteful because the Stripe pass performs no
231
+ * network call (`stripeLinkProductProvider`) and each provider silently drops
232
+ * refs it cannot resolve, so neither pass does work for the other's paywalls.
233
+ *
234
+ * The statuses are deliberately NOT merged. Merging is the trap
235
+ * `mergeProductRuntimes` documents from the other direction: an `"idle"`
236
+ * runtime with nothing to wait for drags a `"ready"` one down and closes every
237
+ * `products.loaded` gate for the life of the process.
238
+ *
239
+ * `hasStripeProvider` is the guard for a misconfigured host: a `stripe`
240
+ * paywall with no `stripeProductProvider` passed leaves `useProducts` at
241
+ * `"idle"` forever (it bails before calling a provider it does not have), so
242
+ * `computeIsReady` would never turn true and paywalls would stop appearing
243
+ * with no error. Falling back to the store runtime keeps the app alive; the
244
+ * caller warns.
245
+ *
246
+ * `billing` is `undefined` whenever no paywall is active (`activePaywall` is
247
+ * null before the first `present()`, and after every `complete()`) — there is
248
+ * no `.billing` to read yet. That USED to fall through to the store runtime
249
+ * unconditionally, which meant a host that passes ONLY
250
+ * `stripeProductProvider` (no `productProvider` at all) sat on an `"idle"`
251
+ * `storeRuntime` — forever, for every screen before a paywall first presents,
252
+ * since nothing ever calls a `present()` to select the stripe runtime instead
253
+ * — so `products.loaded` read `"false"` everywhere and `isReady` never turned
254
+ * true. `hasStoreProvider` closes that gap: with no active paywall, prefer
255
+ * whichever provider was actually SUPPLIED. When both or neither exist there
256
+ * is no asymmetry to correct, so this keeps the pre-existing store-first
257
+ * default — unchanged for every host that already worked.
258
+ */
259
+ export const selectActiveProductRuntime = (args: {
260
+ storeRuntime: ProductRuntime;
261
+ stripeRuntime: ProductRuntime;
262
+ billing: "store" | "stripe" | undefined;
263
+ hasStripeProvider: boolean;
264
+ hasStoreProvider: boolean;
265
+ }): ProductRuntime => {
266
+ if (args.billing === "stripe" && args.hasStripeProvider) return args.stripeRuntime;
267
+ if (args.billing !== undefined) return args.storeRuntime;
268
+ if (args.hasStripeProvider && !args.hasStoreProvider) return args.stripeRuntime;
269
+ return args.storeRuntime;
270
+ };
@@ -1,23 +1,46 @@
1
+ import type { StripeProductRef } from "../products/types";
2
+
1
3
  /**
2
4
  * Response types for `OnboardingStudioClient.getPaywalls()` (spec §6.1). The
3
- * endpoint returns every placement for the project/audience/locale in one
5
+ * endpoint returns every moment for the project/audience/locale in one
4
6
  * payload — a paywall must render the instant the user taps upgrade, and a
5
7
  * network round-trip at that moment is a conversion bug, so there is no
6
- * per-placement fetch in the common path (see `PaywallOptions.placement`).
8
+ * per-moment fetch in the common path (see `PaywallOptions.moment`).
7
9
  */
8
10
 
9
- /** A single studio-authored paywall, keyed by `placement` in `PaywallCatalog.paywalls`. */
11
+ /**
12
+ * A single studio-authored paywall, keyed by `moment` in `PaywallCatalog.paywalls`.
13
+ *
14
+ * `audienceId`/`audienceName` are resolved PER MOMENT, not once for the whole
15
+ * catalog: each moment (an onboarding-end upsell, a settings-screen upgrade,
16
+ * …) runs its own independent audience waterfall, so two entries in the same
17
+ * catalog can legitimately have matched different audiences. That is why
18
+ * these fields live here and not on `PaywallCatalog.metadata`.
19
+ */
10
20
  export type Paywall = {
11
21
  id: string;
12
22
  name: string;
13
- placement: string;
23
+ moment: string;
24
+ audienceId: number | null;
25
+ audienceName: string | null;
14
26
  /** UIElement[] — parsed by the UI adapter (Task 7's `ScreenElementsSchema`), not here. */
15
27
  elements: unknown[];
28
+ /**
29
+ * Which billing path this paywall's buy actions take. `"store"` uses the
30
+ * host's store adapter (`PaywallProvider`'s `productProvider`); `"stripe"`
31
+ * uses `PaywallProvider`'s `stripeProductProvider` and opens a Payment Link.
32
+ *
33
+ * A property of the PAYWALL, not the moment or the project, so one moment
34
+ * audience can weight a `store` variant against a `stripe` variant and ramp
35
+ * the billing change as an A/B test.
36
+ */
37
+ billing: "store" | "stripe";
16
38
  products: Array<{
17
39
  key: string;
18
40
  ios?: string;
19
41
  android?: string;
20
42
  compareTo?: string;
43
+ stripe?: StripeProductRef;
21
44
  }>;
22
45
  configuration: Record<string, unknown> | null;
23
46
  };
@@ -25,12 +48,10 @@ export type Paywall = {
25
48
  /** Full response body of `GET get-paywalls`. */
26
49
  export type PaywallCatalog = {
27
50
  metadata: {
28
- audienceId: number | null;
29
- audienceName: string | null;
30
51
  locale: string | null;
31
52
  draft: boolean;
32
53
  };
33
- /** Keyed by placement, not by paywall id. */
54
+ /** Keyed by moment, not by paywall id. */
34
55
  paywalls: Record<string, Paywall>;
35
56
  fonts: Record<string, unknown> | null;
36
57
  };
@@ -38,14 +59,14 @@ export type PaywallCatalog = {
38
59
  /**
39
60
  * Options for `OnboardingStudioClient.getPaywalls()`.
40
61
  *
41
- * `placement` narrows the response to a single paywall — the exception, not
42
- * the default. Omit it to get every placement in one round-trip, which is
62
+ * `moment` narrows the response to a single paywall — the exception, not
63
+ * the default. Omit it to get every moment in one round-trip, which is
43
64
  * what lets a paywall render instantly at buy-tap time (see the module doc
44
65
  * above).
45
66
  */
46
67
  export type PaywallOptions = {
47
68
  locale?: string;
48
- placement?: string;
69
+ moment?: string;
49
70
  };
50
71
 
51
72
  /**
@@ -53,22 +74,27 @@ export type PaywallOptions = {
53
74
  * trio (`ONBS-Onboarding-Id` / `ONBS-Audience-Id` / `ONBS-Onboarding-Name`) —
54
75
  * the paywall endpoint has no single onboarding/audience-name concept and
55
76
  * instead reports which paywall ids came back.
77
+ *
78
+ * `ONBS-Audience-Ids` is plural (comma-separated) and parallel in order to
79
+ * `ONBS-Paywall-Ids`: each moment resolves its own audience waterfall (see
80
+ * `Paywall.audienceId`), so there is no single catalog-level audience left
81
+ * to report under a singular header.
56
82
  */
57
83
  export interface GetPaywallsResponseHeaders {
58
- "ONBS-Audience-Id": string | null;
84
+ "ONBS-Audience-Ids": string | null;
59
85
  "ONBS-Paywall-Ids": string | null;
60
86
  }
61
87
 
62
88
  /**
63
- * Resolved outcome of a `usePaywall().present(placement)` call (spec §7).
89
+ * Resolved outcome of a `usePaywall().present(moment)` call (spec §7).
64
90
  * Closed union, not the open-ended `ScreenHost.CompleteOutcome` — a
65
91
  * presentation ends in exactly one of these four ways, and `present()`'s
66
92
  * caller (typically an `await`) should be able to switch over it exhaustively.
67
93
  *
68
- * `"error"` covers both: (a) `placement` is absent from the catalog (or the
94
+ * `"error"` covers both: (a) `moment` is absent from the catalog (or the
69
95
  * catalog hasn't resolved yet), and (b) `present()` was called while another
70
96
  * paywall is already being presented. Both resolve rather than throw — a
71
- * missing placement or a mistimed call must not crash a host app mid-flow.
97
+ * missing moment or a mistimed call must not crash a host app mid-flow.
72
98
  * See `resolvePresentDecision` in `present.ts` for the exact decision logic.
73
99
  *
74
100
  * `"purchased"` / `"cancelled"` are NOT produced by any ButtonAction directly
@@ -99,18 +125,18 @@ export interface GetPaywallsResponseHeaders {
99
125
  * absent on every other status.
100
126
  *
101
127
  * This exists because `"error"` alone conflates conditions whose correct
102
- * recovery is OPPOSITE. `"unknown-placement"` means the catalog may simply not
128
+ * recovery is OPPOSITE. `"unknown-moment"` means the catalog may simply not
103
129
  * have arrived yet, so retrying later is right. `"already-presenting"` means a
104
130
  * presentation is in progress, so retrying is wrong and something may be
105
131
  * stuck. A caller given only the status can act correctly on neither, and two
106
132
  * separate multi-hour production investigations were spent reconstructing by
107
133
  * elimination what the SDK already knew here.
108
134
  *
109
- * - `unknown-placement` — absent from the catalog, or the catalog has not
135
+ * - `unknown-moment` — absent from the catalog, or the catalog has not
110
136
  * resolved yet. The two are indistinguishable at this layer, and both are
111
137
  * worth retrying once `usePaywall().isReady` is true.
112
- * - `already-presenting` — one paywall shows at a time. `activePlacement` on
113
- * the result names the one that IS showing: the same placement means the
138
+ * - `already-presenting` — one paywall shows at a time. `activeMoment` on
139
+ * the result names the one that IS showing: the same moment means the
114
140
  * caller double-called and wants its own in-flight guard, a different one
115
141
  * means something else holds the surface.
116
142
  * - `parse-error` — `elements` failed the UI schema, so the Modal was never
@@ -125,11 +151,11 @@ export interface GetPaywallsResponseHeaders {
125
151
  * keep the surface usable. In practice the platform refused to present
126
152
  * (iOS will not present over an already-presenting view controller — another
127
153
  * Modal, a `presentation: "modal"` route, a StoreKit alert).
128
- * - `paywall-disappeared` — the placement vanished from the catalog while it
154
+ * - `paywall-disappeared` — the moment vanished from the catalog while it
129
155
  * was on screen (a studio publish, or a query-key change mid-presentation).
130
156
  */
131
157
  export type PresentErrorReason =
132
- | "unknown-placement"
158
+ | "unknown-moment"
133
159
  | "already-presenting"
134
160
  | "parse-error"
135
161
  | "render-error"
@@ -141,9 +167,9 @@ export type PresentResult = {
141
167
  /** Why it failed. Set on `"error"` only — see `PresentErrorReason`. */
142
168
  reason?: PresentErrorReason;
143
169
  /**
144
- * The placement currently being presented. Set only alongside
145
- * `reason: "already-presenting"`, where knowing WHICH placement holds the
170
+ * The moment currently being presented. Set only alongside
171
+ * `reason: "already-presenting"`, where knowing WHICH moment holds the
146
172
  * surface is what distinguishes a caller double-call from an unrelated stall.
147
173
  */
148
- activePlacement?: string;
174
+ activeMoment?: string;
149
175
  };
@@ -6,27 +6,27 @@ import type { ProductStatus } from "../products/types";
6
6
 
7
7
  export type UsePaywallResult = {
8
8
  /**
9
- * Present a paywall by placement. Never fetches — the catalog and its
9
+ * Present a paywall by moment. Never fetches — the catalog and its
10
10
  * products are already resolved (or resolving) from `PaywallProvider`
11
11
  * mount, which is the whole point: a paywall must render the instant the
12
12
  * user taps upgrade.
13
13
  *
14
- * Resolves `{ status: "error" }` — never throws — when `placement` is
14
+ * Resolves `{ status: "error" }` — never throws — when `moment` is
15
15
  * absent from the catalog, or when another paywall is already being
16
16
  * presented. See `resolvePresentDecision` in `present.ts`.
17
17
  */
18
- present: (placement: string) => Promise<PresentResult>;
18
+ present: (moment: string) => Promise<PresentResult>;
19
19
  /**
20
20
  * `true` once BOTH the catalog and its products have resolved — the one
21
21
  * flag meaning "calling `present()` now will not show a spinner".
22
22
  */
23
23
  isReady: boolean;
24
- /** The full resolved catalog (every placement), or `null` before it loads. */
24
+ /** The full resolved catalog (every moment), or `null` before it loads. */
25
25
  catalog: PaywallCatalog | null;
26
26
  /**
27
27
  * What the catalog is doing, for hosts that need more than `isReady`'s one
28
28
  * bit. `"revalidating"` is the important one: the catalog on hand was served
29
- * from disk and a fresh fetch is in flight, so a placement missing from it may
29
+ * from disk and a fresh fetch is in flight, so a moment missing from it may
30
30
  * simply not have arrived yet. Gate "fall back to another engine" on
31
31
  * `"ready"`, not merely on a non-null catalog.
32
32
  */