@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.
- 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/expoIap.d.ts.map +1 -1
- package/dist/products/adapters/expoIap.js +118 -11
- package/dist/products/adapters/expoIap.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/dist/steps/common.types.d.ts +2 -0
- package/dist/steps/common.types.d.ts.map +1 -1
- package/dist/steps/common.types.js +1 -0
- package/dist/steps/common.types.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__/expoIap.test.ts +210 -0
- package/src/products/adapters/__tests__/stripeLink.test.ts +179 -0
- package/src/products/adapters/expoIap.ts +121 -5
- 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/steps/common.types.ts +3 -0
|
@@ -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,210 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
// The adapter imports `Platform` from react-native, whose entry point is Flow
|
|
4
|
+
// and cannot be parsed by vitest's transformer. Same one-line stub every other
|
|
5
|
+
// react-native-touching test in this package uses.
|
|
6
|
+
vi.mock("react-native", () => ({ Platform: { OS: "ios" } }));
|
|
7
|
+
|
|
8
|
+
import { expoIapProductProvider } from "../expoIap";
|
|
9
|
+
import type { ProductRef } from "../../types";
|
|
10
|
+
|
|
11
|
+
// The adapter resolves ids by platform and vitest runs the iOS branch of the
|
|
12
|
+
// `react-native` Platform shim, so every ref here carries `ios`.
|
|
13
|
+
const YEARLY: ProductRef = { key: "yearly", ios: "pro_yearly" };
|
|
14
|
+
const LIFETIME: ProductRef = { key: "lifetime", ios: "pro_lifetime" };
|
|
15
|
+
|
|
16
|
+
// A subscription as expo-iap 5.x actually reports it: the period is SPLIT into
|
|
17
|
+
// a unit and a stringly count, and there is no `subscriptionPeriodISO` at all.
|
|
18
|
+
const iap5Yearly = {
|
|
19
|
+
id: "pro_yearly",
|
|
20
|
+
type: "subs",
|
|
21
|
+
title: "Pro Yearly",
|
|
22
|
+
description: "Best value",
|
|
23
|
+
displayPrice: "$59.99",
|
|
24
|
+
price: 59.99,
|
|
25
|
+
currency: "USD",
|
|
26
|
+
subscriptionPeriodUnitIOS: "year",
|
|
27
|
+
subscriptionPeriodNumberIOS: "1",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const iap5Lifetime = {
|
|
31
|
+
id: "pro_lifetime",
|
|
32
|
+
type: "in-app",
|
|
33
|
+
title: "Lifetime",
|
|
34
|
+
description: "One time",
|
|
35
|
+
displayPrice: "$119.99",
|
|
36
|
+
price: 119.99,
|
|
37
|
+
currency: "USD",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const mock5 = (over: Record<string, any> = {}) => ({
|
|
41
|
+
initConnection: vi.fn().mockResolvedValue(true),
|
|
42
|
+
fetchProducts: vi.fn().mockResolvedValue([iap5Yearly, iap5Lifetime]),
|
|
43
|
+
requestPurchase: vi.fn().mockResolvedValue(null),
|
|
44
|
+
finishTransaction: vi.fn().mockResolvedValue(undefined),
|
|
45
|
+
getAvailablePurchases: vi.fn().mockResolvedValue([]),
|
|
46
|
+
...over,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("expoIapProductProvider — expo-iap 5.x API", () => {
|
|
50
|
+
it("calls fetchProducts, not the removed getProducts", async () => {
|
|
51
|
+
// 5.x deleted `getProducts`; calling it threw "M.getProducts is not a
|
|
52
|
+
// function" and every product silently failed to resolve. A mock with NO
|
|
53
|
+
// getProducts is what pins that.
|
|
54
|
+
const M = mock5();
|
|
55
|
+
const products = await expoIapProductProvider(M).getProducts([YEARLY, LIFETIME]);
|
|
56
|
+
expect(M.fetchProducts).toHaveBeenCalledWith({ skus: ["pro_yearly", "pro_lifetime"], type: "all" });
|
|
57
|
+
expect(products.map((p) => p.key)).toEqual(["yearly", "lifetime"]);
|
|
58
|
+
expect(products[0].price).toBe("$59.99");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("opens the store connection before querying, exactly once", async () => {
|
|
62
|
+
const M = mock5();
|
|
63
|
+
const provider = expoIapProductProvider(M);
|
|
64
|
+
await provider.getProducts([YEARLY]);
|
|
65
|
+
await provider.getProducts([YEARLY]);
|
|
66
|
+
await provider.restore();
|
|
67
|
+
expect(M.initConnection).toHaveBeenCalledTimes(1);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("retries the connection after a failed one instead of replaying the rejection", async () => {
|
|
71
|
+
// A first call while offline must not poison the provider for the session.
|
|
72
|
+
const initConnection = vi
|
|
73
|
+
.fn()
|
|
74
|
+
.mockRejectedValueOnce(new Error("offline"))
|
|
75
|
+
.mockResolvedValue(true);
|
|
76
|
+
const M = mock5({ initConnection });
|
|
77
|
+
const provider = expoIapProductProvider(M);
|
|
78
|
+
await expect(provider.getProducts([YEARLY])).rejects.toThrow("offline");
|
|
79
|
+
await expect(provider.getProducts([YEARLY])).resolves.toHaveLength(1);
|
|
80
|
+
expect(initConnection).toHaveBeenCalledTimes(2);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("derives periodIso from the split iOS fields", async () => {
|
|
84
|
+
// The whole point: `periodIso` feeds deriveProductFields, so a null here
|
|
85
|
+
// removes pricePerWeek/pricePerMonth/savingsPct rather than degrading them.
|
|
86
|
+
const [yearly, lifetime] = await expoIapProductProvider(mock5()).getProducts([YEARLY, LIFETIME]);
|
|
87
|
+
expect(yearly.periodIso).toBe("P1Y");
|
|
88
|
+
expect(yearly.period).toBe("year");
|
|
89
|
+
expect(yearly.periodCount).toBe(1);
|
|
90
|
+
// A one-off product has no period at all — not "P1Y" by accident.
|
|
91
|
+
expect(lifetime.periodIso).toBeNull();
|
|
92
|
+
expect(lifetime.period).toBeNull();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("defaults a missing period count to 1, never 0", async () => {
|
|
96
|
+
// StoreKit omits the count for a single-unit period. "P0M" would make every
|
|
97
|
+
// derived per-unit price divide by zero.
|
|
98
|
+
const M = mock5({
|
|
99
|
+
fetchProducts: vi.fn().mockResolvedValue([
|
|
100
|
+
{ ...iap5Yearly, subscriptionPeriodUnitIOS: "month", subscriptionPeriodNumberIOS: undefined },
|
|
101
|
+
]),
|
|
102
|
+
});
|
|
103
|
+
const [p] = await expoIapProductProvider(M).getProducts([YEARLY]);
|
|
104
|
+
expect(p.periodIso).toBe("P1M");
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("reads the Android billing period out of the first pricing phase", async () => {
|
|
108
|
+
const M = mock5({
|
|
109
|
+
fetchProducts: vi.fn().mockResolvedValue([
|
|
110
|
+
{
|
|
111
|
+
id: "pro_yearly",
|
|
112
|
+
type: "subs",
|
|
113
|
+
displayPrice: "$59.99",
|
|
114
|
+
price: 59.99,
|
|
115
|
+
currency: "USD",
|
|
116
|
+
subscriptionOffers: [
|
|
117
|
+
{ pricingPhasesAndroid: { pricingPhaseList: [{ billingPeriod: "P1Y" }] } },
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
]),
|
|
121
|
+
});
|
|
122
|
+
const [p] = await expoIapProductProvider(M).getProducts([YEARLY]);
|
|
123
|
+
expect(p.periodIso).toBe("P1Y");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("still honours a pre-5.x product that publishes the ISO period directly", async () => {
|
|
127
|
+
const M = mock5({
|
|
128
|
+
fetchProducts: vi
|
|
129
|
+
.fn()
|
|
130
|
+
.mockResolvedValue([{ ...iap5Yearly, subscriptionPeriodISO: "P6M", subscriptionPeriodUnitIOS: "year" }]),
|
|
131
|
+
});
|
|
132
|
+
const [p] = await expoIapProductProvider(M).getProducts([YEARLY]);
|
|
133
|
+
expect(p.periodIso).toBe("P6M");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("falls back to the legacy getProducts when fetchProducts is absent", async () => {
|
|
137
|
+
// A host pinned to expo-iap ≤4 must keep working.
|
|
138
|
+
const getProducts = vi.fn().mockResolvedValue([{ ...iap5Yearly, subscriptionPeriodISO: "P1Y" }]);
|
|
139
|
+
const M = { getProducts, requestPurchase: vi.fn(), getAvailablePurchases: vi.fn() };
|
|
140
|
+
const [p] = await expoIapProductProvider(M).getProducts([YEARLY]);
|
|
141
|
+
expect(getProducts).toHaveBeenCalledWith(["pro_yearly"]);
|
|
142
|
+
expect(p.periodIso).toBe("P1Y");
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe("expoIapProductProvider — purchase", () => {
|
|
147
|
+
const resolved = async (M: any) => (await expoIapProductProvider(M).getProducts([YEARLY]))[0];
|
|
148
|
+
|
|
149
|
+
it("sends the per-platform request shape with the subscription type", async () => {
|
|
150
|
+
// The old flat `{ request: { sku } }` reached neither platform branch, so
|
|
151
|
+
// StoreKit received an undefined sku.
|
|
152
|
+
const M = mock5({ requestPurchase: vi.fn().mockResolvedValue({ id: "tx" }) });
|
|
153
|
+
const provider = expoIapProductProvider(M);
|
|
154
|
+
const product = (await provider.getProducts([YEARLY]))[0];
|
|
155
|
+
await provider.purchase(product);
|
|
156
|
+
expect(M.requestPurchase).toHaveBeenCalledWith({
|
|
157
|
+
request: { ios: { sku: "pro_yearly" }, android: { skus: ["pro_yearly"] } },
|
|
158
|
+
type: "subs",
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("sends type in-app for a one-off product", async () => {
|
|
163
|
+
const M = mock5({ requestPurchase: vi.fn().mockResolvedValue({ id: "tx" }) });
|
|
164
|
+
const provider = expoIapProductProvider(M);
|
|
165
|
+
const product = (await provider.getProducts([LIFETIME]))[0];
|
|
166
|
+
await provider.purchase(product);
|
|
167
|
+
expect(M.requestPurchase.mock.calls[0][0].type).toBe("in-app");
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("resolves pending — not purchased — when requestPurchase resolves null", async () => {
|
|
171
|
+
// The normal 5.x outcome: the transaction arrives via purchaseUpdatedListener,
|
|
172
|
+
// so nothing is confirmed yet. Reporting "purchased" granted access for a
|
|
173
|
+
// purchase that may still fail.
|
|
174
|
+
const M = mock5({ requestPurchase: vi.fn().mockResolvedValue(null) });
|
|
175
|
+
const provider = expoIapProductProvider(M);
|
|
176
|
+
const product = (await provider.getProducts([YEARLY]))[0];
|
|
177
|
+
expect(await provider.purchase(product)).toEqual({ status: "pending" });
|
|
178
|
+
expect(M.finishTransaction).not.toHaveBeenCalled();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("finishes the transaction when one comes back", async () => {
|
|
182
|
+
// Unfinished transactions are re-delivered by StoreKit on every launch.
|
|
183
|
+
const purchase = { id: "tx-1" };
|
|
184
|
+
const M = mock5({ requestPurchase: vi.fn().mockResolvedValue(purchase) });
|
|
185
|
+
const provider = expoIapProductProvider(M);
|
|
186
|
+
const product = (await provider.getProducts([YEARLY]))[0];
|
|
187
|
+
expect(await provider.purchase(product)).toEqual({ status: "purchased", productKey: "yearly" });
|
|
188
|
+
expect(M.finishTransaction).toHaveBeenCalledWith({ purchase, isConsumable: false });
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("still reports purchased when finishing throws", async () => {
|
|
192
|
+
// A finish failure does not un-buy anything; it must not become an error.
|
|
193
|
+
const M = mock5({
|
|
194
|
+
requestPurchase: vi.fn().mockResolvedValue({ id: "tx-1" }),
|
|
195
|
+
finishTransaction: vi.fn().mockRejectedValue(new Error("finish failed")),
|
|
196
|
+
});
|
|
197
|
+
const provider = expoIapProductProvider(M);
|
|
198
|
+
const product = (await provider.getProducts([YEARLY]))[0];
|
|
199
|
+
expect(await provider.purchase(product)).toEqual({ status: "purchased", productKey: "yearly" });
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("maps a user cancellation to cancelled, not error", async () => {
|
|
203
|
+
const M = mock5({
|
|
204
|
+
requestPurchase: vi.fn().mockRejectedValue({ code: "E_USER_CANCELLED" }),
|
|
205
|
+
});
|
|
206
|
+
const provider = expoIapProductProvider(M);
|
|
207
|
+
const product = (await provider.getProducts([YEARLY]))[0];
|
|
208
|
+
expect(await provider.purchase(product)).toEqual({ status: "cancelled" });
|
|
209
|
+
});
|
|
210
|
+
});
|
|
@@ -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
|
+
});
|
|
@@ -24,6 +24,43 @@ const isCancellation = (e: any): boolean =>
|
|
|
24
24
|
const idFor = (ref: ProductRef): string | undefined =>
|
|
25
25
|
Platform.OS === "ios" ? ref.ios : ref.android;
|
|
26
26
|
|
|
27
|
+
const ISO_UNIT: Record<string, string> = { day: "D", week: "W", month: "M", year: "Y" };
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The store's billing period as an ISO-8601 duration.
|
|
31
|
+
*
|
|
32
|
+
* Load-bearing far beyond the `period` field: `deriveProductFields` computes
|
|
33
|
+
* `pricePerDay` / `pricePerWeek` / `pricePerMonth` / `pricePerYear` and
|
|
34
|
+
* `savingsPct` from `periodIso` alone. A null here does not degrade those — it
|
|
35
|
+
* removes them, so `{{product.yearly.pricePerWeek}}` renders EMPTY (an unknown
|
|
36
|
+
* variable interpolates to nothing, not to a literal) and a per-week-framed
|
|
37
|
+
* paywall silently loses its headline number.
|
|
38
|
+
*
|
|
39
|
+
* expo-iap has never exposed one ready-made, and the shape has moved:
|
|
40
|
+
* - ≤4.x published `subscriptionPeriodISO` / `subscriptionPeriod` on the product
|
|
41
|
+
* - 5.x publishes the iOS period SPLIT in two (`subscriptionPeriodUnitIOS`,
|
|
42
|
+
* `subscriptionPeriodNumberIOS`) and the Android one buried in the first
|
|
43
|
+
* pricing phase of the first subscription offer
|
|
44
|
+
* All three are read, newest-shape-last so an older peer keeps working.
|
|
45
|
+
*/
|
|
46
|
+
const periodIsoFrom = (s: any): string | null => {
|
|
47
|
+
const direct = s?.subscriptionPeriodISO ?? s?.subscriptionPeriod;
|
|
48
|
+
if (typeof direct === "string" && direct) return direct;
|
|
49
|
+
|
|
50
|
+
const unit = ISO_UNIT[String(s?.subscriptionPeriodUnitIOS ?? "").toLowerCase()];
|
|
51
|
+
if (unit) {
|
|
52
|
+
// `subscriptionPeriodNumberIOS` is typed as a STRING by expo-iap. Default to
|
|
53
|
+
// 1 rather than 0: StoreKit omits the count for a single-unit period, and
|
|
54
|
+
// "P0M" would make every derived price divide by zero.
|
|
55
|
+
const n = Number(s?.subscriptionPeriodNumberIOS ?? 1);
|
|
56
|
+
return `P${Number.isFinite(n) && n > 0 ? n : 1}${unit}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const androidPeriod =
|
|
60
|
+
s?.subscriptionOffers?.[0]?.pricingPhasesAndroid?.pricingPhaseList?.[0]?.billingPeriod;
|
|
61
|
+
return typeof androidPeriod === "string" && androidPeriod ? androidPeriod : null;
|
|
62
|
+
};
|
|
63
|
+
|
|
27
64
|
/** Direct StoreKit / Play Billing provider, no vendor in the path. */
|
|
28
65
|
export const expoIapProductProvider = (Iap: any = IAP): ProductProvider => {
|
|
29
66
|
const required = () => {
|
|
@@ -35,23 +72,62 @@ export const expoIapProductProvider = (Iap: any = IAP): ProductProvider => {
|
|
|
35
72
|
return Iap;
|
|
36
73
|
};
|
|
37
74
|
|
|
75
|
+
// Every expo-iap query fails until the store connection is open, and nothing
|
|
76
|
+
// opens it implicitly — `useIAP` does it for hook consumers, but this adapter
|
|
77
|
+
// is not a hook. Cached so concurrent getProducts/purchase/restore calls share
|
|
78
|
+
// one connect, and CLEARED on failure so a later call retries rather than
|
|
79
|
+
// replaying a rejected promise forever (a first call during airplane mode
|
|
80
|
+
// would otherwise poison the provider for the whole session).
|
|
81
|
+
let connecting: Promise<unknown> | null = null;
|
|
82
|
+
const connect = async () => {
|
|
83
|
+
const M = required();
|
|
84
|
+
if (typeof M.initConnection !== "function") return; // older peer: implicit connection
|
|
85
|
+
if (!connecting) {
|
|
86
|
+
connecting = Promise.resolve(M.initConnection()).catch((e: unknown) => {
|
|
87
|
+
connecting = null;
|
|
88
|
+
throw e;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
await connecting;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// The raw store product per resolved productId, captured during getProducts.
|
|
95
|
+
// `purchase()` needs expo-iap's own `type` discriminator ("in-app" | "subs")
|
|
96
|
+
// and the full Purchase to finish, neither of which survives the trip through
|
|
97
|
+
// `ResolvedProduct`. Same pattern as stripeLink's `linkByKey`.
|
|
98
|
+
const rawById = new Map<string, any>();
|
|
99
|
+
|
|
38
100
|
return {
|
|
39
101
|
async getProducts(refs: ProductRef[]): Promise<ResolvedProduct[]> {
|
|
40
102
|
const M = required();
|
|
41
103
|
const wanted = refs.map((r) => ({ ref: r, id: idFor(r) })).filter((x) => !!x.id);
|
|
42
104
|
if (wanted.length === 0) return [];
|
|
105
|
+
await connect();
|
|
43
106
|
const skus = wanted.map((w) => w.id as string);
|
|
44
|
-
|
|
45
|
-
|
|
107
|
+
|
|
108
|
+
// 5.x renamed `getProducts(skus)` to `fetchProducts({ skus, type })` — an
|
|
109
|
+
// object arg, not a positional array. Calling the old name on 5.x throws
|
|
110
|
+
// "M.getProducts is not a function", so probe rather than assume; `"all"`
|
|
111
|
+
// because a paywall mixes subscriptions and one-off lifetime products.
|
|
112
|
+
const store =
|
|
113
|
+
typeof M.fetchProducts === "function"
|
|
114
|
+
? await M.fetchProducts({ skus, type: "all" })
|
|
115
|
+
: await M.getProducts(skus);
|
|
116
|
+
|
|
117
|
+
const list: any[] = Array.isArray(store) ? store : [];
|
|
118
|
+
const byId = new Map<string, any>(list.map((s: any) => [s.id ?? s.productId, s]));
|
|
119
|
+
rawById.clear();
|
|
46
120
|
const out: ResolvedProduct[] = [];
|
|
47
121
|
for (const { ref, id } of wanted) {
|
|
48
122
|
const s = byId.get(id as string);
|
|
49
123
|
if (!s) continue;
|
|
50
|
-
const
|
|
124
|
+
const productId = s.id ?? s.productId;
|
|
125
|
+
rawById.set(productId, s);
|
|
126
|
+
const iso = periodIsoFrom(s);
|
|
51
127
|
const { period, count } = toPeriod(iso);
|
|
52
128
|
out.push({
|
|
53
129
|
key: ref.key,
|
|
54
|
-
productId
|
|
130
|
+
productId,
|
|
55
131
|
store: Platform.OS === "ios" ? "app_store" : "play_store",
|
|
56
132
|
title: s.title ?? "",
|
|
57
133
|
description: s.description ?? "",
|
|
@@ -69,7 +145,46 @@ export const expoIapProductProvider = (Iap: any = IAP): ProductProvider => {
|
|
|
69
145
|
async purchase(product) {
|
|
70
146
|
const M = required();
|
|
71
147
|
try {
|
|
72
|
-
await
|
|
148
|
+
await connect();
|
|
149
|
+
const raw = rawById.get(product.productId);
|
|
150
|
+
// A non-null period means a subscription even when the raw product is
|
|
151
|
+
// missing (a host that hydrated the runtime from elsewhere).
|
|
152
|
+
const isSubs = (raw?.type ?? (product.period ? "subs" : "in-app")) === "subs";
|
|
153
|
+
const sku = product.productId;
|
|
154
|
+
|
|
155
|
+
// 5.x wants `{ request: { ios, android }, type }`. The old flat
|
|
156
|
+
// `{ request: { sku } }` reaches neither platform branch, so StoreKit is
|
|
157
|
+
// handed an undefined sku. `type` is required to tell a subscription
|
|
158
|
+
// from a one-off — Play rejects the wrong one outright.
|
|
159
|
+
const args =
|
|
160
|
+
typeof M.fetchProducts === "function"
|
|
161
|
+
? {
|
|
162
|
+
request: { ios: { sku }, android: { skus: [sku] } },
|
|
163
|
+
type: isSubs ? "subs" : "in-app",
|
|
164
|
+
}
|
|
165
|
+
: { request: { sku } };
|
|
166
|
+
|
|
167
|
+
const result = await M.requestPurchase(args as any);
|
|
168
|
+
|
|
169
|
+
// `requestPurchase` resolves `Purchase | Purchase[] | null`. `null` is
|
|
170
|
+
// the normal 5.x outcome: the transaction is delivered to
|
|
171
|
+
// `purchaseUpdatedListener` instead, so at this point nothing is
|
|
172
|
+
// confirmed. Reporting "purchased" there would grant access for a
|
|
173
|
+
// purchase that may still fail — hence "pending", which is exactly the
|
|
174
|
+
// outcome `PurchaseButtonAction.onPending` exists to handle.
|
|
175
|
+
const purchase = Array.isArray(result) ? result[0] : result;
|
|
176
|
+
if (!purchase) return { status: "pending" };
|
|
177
|
+
|
|
178
|
+
// Unfinished transactions are re-delivered by StoreKit on every launch
|
|
179
|
+
// until finished. Best-effort: a failure here does not un-buy anything,
|
|
180
|
+
// so it must not turn a completed purchase into an error.
|
|
181
|
+
if (typeof M.finishTransaction === "function") {
|
|
182
|
+
try {
|
|
183
|
+
await M.finishTransaction({ purchase, isConsumable: false });
|
|
184
|
+
} catch {
|
|
185
|
+
// ignore — reported as purchased, will be re-delivered and finished later
|
|
186
|
+
}
|
|
187
|
+
}
|
|
73
188
|
return { status: "purchased", productKey: product.key };
|
|
74
189
|
} catch (e: any) {
|
|
75
190
|
if (isCancellation(e)) return { status: "cancelled" };
|
|
@@ -80,6 +195,7 @@ export const expoIapProductProvider = (Iap: any = IAP): ProductProvider => {
|
|
|
80
195
|
async restore() {
|
|
81
196
|
const M = required();
|
|
82
197
|
try {
|
|
198
|
+
await connect();
|
|
83
199
|
const purchases = await M.getAvailablePurchases();
|
|
84
200
|
const ids: string[] = (purchases ?? []).map((p: any) => p.id ?? p.productId).filter(Boolean);
|
|
85
201
|
return ids.length > 0
|