@rocapine/react-native-onboarding 1.68.2 → 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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/paywalls/PaywallProvider.d.ts +38 -7
- package/dist/paywalls/PaywallProvider.d.ts.map +1 -1
- package/dist/paywalls/PaywallProvider.js +101 -44
- package/dist/paywalls/PaywallProvider.js.map +1 -1
- package/dist/paywalls/present.d.ts +91 -12
- package/dist/paywalls/present.d.ts.map +1 -1
- package/dist/paywalls/present.js +75 -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 +19 -4
- package/dist/paywalls/usePaywall.d.ts.map +1 -1
- package/dist/paywalls/usePaywall.js +2 -2
- package/dist/paywalls/usePaywall.js.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/index.ts +3 -0
- package/src/paywalls/PaywallProvider.tsx +145 -48
- package/src/paywalls/__tests__/getPaywalls.test.ts +15 -12
- package/src/paywalls/__tests__/present.test.ts +192 -26
- package/src/paywalls/present.ts +115 -19
- package/src/paywalls/types.ts +49 -23
- package/src/paywalls/usePaywall.ts +22 -6
- 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
package/src/paywalls/types.ts
CHANGED
|
@@ -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
|
|
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-
|
|
8
|
+
* per-moment fetch in the common path (see `PaywallOptions.moment`).
|
|
7
9
|
*/
|
|
8
10
|
|
|
9
|
-
/**
|
|
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
|
-
|
|
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
|
|
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
|
-
* `
|
|
42
|
-
* the default. Omit it to get every
|
|
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
|
-
|
|
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-
|
|
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(
|
|
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) `
|
|
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
|
|
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-
|
|
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-
|
|
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. `
|
|
113
|
-
* the result names the one that IS showing: the same
|
|
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
|
|
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-
|
|
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
|
|
145
|
-
* `reason: "already-presenting"`, where knowing WHICH
|
|
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
|
-
|
|
174
|
+
activeMoment?: string;
|
|
149
175
|
};
|
|
@@ -1,26 +1,41 @@
|
|
|
1
1
|
import { useContext } from "react";
|
|
2
2
|
import { PaywallContext } from "./PaywallProvider";
|
|
3
3
|
import { PaywallCatalog, PresentResult } from "./types";
|
|
4
|
+
import type { CatalogStatus } from "./present";
|
|
5
|
+
import type { ProductStatus } from "../products/types";
|
|
4
6
|
|
|
5
7
|
export type UsePaywallResult = {
|
|
6
8
|
/**
|
|
7
|
-
* Present a paywall by
|
|
9
|
+
* Present a paywall by moment. Never fetches — the catalog and its
|
|
8
10
|
* products are already resolved (or resolving) from `PaywallProvider`
|
|
9
11
|
* mount, which is the whole point: a paywall must render the instant the
|
|
10
12
|
* user taps upgrade.
|
|
11
13
|
*
|
|
12
|
-
* Resolves `{ status: "error" }` — never throws — when `
|
|
14
|
+
* Resolves `{ status: "error" }` — never throws — when `moment` is
|
|
13
15
|
* absent from the catalog, or when another paywall is already being
|
|
14
16
|
* presented. See `resolvePresentDecision` in `present.ts`.
|
|
15
17
|
*/
|
|
16
|
-
present: (
|
|
18
|
+
present: (moment: string) => Promise<PresentResult>;
|
|
17
19
|
/**
|
|
18
20
|
* `true` once BOTH the catalog and its products have resolved — the one
|
|
19
21
|
* flag meaning "calling `present()` now will not show a spinner".
|
|
20
22
|
*/
|
|
21
23
|
isReady: boolean;
|
|
22
|
-
/** The full resolved catalog (every
|
|
24
|
+
/** The full resolved catalog (every moment), or `null` before it loads. */
|
|
23
25
|
catalog: PaywallCatalog | null;
|
|
26
|
+
/**
|
|
27
|
+
* What the catalog is doing, for hosts that need more than `isReady`'s one
|
|
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 moment missing from it may
|
|
30
|
+
* simply not have arrived yet. Gate "fall back to another engine" on
|
|
31
|
+
* `"ready"`, not merely on a non-null catalog.
|
|
32
|
+
*/
|
|
33
|
+
catalogStatus: CatalogStatus;
|
|
34
|
+
/**
|
|
35
|
+
* What the store products are doing — the other half of `isReady`. Lets a
|
|
36
|
+
* host tell "still waiting on the store" from "still waiting on us".
|
|
37
|
+
*/
|
|
38
|
+
productsStatus: ProductStatus;
|
|
24
39
|
};
|
|
25
40
|
|
|
26
41
|
/**
|
|
@@ -30,6 +45,7 @@ export type UsePaywallResult = {
|
|
|
30
45
|
* `useProductRuntime()` degrades to `null` with no ancestor.
|
|
31
46
|
*/
|
|
32
47
|
export const usePaywall = (): UsePaywallResult => {
|
|
33
|
-
const { present, isReady, catalog } =
|
|
34
|
-
|
|
48
|
+
const { present, isReady, catalog, catalogStatus, productsStatus } =
|
|
49
|
+
useContext(PaywallContext);
|
|
50
|
+
return { present, isReady, catalog, catalogStatus, productsStatus };
|
|
35
51
|
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { productRefIdentity } from "../refIdentity";
|
|
3
|
+
import type { ProductRef } from "../types";
|
|
4
|
+
|
|
5
|
+
const STRIPE = {
|
|
6
|
+
paymentLink: "https://buy.stripe.com/test_a",
|
|
7
|
+
amount: 79,
|
|
8
|
+
currency: "USD",
|
|
9
|
+
} as const;
|
|
10
|
+
|
|
11
|
+
describe("productRefIdentity", () => {
|
|
12
|
+
it("distinguishes two refs that differ ONLY in their stripe block", () => {
|
|
13
|
+
const a: ProductRef = { key: "yearly", stripe: { ...STRIPE } };
|
|
14
|
+
const b: ProductRef = { key: "yearly", stripe: { ...STRIPE, paymentLink: "https://buy.stripe.com/test_b" } };
|
|
15
|
+
expect(productRefIdentity(a)).not.toBe(productRefIdentity(b));
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("distinguishes a store-only ref from the same key with a stripe block", () => {
|
|
19
|
+
expect(productRefIdentity({ key: "yearly", ios: "y_ios" })).not.toBe(
|
|
20
|
+
productRefIdentity({ key: "yearly", ios: "y_ios", stripe: { ...STRIPE } }),
|
|
21
|
+
);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("is stable for two structurally equal refs", () => {
|
|
25
|
+
expect(productRefIdentity({ key: "yearly", ios: "y_ios", stripe: { ...STRIPE } })).toBe(
|
|
26
|
+
productRefIdentity({ key: "yearly", ios: "y_ios", stripe: { ...STRIPE } }),
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("still separates the pre-existing store fields", () => {
|
|
31
|
+
expect(productRefIdentity({ key: "k", ios: "a" })).not.toBe(productRefIdentity({ key: "k", ios: "b" }));
|
|
32
|
+
expect(productRefIdentity({ key: "k", android: "a" })).not.toBe(productRefIdentity({ key: "k", android: "b" }));
|
|
33
|
+
expect(productRefIdentity({ key: "k", compareTo: "a" })).not.toBe(
|
|
34
|
+
productRefIdentity({ key: "k", compareTo: "b" }),
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("does not collide across field boundaries", () => {
|
|
39
|
+
// "a|b" split across two fields must not equal "a|b" in one.
|
|
40
|
+
expect(productRefIdentity({ key: "k", ios: "a", android: "b" })).not.toBe(
|
|
41
|
+
productRefIdentity({ key: "k", ios: "a|b" }),
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { stripeLinkProductProvider } from "../stripeLink";
|
|
3
|
+
import type { ProductRef } from "../../types";
|
|
4
|
+
|
|
5
|
+
const YEARLY: ProductRef = {
|
|
6
|
+
key: "yearly",
|
|
7
|
+
ios: "pro_yearly_ios",
|
|
8
|
+
stripe: {
|
|
9
|
+
paymentLink: "https://buy.stripe.com/test_yearly",
|
|
10
|
+
priceId: "price_123",
|
|
11
|
+
amount: 79,
|
|
12
|
+
currency: "USD",
|
|
13
|
+
periodIso: "P1Y",
|
|
14
|
+
trialDays: 7,
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const STORE_ONLY: ProductRef = { key: "monthly", ios: "pro_monthly_ios" };
|
|
19
|
+
|
|
20
|
+
const harness = (over: Partial<Parameters<typeof stripeLinkProductProvider>[0]> = {}) => {
|
|
21
|
+
const opened: string[] = [];
|
|
22
|
+
const provider = stripeLinkProductProvider({
|
|
23
|
+
clientReferenceId: () => "rc_user_1",
|
|
24
|
+
openUrl: (url) => void opened.push(url),
|
|
25
|
+
...over,
|
|
26
|
+
});
|
|
27
|
+
return { provider, opened };
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
describe("stripeLinkProductProvider.getProducts", () => {
|
|
31
|
+
it("synthesises a ResolvedProduct from the ref, with no network call", async () => {
|
|
32
|
+
const { provider } = harness();
|
|
33
|
+
const [p] = await provider.getProducts([YEARLY]);
|
|
34
|
+
expect(p.key).toBe("yearly");
|
|
35
|
+
expect(p.store).toBe("stripe");
|
|
36
|
+
expect(p.productId).toBe("price_123");
|
|
37
|
+
expect(p.priceAmount).toBe(79);
|
|
38
|
+
expect(p.currencyCode).toBe("USD");
|
|
39
|
+
expect(p.periodIso).toBe("P1Y");
|
|
40
|
+
expect(p.period).toBe("year");
|
|
41
|
+
expect(p.periodCount).toBe(1);
|
|
42
|
+
expect(p.trial).toEqual({ period: "week", periodCount: 1, days: 7 });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("formats price in the product's currency", async () => {
|
|
46
|
+
const { provider } = harness();
|
|
47
|
+
const [p] = await provider.getProducts([YEARLY]);
|
|
48
|
+
// Exact glyph placement is Intl's business; assert the part we control.
|
|
49
|
+
expect(p.price).toContain("79");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("falls back to the payment link as productId when priceId is absent", async () => {
|
|
53
|
+
const { provider } = harness();
|
|
54
|
+
const ref: ProductRef = { key: "k", stripe: { paymentLink: "https://buy.stripe.com/x", amount: 1, currency: "USD" } };
|
|
55
|
+
const [p] = await provider.getProducts([ref]);
|
|
56
|
+
expect(p.productId).toBe("https://buy.stripe.com/x");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("drops refs with no stripe block, exactly as the store adapters drop refs with no platform id", async () => {
|
|
60
|
+
const { provider } = harness();
|
|
61
|
+
expect(await provider.getProducts([STORE_ONLY])).toEqual([]);
|
|
62
|
+
expect((await provider.getProducts([YEARLY, STORE_ONLY])).map((p) => p.key)).toEqual(["yearly"]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("omits trial when trialDays is absent or zero", async () => {
|
|
66
|
+
const { provider } = harness();
|
|
67
|
+
const [p] = await provider.getProducts([
|
|
68
|
+
{ key: "k", stripe: { paymentLink: "https://buy.stripe.com/x", amount: 1, currency: "USD", trialDays: 0 } },
|
|
69
|
+
]);
|
|
70
|
+
expect(p.trial).toBeUndefined();
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("stripeLinkProductProvider.purchase", () => {
|
|
75
|
+
it("opens the link with client_reference_id and resolves pending", async () => {
|
|
76
|
+
const { provider, opened } = harness();
|
|
77
|
+
const [p] = await provider.getProducts([YEARLY]);
|
|
78
|
+
const result = await provider.purchase(p);
|
|
79
|
+
expect(result).toEqual({ status: "pending" });
|
|
80
|
+
expect(opened).toHaveLength(1);
|
|
81
|
+
const url = new URL(opened[0]);
|
|
82
|
+
expect(url.origin + url.pathname).toBe("https://buy.stripe.com/test_yearly");
|
|
83
|
+
expect(url.searchParams.get("client_reference_id")).toBe("rc_user_1");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("adds prefilled_email when configured", async () => {
|
|
87
|
+
const { provider, opened } = harness({ prefilledEmail: () => "a@b.com" });
|
|
88
|
+
const [p] = await provider.getProducts([YEARLY]);
|
|
89
|
+
await provider.purchase(p);
|
|
90
|
+
expect(new URL(opened[0]).searchParams.get("prefilled_email")).toBe("a@b.com");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("refuses (fails closed) rather than open the link with no client_reference_id", async () => {
|
|
94
|
+
const { provider, opened } = harness({ clientReferenceId: () => null });
|
|
95
|
+
const [p] = await provider.getProducts([YEARLY]);
|
|
96
|
+
const result = await provider.purchase(p);
|
|
97
|
+
expect(result.status).toBe("error");
|
|
98
|
+
// The whole point: nothing opens, so no money moves for a purchase
|
|
99
|
+
// RevenueCat could never attribute.
|
|
100
|
+
expect(opened).toHaveLength(0);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("also refuses for an undefined or empty-string client_reference_id", async () => {
|
|
104
|
+
for (const value of [undefined, ""]) {
|
|
105
|
+
const { provider, opened } = harness({ clientReferenceId: () => value });
|
|
106
|
+
const [p] = await provider.getProducts([YEARLY]);
|
|
107
|
+
const result = await provider.purchase(p);
|
|
108
|
+
expect(result.status).toBe("error");
|
|
109
|
+
expect(opened).toHaveLength(0);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("allowAnonymous: true opens the link anyway, omitting client_reference_id", async () => {
|
|
114
|
+
const { provider, opened } = harness({ clientReferenceId: () => null, allowAnonymous: true });
|
|
115
|
+
const [p] = await provider.getProducts([YEARLY]);
|
|
116
|
+
const result = await provider.purchase(p);
|
|
117
|
+
expect(result).toEqual({ status: "pending" });
|
|
118
|
+
expect(opened).toHaveLength(1);
|
|
119
|
+
// Still omitted rather than sent as the literal string "null" —
|
|
120
|
+
// `allowAnonymous` opts into opening the link, not into a bad param.
|
|
121
|
+
expect(new URL(opened[0]).searchParams.has("client_reference_id")).toBe(false);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("preserves query parameters already on the payment link", async () => {
|
|
125
|
+
const { provider, opened } = harness();
|
|
126
|
+
const [p] = await provider.getProducts([
|
|
127
|
+
{ key: "k", stripe: { paymentLink: "https://buy.stripe.com/x?locale=fr", amount: 1, currency: "USD" } },
|
|
128
|
+
]);
|
|
129
|
+
await provider.purchase(p);
|
|
130
|
+
const url = new URL(opened[0]);
|
|
131
|
+
expect(url.searchParams.get("locale")).toBe("fr");
|
|
132
|
+
expect(url.searchParams.get("client_reference_id")).toBe("rc_user_1");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("errors for a product it never resolved, instead of opening nothing silently", async () => {
|
|
136
|
+
const { provider, opened } = harness();
|
|
137
|
+
const result = await provider.purchase({
|
|
138
|
+
key: "ghost", productId: "x", store: "stripe", title: "", description: "",
|
|
139
|
+
price: "", priceAmount: 0, currencyCode: "USD", period: null, periodCount: 0, periodIso: null,
|
|
140
|
+
});
|
|
141
|
+
expect(result.status).toBe("error");
|
|
142
|
+
expect(opened).toHaveLength(0);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("resolves status: error (never rejects) for a malformed payment link", async () => {
|
|
146
|
+
const { provider, opened } = harness();
|
|
147
|
+
const [p] = await provider.getProducts([
|
|
148
|
+
{ key: "k", stripe: { paymentLink: "not a url", amount: 1, currency: "USD" } },
|
|
149
|
+
]);
|
|
150
|
+
const result = await provider.purchase(p);
|
|
151
|
+
expect(result.status).toBe("error");
|
|
152
|
+
expect(opened).toHaveLength(0);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("does not serve a stale link once a key's stripe block is dropped from the catalog", async () => {
|
|
156
|
+
const { provider, opened } = harness();
|
|
157
|
+
const ref: ProductRef = { key: "k", stripe: { paymentLink: "https://buy.stripe.com/x", amount: 1, currency: "USD" } };
|
|
158
|
+
const [p] = await provider.getProducts([ref]);
|
|
159
|
+
// Re-authored off Stripe billing: the same key, now with no stripe block.
|
|
160
|
+
await provider.getProducts([{ key: "k" }]);
|
|
161
|
+
const result = await provider.purchase(p);
|
|
162
|
+
expect(result.status).toBe("error");
|
|
163
|
+
expect(opened).toHaveLength(0);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe("stripeLinkProductProvider.restore", () => {
|
|
168
|
+
it("errors by default rather than reporting nothing_to_restore", async () => {
|
|
169
|
+
const { provider } = harness();
|
|
170
|
+
const result = await provider.restore();
|
|
171
|
+
// `nothing_to_restore` would make a Restore button silently do nothing.
|
|
172
|
+
expect(result.status).toBe("error");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("delegates when the host supplies a restore implementation", async () => {
|
|
176
|
+
const { provider } = harness({ restore: async () => ({ status: "restored", entitlements: ["pro"] }) });
|
|
177
|
+
expect(await provider.restore()).toEqual({ status: "restored", entitlements: ["pro"] });
|
|
178
|
+
});
|
|
179
|
+
});
|