create-disposabl-app 0.15.0 → 0.17.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/README.md +26 -9
- package/dist/disposabl.js +357 -50
- package/dist/index.js +9026 -160
- package/dist/templates/default/.github/scripts/mobile.sh +3 -2
- package/dist/templates/default/.github/scripts/release.sh +1 -0
- package/dist/templates/default/.github/scripts/verify-android-abis.mjs +22 -0
- package/dist/templates/default/apps/desktop/forge.config.mts +2 -1
- package/dist/templates/default/apps/docs/content/getting-started.mdx +1 -1
- package/dist/templates/default/apps/mobile/app.config.js +18 -9
- package/dist/templates/default/apps/mobile/package.json +1 -0
- package/dist/templates/default/apps/mobile/plugins/with-e2e-network.js +41 -0
- package/dist/templates/default/apps/web/eslint.config.ts +5 -1
- package/dist/templates/default/apps/web/public/disposabl-logo-light.svg +8 -0
- package/dist/templates/default/apps/web/public/landing/coast.jpg +0 -0
- package/dist/templates/default/apps/web/public/landing/community.jpg +0 -0
- package/dist/templates/default/apps/web/public/landing/stores/app-store.svg +46 -0
- package/dist/templates/default/apps/web/public/landing/stores/google-play.png +0 -0
- package/dist/templates/default/apps/web/public/landing/taste/LICENSE +21 -0
- package/dist/templates/default/apps/web/public/landing/taste/init.js +3 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/lib.js +189 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/moments.js +109 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.css +8 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.js +35 -0
- package/dist/templates/default/apps/web/public/landing/taste/motion/motion.css +321 -0
- package/dist/templates/default/apps/web/public/landing/taste/motion/motion.js +406 -0
- package/dist/templates/default/apps/web/routes.config.ts +14 -0
- package/dist/templates/default/apps/web/src/router/__root.tsx +8 -2
- package/dist/templates/default/apps/web/tsr.config.json +1 -1
- package/dist/templates/default/apps/web/vite.config.ts +10 -2
- package/dist/templates/default/bun.lock +6 -0
- package/dist/templates/default/bunfig.toml +1 -0
- package/dist/templates/default/disposabl.json +2 -1
- package/dist/templates/default/packages/backend/convex/authSetup.ts +2 -5
- package/dist/templates/default/packages/backend/convex/billing.ts +5 -0
- package/dist/templates/default/packages/backend/convex/billingHandoff.ts +6 -0
- package/dist/templates/default/packages/backend/convex/convex.config.ts +8 -0
- package/dist/templates/default/packages/backend/convex/http.ts +6 -0
- package/dist/templates/default/packages/backend/convex/revenuecat.ts +13 -0
- package/dist/templates/default/packages/backend/convex/schema.ts +44 -0
- package/dist/templates/default/packages/backend/convex/telemetry.ts +41 -5
- package/dist/templates/default/packages/backend/generate.ts +6 -0
- package/dist/templates/default/packages/cli/auth.ts +36 -3
- package/dist/templates/default/packages/cli/build.ts +6 -0
- package/dist/templates/default/packages/cli/client.ts +189 -19
- package/dist/templates/default/packages/cli/errors.ts +18 -0
- package/dist/templates/default/packages/cli/telemetry-build.ts +27 -0
- package/dist/templates/default/packages/cli/telemetry.ts +266 -0
- package/dist/templates/default/packages/disposabl-kit/PAYMENTS.md +23 -5
- package/dist/templates/default/packages/disposabl-kit/android-architectures.ts +35 -0
- package/dist/templates/default/packages/disposabl-kit/client/lib/telemetry.ts +24 -2
- package/dist/templates/default/packages/disposabl-kit/client/lib/theme.ts +1 -0
- package/dist/templates/default/packages/disposabl-kit/client/routes/billing-login.tsx +92 -0
- package/dist/templates/default/packages/disposabl-kit/client/routes/pricing.tsx +113 -43
- package/dist/templates/default/packages/disposabl-kit/client/routes/route.tsx +13 -5
- package/dist/templates/default/packages/disposabl-kit/client/vite.config.ts +3 -2
- package/dist/templates/default/packages/disposabl-kit/lib/telemetry.ts +148 -12
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/account-billing.tsx +143 -40
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/paywall.ts +83 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/policy.ts +125 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/session.ts +31 -3
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/topup.ts +99 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/use-topup.ts +116 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/web-checkout.ts +190 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/expo-plugin.cjs +23 -1
- package/dist/templates/default/packages/disposabl-kit/mobile/layout.tsx +9 -2
- package/dist/templates/default/packages/disposabl-kit/package.json +10 -1
- package/dist/templates/default/packages/disposabl-kit/payments/checkout-attempt.ts +64 -0
- package/dist/templates/default/packages/disposabl-kit/payments/config.ts +16 -3
- package/dist/templates/default/packages/disposabl-kit/server/billing-functions.ts +218 -9
- package/dist/templates/default/packages/disposabl-kit/server/billing-handoff.ts +126 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing-native-capability.ts +12 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing-setup.ts +2 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing.ts +31 -0
- package/dist/templates/default/packages/disposabl-kit/server/browser-session.ts +27 -0
- package/dist/templates/default/packages/disposabl-kit/server/lib/telemetry.ts +173 -59
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-contract.ts +233 -0
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-functions.ts +554 -0
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-validator.ts +76 -0
- package/dist/templates/default/packages/reconfigure-skill/SKILL.md +38 -0
- package/dist/templates/default/packages/sdk/README.md +21 -0
- package/dist/templates/default/packages/sdk/src/analytics.ts +102 -0
- package/dist/templates/default/packages/sdk/src/index.ts +2 -0
- package/dist/templates/default/packages/skill/SKILL.md +2 -0
- package/dist/templates/default/packages/skill/references/agent.md +2 -0
- package/dist/templates/default/packages/tests/android-architectures.test.ts +43 -0
- package/dist/templates/default/packages/tests/android-ota-artifacts.test.ts +5 -1
- package/dist/templates/default/packages/tests/android-ota-selection.test.ts +7 -2
- package/dist/templates/default/packages/tests/billing-attempts.test.ts +156 -0
- package/dist/templates/default/packages/tests/billing-handoff.test.ts +116 -0
- package/dist/templates/default/packages/tests/billing-setup.test.ts +4 -3
- package/dist/templates/default/packages/tests/billing.test.ts +48 -4
- package/dist/templates/default/packages/tests/desktop-packaging.test.ts +12 -0
- package/dist/templates/default/packages/tests/flows/seo.e2e.ts +3 -1
- package/dist/templates/default/packages/tests/mobile-android-e2e-network.test.ts +112 -0
- package/dist/templates/default/packages/tests/native-billing.test.ts +417 -0
- package/dist/templates/default/packages/tests/page-telemetry.test.ts +218 -0
- package/dist/templates/default/packages/tests/revenuecat-inbox.test.ts +476 -0
- package/dist/templates/default/packages/tests/sdk-analytics.test.ts +158 -0
- package/dist/templates/default/packages/tests/telemetry.test.ts +272 -55
- package/package.json +1 -1
- package/dist/templates/default/apps/web/src/routeTree.gen.ts +0 -578
|
@@ -5,12 +5,21 @@ import { AppState, Linking, Platform, Text, View } from "react-native";
|
|
|
5
5
|
import type { PurchasesPackage } from "react-native-purchases";
|
|
6
6
|
import { AuthButton, authClasses } from "../components/auth-ui";
|
|
7
7
|
import { billingSession } from "./session";
|
|
8
|
-
import {
|
|
8
|
+
import { presentMonthlyPaywall, validMonthlyOffering } from "./paywall";
|
|
9
|
+
import { creditQuantity, monthlyLabel } from "../../payments/config";
|
|
10
|
+
import { paymentOptions, storeEligibility } from "./policy";
|
|
11
|
+
import { useWebCheckout } from "./web-checkout";
|
|
12
|
+
import { useTopup } from "./use-topup";
|
|
13
|
+
import type { TopupRoute } from "./topup";
|
|
9
14
|
|
|
10
15
|
type Snapshot = {
|
|
11
16
|
balance: number | null;
|
|
12
17
|
hasMonthlyPlan: boolean | null;
|
|
18
|
+
monthlyCheckoutPending: boolean;
|
|
13
19
|
nativeSubscriptionsEnabled: boolean;
|
|
20
|
+
nativeTopupRoutes: TopupRoute[];
|
|
21
|
+
mobileWebCheckoutEnabled: boolean;
|
|
22
|
+
customerId: string;
|
|
14
23
|
};
|
|
15
24
|
const snapshot = makeFunctionReference<
|
|
16
25
|
"action",
|
|
@@ -49,24 +58,38 @@ export function AccountBilling({ customerId }: { customerId: string }) {
|
|
|
49
58
|
const sequence = ++refreshSequence.current;
|
|
50
59
|
const latest = () =>
|
|
51
60
|
current(version) && sequence === refreshSequence.current;
|
|
61
|
+
if (latest()) setMonthly(null);
|
|
52
62
|
try {
|
|
53
63
|
const result = await getSnapshot({});
|
|
54
64
|
if (!latest()) return;
|
|
55
65
|
setData(result);
|
|
56
66
|
if (key && productId && result.nativeSubscriptionsEnabled) {
|
|
57
|
-
const
|
|
67
|
+
const store = await billingSession.run(
|
|
58
68
|
customerId,
|
|
59
69
|
version,
|
|
60
70
|
key,
|
|
61
|
-
(sdk) =>
|
|
71
|
+
async (sdk) => ({
|
|
72
|
+
eligibility: await storeEligibility(sdk, Platform.OS),
|
|
73
|
+
offerings: await sdk.getOfferings(),
|
|
74
|
+
}),
|
|
62
75
|
);
|
|
76
|
+
const options = paymentOptions({
|
|
77
|
+
platform: Platform.OS,
|
|
78
|
+
...store.eligibility,
|
|
79
|
+
paymentsEnabled: true,
|
|
80
|
+
nativeReady: result.nativeSubscriptionsEnabled,
|
|
81
|
+
webCheckoutEnabled: result.mobileWebCheckoutEnabled,
|
|
82
|
+
});
|
|
63
83
|
if (latest())
|
|
64
84
|
setMonthly(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
85
|
+
options.native &&
|
|
86
|
+
store.offerings.current &&
|
|
87
|
+
validMonthlyOffering(
|
|
88
|
+
store.offerings.current.availablePackages,
|
|
89
|
+
productId,
|
|
90
|
+
)
|
|
91
|
+
? (store.offerings.current.availablePackages[0] ?? null)
|
|
92
|
+
: null,
|
|
70
93
|
);
|
|
71
94
|
} else setMonthly(null);
|
|
72
95
|
if (latest()) setRefreshError("");
|
|
@@ -81,7 +104,7 @@ export function AccountBilling({ customerId }: { customerId: string }) {
|
|
|
81
104
|
);
|
|
82
105
|
|
|
83
106
|
useEffect(() => {
|
|
84
|
-
const version = billingSession.
|
|
107
|
+
const version = billingSession.acquire(customerId);
|
|
85
108
|
generation.current = version;
|
|
86
109
|
const update = () => void refresh(version);
|
|
87
110
|
update();
|
|
@@ -94,50 +117,90 @@ export function AccountBilling({ customerId }: { customerId: string }) {
|
|
|
94
117
|
};
|
|
95
118
|
}, [customerId, current, refresh]);
|
|
96
119
|
|
|
120
|
+
const webSnapshot = useCallback(() => getSnapshot({}), [getSnapshot]);
|
|
121
|
+
const web = useWebCheckout(
|
|
122
|
+
customerId,
|
|
123
|
+
generation,
|
|
124
|
+
key,
|
|
125
|
+
productId,
|
|
126
|
+
webSnapshot,
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const refreshBalance = useCallback(
|
|
130
|
+
() => refresh(generation.current),
|
|
131
|
+
[refresh],
|
|
132
|
+
);
|
|
133
|
+
const topup = useTopup(
|
|
134
|
+
customerId,
|
|
135
|
+
generation,
|
|
136
|
+
key,
|
|
137
|
+
webSnapshot,
|
|
138
|
+
refreshBalance,
|
|
139
|
+
);
|
|
140
|
+
|
|
97
141
|
async function transact(restore: boolean) {
|
|
98
|
-
if (
|
|
142
|
+
if (
|
|
143
|
+
operation.current ||
|
|
144
|
+
topup.busy ||
|
|
145
|
+
!key ||
|
|
146
|
+
!billingSession.beginTransaction()
|
|
147
|
+
)
|
|
148
|
+
return;
|
|
99
149
|
operation.current = true;
|
|
100
150
|
const version = generation.current;
|
|
101
151
|
setBusy(true);
|
|
102
152
|
setMessage("");
|
|
103
|
-
let completed =
|
|
153
|
+
let completed: "purchased" | "restored" | null = null;
|
|
104
154
|
try {
|
|
105
|
-
await billingSession.run(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
155
|
+
const outcome = await billingSession.run(
|
|
156
|
+
customerId,
|
|
157
|
+
version,
|
|
158
|
+
key,
|
|
159
|
+
async (sdk) => {
|
|
160
|
+
if (restore) {
|
|
161
|
+
const latest = await getSnapshot({});
|
|
162
|
+
if (!current(version)) throw new Error("Account changed.");
|
|
163
|
+
if (!latest.nativeSubscriptionsEnabled)
|
|
164
|
+
throw new Error("Not configured.");
|
|
165
|
+
await sdk.restorePurchases();
|
|
166
|
+
return "restored" as const;
|
|
167
|
+
}
|
|
168
|
+
if (!monthly || !productId) throw new Error("Product unavailable.");
|
|
169
|
+
return presentMonthlyPaywall(
|
|
170
|
+
sdk,
|
|
171
|
+
() => getSnapshot({}),
|
|
172
|
+
() => current(version),
|
|
173
|
+
productId,
|
|
174
|
+
);
|
|
175
|
+
},
|
|
176
|
+
);
|
|
177
|
+
completed = outcome === "cancelled" ? null : outcome;
|
|
178
|
+
if (outcome === "cancelled" && current(version))
|
|
179
|
+
setMessage("Purchase cancelled.");
|
|
123
180
|
} catch (error) {
|
|
124
181
|
if (current(version))
|
|
125
182
|
setMessage(
|
|
126
183
|
(error as { userCancelled?: boolean })?.userCancelled
|
|
127
184
|
? "Purchase cancelled."
|
|
128
185
|
: error instanceof Error &&
|
|
129
|
-
error.message ===
|
|
130
|
-
|
|
131
|
-
|
|
186
|
+
error.message ===
|
|
187
|
+
"Finish or cancel your website checkout before subscribing here."
|
|
188
|
+
? error.message
|
|
189
|
+
: error instanceof Error &&
|
|
190
|
+
error.message === "existing_subscription"
|
|
191
|
+
? "You already have a subscription. Manage it where you purchased it."
|
|
192
|
+
: "Could not complete the store request. Refresh before trying again.",
|
|
132
193
|
);
|
|
133
194
|
} finally {
|
|
134
195
|
operation.current = false;
|
|
196
|
+
billingSession.endTransaction();
|
|
135
197
|
if (current(version)) setBusy(false);
|
|
136
198
|
}
|
|
137
199
|
if (completed && current(version)) {
|
|
138
|
-
const confirmation =
|
|
139
|
-
|
|
140
|
-
|
|
200
|
+
const confirmation =
|
|
201
|
+
completed === "restored"
|
|
202
|
+
? "Restore check completed. Any eligible purchases will sync to your account."
|
|
203
|
+
: "Purchase received. Credits appear after payment sync.";
|
|
141
204
|
setMessage(confirmation);
|
|
142
205
|
await refresh(version);
|
|
143
206
|
}
|
|
@@ -150,17 +213,29 @@ export function AccountBilling({ customerId }: { customerId: string }) {
|
|
|
150
213
|
Available: {data?.balance ?? "unavailable"}
|
|
151
214
|
</Text>
|
|
152
215
|
<Text className={authClasses.body}>
|
|
153
|
-
{creditQuantity("monthly")} credits each
|
|
216
|
+
{monthlyLabel()}: {creditQuantity("monthly")} credits each billing
|
|
217
|
+
cycle. Unused monthly credits expire.
|
|
154
218
|
</Text>
|
|
155
219
|
{data?.hasMonthlyPlan && (
|
|
156
220
|
<Text className={authClasses.body}>
|
|
157
221
|
Your monthly plan is active or scheduled.
|
|
158
222
|
</Text>
|
|
159
223
|
)}
|
|
224
|
+
{data?.monthlyCheckoutPending && (
|
|
225
|
+
<Text className={authClasses.body}>
|
|
226
|
+
Finish or cancel your website checkout before subscribing here.
|
|
227
|
+
</Text>
|
|
228
|
+
)}
|
|
160
229
|
{monthly ? (
|
|
161
230
|
<AuthButton
|
|
162
231
|
title={`${monthly.product.priceString} / month`}
|
|
163
|
-
disabled={
|
|
232
|
+
disabled={
|
|
233
|
+
busy ||
|
|
234
|
+
web.busy ||
|
|
235
|
+
topup.busy ||
|
|
236
|
+
data?.hasMonthlyPlan !== false ||
|
|
237
|
+
data?.monthlyCheckoutPending !== false
|
|
238
|
+
}
|
|
164
239
|
onPress={() => void transact(false)}
|
|
165
240
|
/>
|
|
166
241
|
) : (
|
|
@@ -168,9 +243,32 @@ export function AccountBilling({ customerId }: { customerId: string }) {
|
|
|
168
243
|
Store subscriptions are unavailable.
|
|
169
244
|
</Text>
|
|
170
245
|
)}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
246
|
+
{web.available && (
|
|
247
|
+
<AuthButton
|
|
248
|
+
title="Buy monthly on website"
|
|
249
|
+
disabled={busy || web.busy || topup.busy}
|
|
250
|
+
onPress={() => void web.open()}
|
|
251
|
+
/>
|
|
252
|
+
)}
|
|
253
|
+
{topup.product && (
|
|
254
|
+
<AuthButton
|
|
255
|
+
title={`Buy ${creditQuantity("topup")} credits · ${topup.product.priceString}`}
|
|
256
|
+
disabled={busy || web.busy || topup.busy}
|
|
257
|
+
onPress={() => void topup.purchase()}
|
|
258
|
+
/>
|
|
259
|
+
)}
|
|
260
|
+
{web.topupAvailable && (
|
|
261
|
+
<AuthButton
|
|
262
|
+
title="Buy top-up on website"
|
|
263
|
+
disabled={busy || web.busy || topup.busy}
|
|
264
|
+
onPress={() => void web.open("topup")}
|
|
265
|
+
/>
|
|
266
|
+
)}
|
|
267
|
+
{!!topup.message && (
|
|
268
|
+
<Text accessibilityRole="alert" className={authClasses.body}>
|
|
269
|
+
{topup.message}
|
|
270
|
+
</Text>
|
|
271
|
+
)}
|
|
174
272
|
<AuthButton
|
|
175
273
|
title="Refresh credits"
|
|
176
274
|
disabled={busy}
|
|
@@ -218,6 +316,11 @@ export function AccountBilling({ customerId }: { customerId: string }) {
|
|
|
218
316
|
{message}
|
|
219
317
|
</Text>
|
|
220
318
|
)}
|
|
319
|
+
{!!web.error && (
|
|
320
|
+
<Text accessibilityRole="alert" className={authClasses.body}>
|
|
321
|
+
{web.error}
|
|
322
|
+
</Text>
|
|
323
|
+
)}
|
|
221
324
|
</View>
|
|
222
325
|
);
|
|
223
326
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type Purchases from "react-native-purchases";
|
|
2
|
+
import type { PurchasesOffering } from "react-native-purchases";
|
|
3
|
+
|
|
4
|
+
export type BillingSnapshot = {
|
|
5
|
+
balance: number | null;
|
|
6
|
+
hasMonthlyPlan: boolean | null;
|
|
7
|
+
nativeSubscriptionsEnabled: boolean;
|
|
8
|
+
monthlyCheckoutPending: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type Store = Pick<
|
|
12
|
+
typeof Purchases,
|
|
13
|
+
"getCustomerInfo" | "getOfferings" | "canMakePayments"
|
|
14
|
+
>;
|
|
15
|
+
type Outcome = "purchased" | "restored" | "cancelled";
|
|
16
|
+
|
|
17
|
+
/** Reject mixed offerings until top-up synchronization is verified. */
|
|
18
|
+
export function validMonthlyOffering(
|
|
19
|
+
packages: {
|
|
20
|
+
product: { identifier: string; subscriptionPeriod: string | null };
|
|
21
|
+
}[],
|
|
22
|
+
productId: string,
|
|
23
|
+
) {
|
|
24
|
+
return (
|
|
25
|
+
packages.length === 1 &&
|
|
26
|
+
packages[0].product.identifier === productId &&
|
|
27
|
+
packages[0].product.subscriptionPeriod === "P1M"
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Must run inside BillingSession.run so identity changes are serialized. */
|
|
32
|
+
export async function presentMonthlyPaywall(
|
|
33
|
+
sdk: Store,
|
|
34
|
+
getSnapshot: () => Promise<BillingSnapshot>,
|
|
35
|
+
current: () => boolean,
|
|
36
|
+
productId: string,
|
|
37
|
+
present: (
|
|
38
|
+
offering: PurchasesOffering,
|
|
39
|
+
current: () => boolean,
|
|
40
|
+
) => Promise<Outcome> = showPaywall,
|
|
41
|
+
): Promise<Outcome> {
|
|
42
|
+
const check = () => {
|
|
43
|
+
if (!current()) throw new Error("Account changed.");
|
|
44
|
+
};
|
|
45
|
+
const snapshot = await getSnapshot();
|
|
46
|
+
check();
|
|
47
|
+
if (snapshot.monthlyCheckoutPending !== false)
|
|
48
|
+
throw new Error(
|
|
49
|
+
"Finish or cancel your website checkout before subscribing here.",
|
|
50
|
+
);
|
|
51
|
+
if (!snapshot.nativeSubscriptionsEnabled || snapshot.hasMonthlyPlan !== false)
|
|
52
|
+
throw new Error("existing_subscription");
|
|
53
|
+
if (!(await sdk.canMakePayments()))
|
|
54
|
+
throw new Error("Payments are restricted.");
|
|
55
|
+
check();
|
|
56
|
+
const info = await sdk.getCustomerInfo();
|
|
57
|
+
check();
|
|
58
|
+
if (info.activeSubscriptions.length) throw new Error("existing_subscription");
|
|
59
|
+
const offering = (await sdk.getOfferings()).current;
|
|
60
|
+
check();
|
|
61
|
+
if (!offering || !validMonthlyOffering(offering.availablePackages, productId))
|
|
62
|
+
throw new Error("Store offering is unavailable.");
|
|
63
|
+
const outcome = await present(offering, current);
|
|
64
|
+
check();
|
|
65
|
+
return outcome;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function showPaywall(
|
|
69
|
+
offering: PurchasesOffering,
|
|
70
|
+
current: () => boolean,
|
|
71
|
+
): Promise<Outcome> {
|
|
72
|
+
const { default: ui, PAYWALL_RESULT } =
|
|
73
|
+
await import("react-native-purchases-ui");
|
|
74
|
+
if (!current()) throw new Error("Account changed.");
|
|
75
|
+
const result = await ui.presentPaywall({
|
|
76
|
+
offering,
|
|
77
|
+
displayCloseButton: true,
|
|
78
|
+
});
|
|
79
|
+
if (result === PAYWALL_RESULT.PURCHASED) return "purchased";
|
|
80
|
+
if (result === PAYWALL_RESULT.RESTORED) return "restored";
|
|
81
|
+
if (result === PAYWALL_RESULT.CANCELLED) return "cancelled";
|
|
82
|
+
throw new Error("Store offering is unavailable.");
|
|
83
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type Purchases from "react-native-purchases";
|
|
2
|
+
import { validMonthlyOffering, type BillingSnapshot } from "./paywall";
|
|
3
|
+
import { loadTopup, matchingTopupRoute, type TopupSnapshot } from "./topup";
|
|
4
|
+
|
|
5
|
+
export function paymentOptions(input: {
|
|
6
|
+
platform: string;
|
|
7
|
+
storefront: string | null;
|
|
8
|
+
paymentsEnabled: boolean;
|
|
9
|
+
webCheckoutEnabled: boolean;
|
|
10
|
+
nativeReady: boolean;
|
|
11
|
+
canMakePayments: boolean;
|
|
12
|
+
}) {
|
|
13
|
+
const native =
|
|
14
|
+
input.paymentsEnabled && input.nativeReady && input.canMakePayments;
|
|
15
|
+
return {
|
|
16
|
+
native,
|
|
17
|
+
web:
|
|
18
|
+
native &&
|
|
19
|
+
input.platform === "ios" &&
|
|
20
|
+
input.storefront === "USA" &&
|
|
21
|
+
input.webCheckoutEnabled,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Read only through the configured, serialized billing session. */
|
|
26
|
+
export async function storeEligibility(
|
|
27
|
+
sdk: Pick<typeof Purchases, "canMakePayments" | "getStorefront">,
|
|
28
|
+
platform: string,
|
|
29
|
+
) {
|
|
30
|
+
const canMakePayments = await sdk.canMakePayments();
|
|
31
|
+
const storefront =
|
|
32
|
+
platform === "ios" ? await sdk.getStorefront().catch(() => null) : null;
|
|
33
|
+
return { canMakePayments, storefront: storefront?.countryCode ?? null };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Recheck immediately before opening the browser, within the billing session. */
|
|
37
|
+
export async function preflightWebMonthly(
|
|
38
|
+
sdk: Pick<
|
|
39
|
+
typeof Purchases,
|
|
40
|
+
"canMakePayments" | "getStorefront" | "getCustomerInfo" | "getOfferings"
|
|
41
|
+
>,
|
|
42
|
+
platform: string,
|
|
43
|
+
productId: string,
|
|
44
|
+
customerId: string,
|
|
45
|
+
snapshot: () => Promise<
|
|
46
|
+
BillingSnapshot & { mobileWebCheckoutEnabled: boolean; customerId: string }
|
|
47
|
+
>,
|
|
48
|
+
current: () => boolean,
|
|
49
|
+
) {
|
|
50
|
+
const check = () => {
|
|
51
|
+
if (!current()) throw new Error("Account changed.");
|
|
52
|
+
};
|
|
53
|
+
const latest = await snapshot();
|
|
54
|
+
check();
|
|
55
|
+
if (latest.customerId !== customerId) throw new Error("Account changed.");
|
|
56
|
+
const eligibility = await storeEligibility(sdk, platform);
|
|
57
|
+
check();
|
|
58
|
+
const offering = (await sdk.getOfferings()).current;
|
|
59
|
+
check();
|
|
60
|
+
const options = paymentOptions({
|
|
61
|
+
platform,
|
|
62
|
+
...eligibility,
|
|
63
|
+
paymentsEnabled: true,
|
|
64
|
+
webCheckoutEnabled: latest.mobileWebCheckoutEnabled,
|
|
65
|
+
nativeReady:
|
|
66
|
+
latest.nativeSubscriptionsEnabled &&
|
|
67
|
+
!!offering &&
|
|
68
|
+
validMonthlyOffering(offering.availablePackages, productId),
|
|
69
|
+
});
|
|
70
|
+
if (!options.web) throw new Error("Website checkout is unavailable.");
|
|
71
|
+
if (latest.hasMonthlyPlan !== false) throw new Error("existing_subscription");
|
|
72
|
+
const customer = await sdk.getCustomerInfo();
|
|
73
|
+
check();
|
|
74
|
+
if (customer.activeSubscriptions.length)
|
|
75
|
+
throw new Error("existing_subscription");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The matching route and real consumable must still exist at the moment of handoff. */
|
|
79
|
+
export async function preflightWebTopup(
|
|
80
|
+
sdk: Pick<
|
|
81
|
+
typeof Purchases,
|
|
82
|
+
| "canMakePayments"
|
|
83
|
+
| "getStorefront"
|
|
84
|
+
| "getAppUserID"
|
|
85
|
+
| "getProducts"
|
|
86
|
+
| "PRODUCT_CATEGORY"
|
|
87
|
+
| "purchaseStoreProduct"
|
|
88
|
+
>,
|
|
89
|
+
platform: string,
|
|
90
|
+
app: string | undefined,
|
|
91
|
+
productId: string | undefined,
|
|
92
|
+
customerId: string,
|
|
93
|
+
snapshot: () => Promise<
|
|
94
|
+
TopupSnapshot & { mobileWebCheckoutEnabled: boolean }
|
|
95
|
+
>,
|
|
96
|
+
current: () => boolean,
|
|
97
|
+
) {
|
|
98
|
+
const check = () => {
|
|
99
|
+
if (!current()) throw new Error("Account changed.");
|
|
100
|
+
};
|
|
101
|
+
const latest = await snapshot();
|
|
102
|
+
check();
|
|
103
|
+
if (
|
|
104
|
+
latest.customerId !== customerId ||
|
|
105
|
+
!matchingTopupRoute(latest, platform, app, productId)
|
|
106
|
+
)
|
|
107
|
+
throw new Error("Top-up unavailable.");
|
|
108
|
+
const eligibility = await storeEligibility(sdk, platform);
|
|
109
|
+
check();
|
|
110
|
+
if ((await sdk.getAppUserID()) !== customerId)
|
|
111
|
+
throw new Error("Account changed.");
|
|
112
|
+
check();
|
|
113
|
+
const product = await loadTopup(sdk, platform, productId!);
|
|
114
|
+
check();
|
|
115
|
+
if (
|
|
116
|
+
!paymentOptions({
|
|
117
|
+
platform,
|
|
118
|
+
...eligibility,
|
|
119
|
+
paymentsEnabled: true,
|
|
120
|
+
webCheckoutEnabled: latest.mobileWebCheckoutEnabled,
|
|
121
|
+
nativeReady: !!product,
|
|
122
|
+
}).web
|
|
123
|
+
)
|
|
124
|
+
throw new Error("Website checkout is unavailable.");
|
|
125
|
+
}
|
|
@@ -6,6 +6,8 @@ type SDK = typeof Purchases;
|
|
|
6
6
|
export class BillingSession {
|
|
7
7
|
private generation = 0;
|
|
8
8
|
private owner: string | null = null;
|
|
9
|
+
private leases = new Set<number>();
|
|
10
|
+
private transaction = false;
|
|
9
11
|
private queue: Promise<unknown> = Promise.resolve();
|
|
10
12
|
private sdk: SDK | undefined;
|
|
11
13
|
private configuredKey: string | undefined;
|
|
@@ -15,15 +17,41 @@ export class BillingSession {
|
|
|
15
17
|
|
|
16
18
|
select(owner: string | null) {
|
|
17
19
|
this.owner = owner;
|
|
18
|
-
|
|
20
|
+
this.leases.clear();
|
|
21
|
+
const generation = ++this.generation;
|
|
22
|
+
if (owner) this.leases.add(generation);
|
|
23
|
+
return generation;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
acquire(owner: string) {
|
|
27
|
+
if (this.owner !== owner) {
|
|
28
|
+
this.owner = owner;
|
|
29
|
+
this.leases.clear();
|
|
30
|
+
}
|
|
31
|
+
const generation = ++this.generation;
|
|
32
|
+
this.leases.add(generation);
|
|
33
|
+
return generation;
|
|
19
34
|
}
|
|
20
35
|
|
|
21
36
|
current(owner: string, generation: number) {
|
|
22
|
-
return this.owner === owner && this.generation
|
|
37
|
+
return this.owner === owner && this.leases.has(generation);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
beginTransaction() {
|
|
41
|
+
if (this.transaction) return false;
|
|
42
|
+
this.transaction = true;
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
endTransaction() {
|
|
47
|
+
this.transaction = false;
|
|
23
48
|
}
|
|
24
49
|
|
|
25
50
|
release(owner: string, generation: number) {
|
|
26
|
-
if (this.current(owner, generation))
|
|
51
|
+
if (this.current(owner, generation)) {
|
|
52
|
+
this.leases.delete(generation);
|
|
53
|
+
if (!this.leases.size) this.owner = null;
|
|
54
|
+
}
|
|
27
55
|
}
|
|
28
56
|
|
|
29
57
|
run<T>(
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type Purchases from "react-native-purchases";
|
|
2
|
+
import type { PurchasesStoreProduct } from "react-native-purchases";
|
|
3
|
+
|
|
4
|
+
export type TopupRoute = { app: string; store: string; product: string };
|
|
5
|
+
export type TopupSnapshot = {
|
|
6
|
+
customerId: string;
|
|
7
|
+
nativeTopupRoutes: TopupRoute[];
|
|
8
|
+
};
|
|
9
|
+
type SDK = Pick<
|
|
10
|
+
typeof Purchases,
|
|
11
|
+
| "getProducts"
|
|
12
|
+
| "purchaseStoreProduct"
|
|
13
|
+
| "getAppUserID"
|
|
14
|
+
| "canMakePayments"
|
|
15
|
+
| "PRODUCT_CATEGORY"
|
|
16
|
+
>;
|
|
17
|
+
|
|
18
|
+
export function matchingTopupRoute(
|
|
19
|
+
snapshot: TopupSnapshot,
|
|
20
|
+
platform: string,
|
|
21
|
+
app: string | undefined,
|
|
22
|
+
product: string | undefined,
|
|
23
|
+
) {
|
|
24
|
+
const store =
|
|
25
|
+
platform === "ios"
|
|
26
|
+
? "APP_STORE"
|
|
27
|
+
: platform === "android"
|
|
28
|
+
? "PLAY_STORE"
|
|
29
|
+
: null;
|
|
30
|
+
return (
|
|
31
|
+
!!app &&
|
|
32
|
+
!!product &&
|
|
33
|
+
!!store &&
|
|
34
|
+
snapshot.nativeTopupRoutes.some(
|
|
35
|
+
(route) =>
|
|
36
|
+
route.app === app && route.store === store && route.product === product,
|
|
37
|
+
)
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function loadTopup(
|
|
42
|
+
sdk: SDK,
|
|
43
|
+
platform: string,
|
|
44
|
+
productId: string,
|
|
45
|
+
): Promise<PurchasesStoreProduct | null> {
|
|
46
|
+
const products = await sdk.getProducts(
|
|
47
|
+
[productId],
|
|
48
|
+
sdk.PRODUCT_CATEGORY.NON_SUBSCRIPTION,
|
|
49
|
+
);
|
|
50
|
+
if (products.length !== 1) return null;
|
|
51
|
+
const product = products[0]!;
|
|
52
|
+
if (
|
|
53
|
+
product.identifier !== productId ||
|
|
54
|
+
product.productCategory !== "NON_SUBSCRIPTION" ||
|
|
55
|
+
!product.priceString
|
|
56
|
+
)
|
|
57
|
+
return null;
|
|
58
|
+
// StoreKit 1 cannot prove the type. Play's SDK may return UNKNOWN; its
|
|
59
|
+
// consumable configuration must be independently verified by the operator.
|
|
60
|
+
if (
|
|
61
|
+
platform === "ios"
|
|
62
|
+
? product.productType !== "CONSUMABLE"
|
|
63
|
+
: platform !== "android" ||
|
|
64
|
+
!["CONSUMABLE", "UNKNOWN"].includes(product.productType)
|
|
65
|
+
)
|
|
66
|
+
return null;
|
|
67
|
+
return product;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Run inside the billing session and transaction lease. */
|
|
71
|
+
export async function purchaseTopup(
|
|
72
|
+
sdk: SDK,
|
|
73
|
+
snapshot: () => Promise<TopupSnapshot>,
|
|
74
|
+
current: () => boolean,
|
|
75
|
+
customerId: string,
|
|
76
|
+
platform: string,
|
|
77
|
+
app: string | undefined,
|
|
78
|
+
productId: string | undefined,
|
|
79
|
+
) {
|
|
80
|
+
const check = () => {
|
|
81
|
+
if (!current()) throw new Error("Account changed.");
|
|
82
|
+
};
|
|
83
|
+
const latest = await snapshot();
|
|
84
|
+
check();
|
|
85
|
+
if (
|
|
86
|
+
latest.customerId !== customerId ||
|
|
87
|
+
!matchingTopupRoute(latest, platform, app, productId)
|
|
88
|
+
)
|
|
89
|
+
throw new Error("Top-up unavailable.");
|
|
90
|
+
if (!(await sdk.canMakePayments())) throw new Error("Payments restricted.");
|
|
91
|
+
check();
|
|
92
|
+
if ((await sdk.getAppUserID()) !== customerId)
|
|
93
|
+
throw new Error("Account changed.");
|
|
94
|
+
check();
|
|
95
|
+
const product = await loadTopup(sdk, platform, productId!);
|
|
96
|
+
check();
|
|
97
|
+
if (!product) throw new Error("Top-up unavailable.");
|
|
98
|
+
await sdk.purchaseStoreProduct(product);
|
|
99
|
+
}
|