@rocapine/react-native-onboarding 1.69.0 → 1.70.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.
- package/dist/OnboardingStudioClient.d.ts +4 -4
- package/dist/OnboardingStudioClient.js +7 -7
- package/dist/OnboardingStudioClient.js.map +1 -1
- package/dist/paywalls/PaywallProvider.d.ts +24 -7
- package/dist/paywalls/PaywallProvider.d.ts.map +1 -1
- package/dist/paywalls/PaywallProvider.js +80 -42
- package/dist/paywalls/PaywallProvider.js.map +1 -1
- package/dist/paywalls/present.d.ts +60 -14
- package/dist/paywalls/present.d.ts.map +1 -1
- package/dist/paywalls/present.js +67 -18
- package/dist/paywalls/present.js.map +1 -1
- package/dist/paywalls/types.d.ts +48 -23
- package/dist/paywalls/types.d.ts.map +1 -1
- package/dist/paywalls/types.js +0 -7
- package/dist/paywalls/types.js.map +1 -1
- package/dist/paywalls/usePaywall.d.ts +5 -5
- package/dist/paywalls/usePaywall.d.ts.map +1 -1
- package/dist/products/adapters/stripeLink.d.ts +57 -0
- package/dist/products/adapters/stripeLink.d.ts.map +1 -0
- package/dist/products/adapters/stripeLink.js +158 -0
- package/dist/products/adapters/stripeLink.js.map +1 -0
- package/dist/products/index.d.ts +4 -1
- package/dist/products/index.d.ts.map +1 -1
- package/dist/products/index.js +5 -1
- package/dist/products/index.js.map +1 -1
- package/dist/products/refIdentity.d.ts +3 -0
- package/dist/products/refIdentity.d.ts.map +1 -0
- package/dist/products/refIdentity.js +37 -0
- package/dist/products/refIdentity.js.map +1 -0
- package/dist/products/types.d.ts +29 -1
- package/dist/products/types.d.ts.map +1 -1
- package/dist/products/useProducts.d.ts.map +1 -1
- package/dist/products/useProducts.js +7 -1
- package/dist/products/useProducts.js.map +1 -1
- package/package.json +1 -1
- package/src/OnboardingStudioClient.ts +7 -7
- package/src/paywalls/PaywallProvider.tsx +109 -46
- package/src/paywalls/__tests__/getPaywalls.test.ts +15 -12
- package/src/paywalls/__tests__/present.test.ts +149 -28
- package/src/paywalls/present.ts +74 -21
- package/src/paywalls/types.ts +49 -23
- package/src/paywalls/usePaywall.ts +5 -5
- package/src/products/__tests__/refIdentity.test.ts +44 -0
- package/src/products/adapters/__tests__/stripeLink.test.ts +179 -0
- package/src/products/adapters/stripeLink.ts +245 -0
- package/src/products/index.ts +4 -0
- package/src/products/refIdentity.ts +33 -0
- package/src/products/types.ts +30 -1
- package/src/products/useProducts.ts +7 -4
|
@@ -29,13 +29,16 @@ const CUSTOM_KEY = "v2";
|
|
|
29
29
|
const CUSTOM_CACHE_KEY = "rocapine-paywalls-sdk-v2";
|
|
30
30
|
|
|
31
31
|
const catalog: PaywallCatalog = {
|
|
32
|
-
metadata: {
|
|
32
|
+
metadata: { locale: "en", draft: false },
|
|
33
33
|
paywalls: {
|
|
34
34
|
onboarding_end: {
|
|
35
35
|
id: "pw1",
|
|
36
36
|
name: "End paywall",
|
|
37
|
-
|
|
37
|
+
moment: "onboarding_end",
|
|
38
|
+
audienceId: 1,
|
|
39
|
+
audienceName: "default",
|
|
38
40
|
elements: [],
|
|
41
|
+
billing: "store",
|
|
39
42
|
products: [{ key: "monthly", ios: "com.app.monthly" }],
|
|
40
43
|
configuration: null,
|
|
41
44
|
},
|
|
@@ -70,10 +73,10 @@ describe("OnboardingStudioClient.getPaywalls", () => {
|
|
|
70
73
|
vi.unstubAllGlobals();
|
|
71
74
|
});
|
|
72
75
|
|
|
73
|
-
it("builds the URL against get-paywalls with projectId/platform/appVersion/locale, and omits
|
|
76
|
+
it("builds the URL against get-paywalls with projectId/platform/appVersion/locale, and omits moment by default", async () => {
|
|
74
77
|
fetchMock.mockResolvedValue(
|
|
75
78
|
fakeResponse(catalog, {
|
|
76
|
-
headers: { "ONBS-Audience-
|
|
79
|
+
headers: { "ONBS-Audience-Ids": "42", "ONBS-Paywall-Ids": "pw1" },
|
|
77
80
|
})
|
|
78
81
|
);
|
|
79
82
|
const client = new OnboardingStudioClient("proj-1", { appVersion: "1.2.3" });
|
|
@@ -87,18 +90,18 @@ describe("OnboardingStudioClient.getPaywalls", () => {
|
|
|
87
90
|
expect(requestedUrl.searchParams.get("platform")).toBe("ios");
|
|
88
91
|
expect(requestedUrl.searchParams.get("appVersion")).toBe("1.2.3");
|
|
89
92
|
expect(requestedUrl.searchParams.get("locale")).toBe("fr");
|
|
90
|
-
expect(requestedUrl.searchParams.has("
|
|
93
|
+
expect(requestedUrl.searchParams.has("moment")).toBe(false);
|
|
91
94
|
expect(requestedUrl.searchParams.has("draft")).toBe(false);
|
|
92
95
|
});
|
|
93
96
|
|
|
94
|
-
it("includes
|
|
97
|
+
it("includes moment only when explicitly requested (the exception, not the default)", async () => {
|
|
95
98
|
fetchMock.mockResolvedValue(fakeResponse(catalog));
|
|
96
99
|
const client = new OnboardingStudioClient("proj-1", {});
|
|
97
100
|
|
|
98
|
-
await client.getPaywalls({ locale: "en",
|
|
101
|
+
await client.getPaywalls({ locale: "en", moment: "onboarding_end" });
|
|
99
102
|
|
|
100
103
|
const requestedUrl = new URL(fetchMock.mock.calls[0][0] as string);
|
|
101
|
-
expect(requestedUrl.searchParams.get("
|
|
104
|
+
expect(requestedUrl.searchParams.get("moment")).toBe("onboarding_end");
|
|
102
105
|
});
|
|
103
106
|
|
|
104
107
|
it("appends draft=true in sandbox mode, same as getSteps", async () => {
|
|
@@ -121,10 +124,10 @@ describe("OnboardingStudioClient.getPaywalls", () => {
|
|
|
121
124
|
expect(requestedUrl.searchParams.get("experimentGroup")).toBe("b");
|
|
122
125
|
});
|
|
123
126
|
|
|
124
|
-
it("extracts ONBS-Audience-
|
|
127
|
+
it("extracts ONBS-Audience-Ids and ONBS-Paywall-Ids — not the onboarding header trio", async () => {
|
|
125
128
|
fetchMock.mockResolvedValue(
|
|
126
129
|
fakeResponse(catalog, {
|
|
127
|
-
headers: { "ONBS-Audience-
|
|
130
|
+
headers: { "ONBS-Audience-Ids": "7", "ONBS-Paywall-Ids": "pw1,pw2" },
|
|
128
131
|
})
|
|
129
132
|
);
|
|
130
133
|
const client = new OnboardingStudioClient("proj-1", {});
|
|
@@ -133,7 +136,7 @@ describe("OnboardingStudioClient.getPaywalls", () => {
|
|
|
133
136
|
|
|
134
137
|
expect(data).toEqual(catalog);
|
|
135
138
|
expect(headers).toEqual({
|
|
136
|
-
"ONBS-Audience-
|
|
139
|
+
"ONBS-Audience-Ids": "7",
|
|
137
140
|
"ONBS-Paywall-Ids": "pw1,pw2",
|
|
138
141
|
});
|
|
139
142
|
expect(headers).not.toHaveProperty("ONBS-Onboarding-Id");
|
|
@@ -182,7 +185,7 @@ const makeClient = (
|
|
|
182
185
|
const getPaywalls = vi.fn().mockResolvedValue({
|
|
183
186
|
data,
|
|
184
187
|
headers: {
|
|
185
|
-
"ONBS-Audience-
|
|
188
|
+
"ONBS-Audience-Ids": "1",
|
|
186
189
|
"ONBS-Paywall-Ids": Object.keys(data.paywalls).join(","),
|
|
187
190
|
},
|
|
188
191
|
});
|
|
@@ -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" | "
|
|
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: {
|
|
25
|
-
paywalls: Object.fromEntries(paywalls.map((p) => [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",
|
|
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
|
|
43
|
+
it("flattens products from every moment into one array", () => {
|
|
40
44
|
const catalog = makeCatalog([
|
|
41
|
-
makePaywall({ id: "1",
|
|
42
|
-
makePaywall({ id: "2",
|
|
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
|
|
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",
|
|
54
|
-
makePaywall({ id: "2",
|
|
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
|
-
|
|
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",
|
|
82
|
+
const paywall = makePaywall({ id: "1", moment: "hard_paywall", products: [] });
|
|
79
83
|
const catalog = makeCatalog([paywall]);
|
|
80
84
|
|
|
81
|
-
it("starts when the
|
|
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-
|
|
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-
|
|
98
|
+
result: { status: "error", reason: "unknown-moment" },
|
|
95
99
|
});
|
|
96
100
|
});
|
|
97
101
|
|
|
98
|
-
it("resolves 'unknown-
|
|
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-
|
|
106
|
+
result: { status: "error", reason: "unknown-moment" },
|
|
103
107
|
});
|
|
104
108
|
});
|
|
105
109
|
|
|
106
|
-
it("
|
|
107
|
-
|
|
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
|
-
|
|
128
|
+
activeMoment: "already_showing",
|
|
117
129
|
},
|
|
118
130
|
});
|
|
119
131
|
});
|
|
120
132
|
|
|
121
|
-
it("reports 'already-presenting' when the caller re-presents the SAME
|
|
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
|
-
|
|
140
|
+
activeMoment: "hard_paywall",
|
|
129
141
|
},
|
|
130
142
|
});
|
|
131
143
|
});
|
|
132
144
|
|
|
133
|
-
it("the concurrent-present check takes priority over the unknown-
|
|
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
|
-
|
|
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
|
|
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",
|
|
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
|
|
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
|
+
});
|
package/src/paywalls/present.ts
CHANGED
|
@@ -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/
|
|
8
|
-
*
|
|
9
|
-
*
|
|
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 =
|
|
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(
|
|
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
|
|
42
|
-
* resolved yet — both look identical here: `catalog?.paywalls[
|
|
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
|
-
*
|
|
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-
|
|
53
|
+
* throw" contract as the unknown-moment case, consistently.
|
|
53
54
|
*/
|
|
54
55
|
export const resolvePresentDecision = (
|
|
55
56
|
catalog: PaywallCatalog | null,
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
activeMoment: string | null,
|
|
58
|
+
moment: string
|
|
58
59
|
): PresentDecision => {
|
|
59
|
-
if (
|
|
60
|
-
// `
|
|
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",
|
|
64
|
+
result: { status: "error", reason: "already-presenting", activeMoment },
|
|
64
65
|
};
|
|
65
66
|
}
|
|
66
|
-
const paywall = catalog?.paywalls[
|
|
67
|
+
const paywall = catalog?.paywalls[moment];
|
|
67
68
|
if (!paywall) {
|
|
68
|
-
return { type: "immediate", result: { status: "error", reason: "unknown-
|
|
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[
|
|
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
|
|
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
|
|
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
|
|
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
|
+
};
|