@pithy-sh/payments 0.6.0 → 0.7.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 +6 -1
- package/dist/admin/coverage.d.ts.map +1 -1
- package/dist/admin/coverage.js +13 -7
- package/dist/admin/read.d.ts +60 -0
- package/dist/admin/read.d.ts.map +1 -1
- package/dist/admin/read.js +86 -2
- package/dist/audit/actions.d.ts +5 -0
- package/dist/audit/actions.d.ts.map +1 -1
- package/dist/audit/actions.js +6 -1
- package/dist/capability.d.ts +3 -3
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +14 -7
- package/dist/client/api.d.ts +25 -9
- package/dist/client/api.d.ts.map +1 -1
- package/dist/client/api.js +68 -12
- package/dist/client/hooks.d.ts +27 -39
- package/dist/client/hooks.d.ts.map +1 -1
- package/dist/client/hooks.js +166 -315
- package/dist/client/projection.d.ts +25 -18
- package/dist/client/projection.d.ts.map +1 -1
- package/dist/client/railChoice.d.ts +46 -0
- package/dist/client/railChoice.d.ts.map +1 -0
- package/dist/client/railChoice.js +48 -0
- package/dist/data/webhookEvent.d.ts +67 -0
- package/dist/data/webhookEvent.d.ts.map +1 -1
- package/dist/data/webhookEvent.js +76 -1
- package/dist/http/responses.d.ts +64 -0
- package/dist/http/responses.d.ts.map +1 -1
- package/dist/http/responses.js +34 -1
- package/dist/http/routes.d.ts +1 -0
- package/dist/http/routes.d.ts.map +1 -1
- package/dist/http/routes.js +64 -11
- package/dist/http/schemas.d.ts +19 -0
- package/dist/http/schemas.d.ts.map +1 -1
- package/dist/http/schemas.js +18 -1
- package/dist/http/scopes.d.ts +15 -6
- package/dist/http/scopes.d.ts.map +1 -1
- package/dist/http/scopes.js +55 -5
- package/dist/http/view.d.ts +9 -2
- package/dist/http/view.d.ts.map +1 -1
- package/dist/http/view.js +29 -1
- package/dist/http/webhookGuard.d.ts +7 -1
- package/dist/http/webhookGuard.d.ts.map +1 -1
- package/dist/http/webhookGuard.js +3 -2
- package/dist/migrations/0003_webhook_targets.d.ts +19 -0
- package/dist/migrations/0003_webhook_targets.d.ts.map +1 -0
- package/dist/migrations/0003_webhook_targets.js +29 -0
- package/dist/processing/activities.d.ts +9 -0
- package/dist/processing/activities.d.ts.map +1 -0
- package/dist/processing/activities.js +30 -0
- package/dist/projection/orphans.d.ts.map +1 -1
- package/dist/projection/orphans.js +15 -6
- package/dist/projection/targets.d.ts +50 -0
- package/dist/projection/targets.d.ts.map +1 -0
- package/dist/projection/targets.js +50 -0
- package/dist/purge/contribution.d.ts +32 -0
- package/dist/purge/contribution.d.ts.map +1 -0
- package/dist/purge/contribution.js +120 -0
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/dist/workflows/paddleSweep.d.ts.map +1 -1
- package/dist/workflows/paddleSweep.js +20 -9
- package/docs/lemon-squeezy.md +1 -1
- package/docs/paddle.md +2 -0
- package/package.json +6 -4
- package/src/admin/coverage.ts +13 -9
- package/src/admin/read.ts +135 -0
- package/src/audit/actions.ts +5 -0
- package/src/capability.ts +25 -15
- package/src/client/api.ts +74 -11
- package/src/client/hooks.ts +206 -301
- package/src/client/projection.ts +25 -18
- package/src/client/railChoice.ts +72 -0
- package/src/data/webhookEvent.ts +115 -0
- package/src/http/responses.ts +66 -0
- package/src/http/routes.ts +73 -11
- package/src/http/schemas.ts +26 -0
- package/src/http/scopes.ts +58 -6
- package/src/http/view.ts +31 -1
- package/src/http/webhookGuard.ts +23 -3
- package/src/migrations/0003_webhook_targets.ts +34 -0
- package/src/processing/activities.ts +34 -0
- package/src/projection/orphans.ts +28 -11
- package/src/projection/targets.ts +66 -0
- package/src/purge/contribution.ts +135 -0
- package/src/version.generated.ts +1 -1
- package/src/workflows/paddleSweep.ts +39 -14
package/dist/client/hooks.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
|
-
import { getAllowances, getEntitlements, openBillingPortal, openStoreSubscriptions, restorePurchases, resumeCheckout, startCheckout, submitPurchase } from "./api.js";
|
|
3
|
+
import { PAYMENTS_UNREADABLE, getAllowances, getEntitlements, openBillingPortal, openStoreSubscriptions, restorePurchases, resumeCheckout, startCheckout, submitPurchase } from "./api.js";
|
|
4
4
|
import { loadPaddle, previewPrices, priceQueryKey } from "./paddle.js";
|
|
5
5
|
import { openPaddleCheckout } from "./checkout.js";
|
|
6
6
|
import { readPaddleLinkTransaction } from "./paddleLink.js";
|
|
7
|
-
import {
|
|
7
|
+
import { useAction } from "@pithy-sh/ui-core/src/action";
|
|
8
|
+
import { useRead } from "@pithy-sh/ui-core/src/read";
|
|
9
|
+
import { useCallback, useRef, useState } from "react";
|
|
8
10
|
//#region src/client/hooks.ts
|
|
9
11
|
/**
|
|
10
12
|
* The headless client surface: six hooks a paywall, a pricing page, a subscription screen, and a route
|
|
@@ -22,8 +24,9 @@ import { useCallback, useEffect, useRef, useState } from "react";
|
|
|
22
24
|
* (`@pithy-sh/payments/src/client/hooks`). It is deliberately absent from `src/index.ts` — that is what
|
|
23
25
|
* keeps React out of a Worker bundle that composes payments.
|
|
24
26
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
+
* The transport is whatever the options name, because `./api` decides it: with no `origin`, same-origin
|
|
28
|
+
* with `credentials: "include"` and no token in web storage; with an `origin` and the bearer session's
|
|
29
|
+
* `fetch`, a native app's request that sends no cookie. These hooks hold no credential either way.
|
|
27
30
|
*
|
|
28
31
|
* **Nothing here throws, and nothing here is a security boundary.** A refusal is a message to render, and
|
|
29
32
|
* the server's `requireEntitlement()` is the gate; these exist so a user is sent to the paywall rather than
|
|
@@ -38,26 +41,32 @@ import { useCallback, useEffect, useRef, useState } from "react";
|
|
|
38
41
|
/**
|
|
39
42
|
* Hold the newest value without making it a dependency.
|
|
40
43
|
*
|
|
41
|
-
* Every hook below takes an options object, and a caller writing `useEntitlement("pro", { basePath })`
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* the rest through this.
|
|
44
|
+
* Every hook below takes an options object, and a caller writing `useEntitlement("pro", { basePath })` inline
|
|
45
|
+
* creates a new object every render. So each read is keyed on the one thing that can actually change the
|
|
46
|
+
* request — `basePath`, a string — and reads the rest through this.
|
|
45
47
|
*/
|
|
46
48
|
function useLatest(value) {
|
|
47
49
|
const ref = useRef(value);
|
|
48
50
|
ref.current = value;
|
|
49
51
|
return ref;
|
|
50
52
|
}
|
|
51
|
-
/**
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
53
|
+
/** A refusal carried as a thrown failure through `useRead` and `useAction`, so the last answer survives it. */
|
|
54
|
+
var Refused = class extends Error {
|
|
55
|
+
failure;
|
|
56
|
+
constructor(failure) {
|
|
57
|
+
super(failure.message);
|
|
58
|
+
this.failure = failure;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
/** The value of a result, or its refusal thrown as {@link Refused}. */
|
|
62
|
+
function unwrap(result) {
|
|
63
|
+
if (result.ok) return result.value;
|
|
64
|
+
throw new Refused(result.failure);
|
|
65
|
+
}
|
|
66
|
+
/** The refusal behind a failure. The calls here resolve rather than throw, so anything else is unreadable. */
|
|
67
|
+
function refusalOf(failure) {
|
|
68
|
+
if (failure === null) return null;
|
|
69
|
+
return failure instanceof Refused ? failure.failure : PAYMENTS_UNREADABLE;
|
|
61
70
|
}
|
|
62
71
|
/**
|
|
63
72
|
* Whether the caller holds `key`.
|
|
@@ -71,35 +80,18 @@ function useLive() {
|
|
|
71
80
|
* plan instead of locking it must not inherit this choice; see {@link useSubscription}.
|
|
72
81
|
*/
|
|
73
82
|
function useEntitlement(key, options) {
|
|
74
|
-
const [entitled, setEntitled] = useState(false);
|
|
75
|
-
const [loading, setLoading] = useState(true);
|
|
76
|
-
const [readFailure, setReadFailure] = useState(null);
|
|
77
83
|
const latest = useLatest(options);
|
|
78
|
-
const live = useLive();
|
|
79
84
|
const basePath = options?.basePath;
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}).then((result) => {
|
|
86
|
-
if (!live.current) return;
|
|
87
|
-
setEntitled(result.ok && result.value.some((entitlement) => entitlement.key === key && entitlement.granted));
|
|
88
|
-
setReadFailure(result.ok ? null : result.failure);
|
|
89
|
-
setLoading(false);
|
|
90
|
-
});
|
|
91
|
-
}, [
|
|
92
|
-
key,
|
|
93
|
-
basePath,
|
|
94
|
-
latest,
|
|
95
|
-
live
|
|
96
|
-
]);
|
|
97
|
-
useEffect(refresh, [refresh]);
|
|
85
|
+
const read = useRead(`entitlements:${basePath ?? ""}`, () => getEntitlements({
|
|
86
|
+
...latest.current,
|
|
87
|
+
basePath
|
|
88
|
+
}));
|
|
89
|
+
const result = read.value;
|
|
98
90
|
return {
|
|
99
|
-
entitled,
|
|
100
|
-
loading,
|
|
101
|
-
readFailure,
|
|
102
|
-
refresh
|
|
91
|
+
entitled: result?.ok === true && result.value.some((entitlement) => entitlement.key === key && entitlement.granted),
|
|
92
|
+
loading: read.loading,
|
|
93
|
+
readFailure: result?.ok === false ? result.failure : refusalOf(read.failure),
|
|
94
|
+
refresh: read.refresh
|
|
103
95
|
};
|
|
104
96
|
}
|
|
105
97
|
/**
|
|
@@ -110,36 +102,20 @@ function useEntitlement(key, options) {
|
|
|
110
102
|
* the kit's meters are what refuse.
|
|
111
103
|
*/
|
|
112
104
|
function useAllowances(options) {
|
|
113
|
-
const [allowances, setAllowances] = useState(null);
|
|
114
|
-
const [loading, setLoading] = useState(true);
|
|
115
|
-
const [readFailure, setReadFailure] = useState(null);
|
|
116
105
|
const latest = useLatest(options);
|
|
117
|
-
const live = useLive();
|
|
118
106
|
const basePath = options?.basePath;
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
basePath
|
|
124
|
-
}).then((result) => {
|
|
125
|
-
if (!live.current) return;
|
|
126
|
-
if (result.ok) setAllowances(result.value);
|
|
127
|
-
setReadFailure(result.ok ? null : result.failure);
|
|
128
|
-
setLoading(false);
|
|
129
|
-
});
|
|
130
|
-
}, [
|
|
131
|
-
basePath,
|
|
132
|
-
latest,
|
|
133
|
-
live
|
|
134
|
-
]);
|
|
135
|
-
useEffect(refresh, [refresh]);
|
|
107
|
+
const read = useRead(`allowances:${basePath ?? ""}`, async () => unwrap(await getAllowances({
|
|
108
|
+
...latest.current,
|
|
109
|
+
basePath
|
|
110
|
+
})));
|
|
136
111
|
return {
|
|
137
|
-
allowances,
|
|
138
|
-
loading,
|
|
139
|
-
readFailure,
|
|
140
|
-
refresh
|
|
112
|
+
allowances: read.value ?? null,
|
|
113
|
+
loading: read.loading,
|
|
114
|
+
readFailure: refusalOf(read.failure),
|
|
115
|
+
refresh: read.refresh
|
|
141
116
|
};
|
|
142
117
|
}
|
|
118
|
+
const NO_ENTITLEMENTS = [];
|
|
143
119
|
/**
|
|
144
120
|
* The caller's standing entitlements, and the one action a subscriber needs.
|
|
145
121
|
*
|
|
@@ -148,52 +124,34 @@ function useAllowances(options) {
|
|
|
148
124
|
* subscriptions are managed in their own apps, which a web screen can only link to.
|
|
149
125
|
*/
|
|
150
126
|
function useSubscription(options) {
|
|
151
|
-
const [entitlements, setEntitlements] = useState([]);
|
|
152
|
-
const [loading, setLoading] = useState(true);
|
|
153
|
-
const [managing, setManaging] = useState(false);
|
|
154
|
-
const [failure, setFailure] = useState(null);
|
|
155
|
-
const [readFailure, setReadFailure] = useState(null);
|
|
156
127
|
const latest = useLatest(options);
|
|
157
|
-
const live = useLive();
|
|
158
128
|
const basePath = options?.basePath;
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}).then((result) => {
|
|
165
|
-
if (!live.current) return;
|
|
166
|
-
if (result.ok) setEntitlements(result.value);
|
|
167
|
-
setReadFailure(result.ok ? null : result.failure);
|
|
168
|
-
setLoading(false);
|
|
169
|
-
});
|
|
170
|
-
}, [
|
|
171
|
-
basePath,
|
|
172
|
-
latest,
|
|
173
|
-
live
|
|
174
|
-
]);
|
|
175
|
-
useEffect(refresh, [refresh]);
|
|
176
|
-
const manage = useCallback(async () => {
|
|
177
|
-
setManaging(true);
|
|
178
|
-
setFailure(null);
|
|
129
|
+
const read = useRead(`subscription:${basePath ?? ""}`, async () => unwrap(await getEntitlements({
|
|
130
|
+
...latest.current,
|
|
131
|
+
basePath
|
|
132
|
+
})));
|
|
133
|
+
const portal = useAction(async () => {
|
|
179
134
|
const refused = await openBillingPortal(latest.current);
|
|
180
|
-
if (
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
135
|
+
if (refused) throw new Refused(refused);
|
|
136
|
+
});
|
|
137
|
+
const run = portal.run;
|
|
138
|
+
const manage = useCallback(async () => {
|
|
139
|
+
await run();
|
|
140
|
+
}, [run]);
|
|
184
141
|
const manageStore = useCallback((rail) => {
|
|
185
142
|
openStoreSubscriptions(rail, latest.current);
|
|
186
143
|
}, [latest]);
|
|
144
|
+
const entitlements = read.value ?? NO_ENTITLEMENTS;
|
|
187
145
|
return {
|
|
188
146
|
entitlements,
|
|
189
147
|
subscribed: entitlements.some((entitlement) => entitlement.granted),
|
|
190
|
-
loading,
|
|
191
|
-
refresh,
|
|
148
|
+
loading: read.loading,
|
|
149
|
+
refresh: read.refresh,
|
|
192
150
|
manage,
|
|
193
151
|
manageStore,
|
|
194
|
-
managing,
|
|
195
|
-
failure,
|
|
196
|
-
readFailure
|
|
152
|
+
managing: portal.pending,
|
|
153
|
+
failure: refusalOf(portal.failure),
|
|
154
|
+
readFailure: refusalOf(read.failure)
|
|
197
155
|
};
|
|
198
156
|
}
|
|
199
157
|
/**
|
|
@@ -209,82 +167,62 @@ function useSubscription(options) {
|
|
|
209
167
|
* {@link usePaddleCheckout}.
|
|
210
168
|
*/
|
|
211
169
|
function useCheckout(options) {
|
|
212
|
-
const [starting, setStarting] = useState(false);
|
|
213
|
-
const [failure, setFailure] = useState(null);
|
|
214
|
-
const [handoff, setHandoff] = useState(null);
|
|
215
170
|
const latest = useLatest(options);
|
|
216
|
-
const
|
|
171
|
+
const checkout = useAction(async (_signal, productId, choices) => {
|
|
172
|
+
const outcome = await startCheckout({
|
|
173
|
+
productId,
|
|
174
|
+
rail: choices?.rail,
|
|
175
|
+
discountCode: choices?.discountCode
|
|
176
|
+
}, latest.current);
|
|
177
|
+
if (outcome.kind === "refused") throw new Refused(outcome.failure);
|
|
178
|
+
return outcome.kind === "paddle" ? outcome.handoff : null;
|
|
179
|
+
});
|
|
180
|
+
const run = checkout.run;
|
|
217
181
|
return {
|
|
218
182
|
start: useCallback(async (productId, choices) => {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
rail: choices?.rail,
|
|
225
|
-
discountCode: choices?.discountCode
|
|
226
|
-
}, latest.current);
|
|
227
|
-
if (!live.current) return;
|
|
228
|
-
setStarting(false);
|
|
229
|
-
if (outcome.kind === "refused") setFailure(outcome.failure);
|
|
230
|
-
else if (outcome.kind === "paddle") setHandoff(outcome.handoff);
|
|
231
|
-
}, [latest, live]),
|
|
232
|
-
starting,
|
|
233
|
-
failure,
|
|
234
|
-
handoff
|
|
183
|
+
await run(productId, choices);
|
|
184
|
+
}, [run]),
|
|
185
|
+
starting: checkout.pending,
|
|
186
|
+
failure: refusalOf(checkout.failure),
|
|
187
|
+
handoff: checkout.pending || checkout.failure !== null ? null : checkout.value ?? null
|
|
235
188
|
};
|
|
236
189
|
}
|
|
237
190
|
/**
|
|
238
191
|
* Open the handoff {@link useCheckout} produced, with Paddle.js, over this page or inside it.
|
|
239
192
|
*
|
|
240
|
-
* **
|
|
241
|
-
* Inline checkout renders into an element the screen provides, and Paddle finds it by class name at
|
|
242
|
-
* moment `open` is called. Called from the handler that starts the checkout, the container React is about
|
|
193
|
+
* **After the render that revealed the container, which is why this is a hook rather than a line in a click
|
|
194
|
+
* handler.** Inline checkout renders into an element the screen provides, and Paddle finds it by class name at
|
|
195
|
+
* the moment `open` is called. Called from the handler that starts the checkout, the container React is about
|
|
243
196
|
* to render does not exist yet — and what Paddle does then is throw `TypeError: Cannot read properties of
|
|
244
|
-
* undefined (reading 'appendChild')` out of the click, which is not a sentence anyone can act on.
|
|
245
|
-
*
|
|
197
|
+
* undefined (reading 'appendChild')` out of the click, which is not a sentence anyone can act on. A read runs
|
|
198
|
+
* after the commit, so the element is there.
|
|
246
199
|
*
|
|
247
|
-
* One open per handoff
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
* cleans it up and runs it again, and that is the mode `pithy ui add` scaffolds: `client.tsx` wraps the
|
|
251
|
-
* router in it. So the transaction that has been opened is remembered, rather than the effect trusting
|
|
252
|
-
* that it runs once. The remembered id is the last one opened, not a flag — a buyer who closed the
|
|
253
|
-
* overlay and clicked Buy again arrives with a new transaction, and that one must open.
|
|
200
|
+
* One open per handoff, keyed on its transaction id and run once per key, so a re-render, a second mount under
|
|
201
|
+
* `StrictMode` (the mode `pithy ui add` scaffolds) and an inline handoff object all open one checkout. A buyer who
|
|
202
|
+
* closed the overlay and clicked Buy again arrives with a new transaction, and that one opens.
|
|
254
203
|
*/
|
|
255
204
|
function usePaddleCheckout(handoff, options) {
|
|
256
|
-
const [opening, setOpening] = useState(false);
|
|
257
|
-
const [failure, setFailure] = useState(null);
|
|
258
205
|
const latest = useLatest(options);
|
|
259
206
|
const latestHandoff = useLatest(handoff);
|
|
260
|
-
const live = useLive();
|
|
261
207
|
const transactionId = handoff?.transactionId;
|
|
262
|
-
|
|
263
|
-
const opened = useRef(null);
|
|
264
|
-
useEffect(() => {
|
|
208
|
+
const opened = useRead(`paddle-open:${transactionId ?? ""}`, async () => {
|
|
265
209
|
const current = latestHandoff.current;
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
openPaddleCheckout(current, latest.current).then((refused) => {
|
|
272
|
-
if (!live.current) return;
|
|
273
|
-
setOpening(false);
|
|
274
|
-
setFailure(refused);
|
|
275
|
-
});
|
|
276
|
-
}, [
|
|
277
|
-
transactionId,
|
|
278
|
-
latest,
|
|
279
|
-
latestHandoff,
|
|
280
|
-
live
|
|
281
|
-
]);
|
|
210
|
+
return current === null ? null : openPaddleCheckout(current, latest.current);
|
|
211
|
+
}, {
|
|
212
|
+
skip: transactionId === void 0,
|
|
213
|
+
once: true
|
|
214
|
+
});
|
|
282
215
|
return {
|
|
283
216
|
inline: handoff?.displayMode === "inline",
|
|
284
|
-
opening,
|
|
285
|
-
failure
|
|
217
|
+
opening: opened.loading,
|
|
218
|
+
failure: transactionId === void 0 || opened.loading ? null : opened.value ?? refusalOf(opened.failure)
|
|
286
219
|
};
|
|
287
220
|
}
|
|
221
|
+
/** The transaction a Paddle payment link put in this page's URL, or null — and null with no browser at all. */
|
|
222
|
+
function arrivedTransaction() {
|
|
223
|
+
const search = globalThis.location?.search;
|
|
224
|
+
return search === void 0 ? null : readPaddleLinkTransaction(search);
|
|
225
|
+
}
|
|
288
226
|
/**
|
|
289
227
|
* Resume the checkout a Paddle payment link arrived for.
|
|
290
228
|
*
|
|
@@ -295,49 +233,28 @@ function usePaddleCheckout(handoff, options) {
|
|
|
295
233
|
* one already exists.
|
|
296
234
|
*
|
|
297
235
|
* **Pass the result to {@link usePaddleCheckout}, which opens it.** Nothing about opening is reimplemented
|
|
298
|
-
* here, and that is the point: the one-open-per-transaction
|
|
299
|
-
*
|
|
300
|
-
* same hook the minted path goes through.
|
|
236
|
+
* here, and that is the point: the one-open-per-transaction rule and the `PADDLE_NO_CONTAINER` refusal when
|
|
237
|
+
* an inline container is missing are inherited by going through the same hook the minted path goes through.
|
|
301
238
|
*
|
|
302
239
|
* **In inline mode the page must already render the container.** Paddle finds it by class name when `open`
|
|
303
240
|
* is called, so a Default payment link pointing at a page that does not render one produces a refusal
|
|
304
241
|
* rather than a checkout — which makes the choice of landing page part of configuring this, not a detail.
|
|
305
242
|
* `docs/paddle.md` says so where an adopter sets the link.
|
|
306
243
|
*
|
|
307
|
-
* Reads `location.search` once, on mount
|
|
308
|
-
*
|
|
309
|
-
* **One read per transaction, remembered rather than assumed.** The same rule {@link usePaddleCheckout}
|
|
310
|
-
* states and for the same reason: `StrictMode` runs every effect, cleans it up and runs it again, and that
|
|
311
|
-
* is the mode `pithy ui add` scaffolds. Without the ref this asked the server twice on every mount — not
|
|
312
|
-
* harmful, since the route only echoes config, and still two requests where the file beside it had already
|
|
313
|
-
* solved the problem.
|
|
244
|
+
* Reads `location.search` once, on mount, and asks the server once per transaction, `StrictMode` included. A
|
|
245
|
+
* link is a navigation, so there is no later arrival to watch for.
|
|
314
246
|
*/
|
|
315
247
|
function usePaddleLink(options) {
|
|
316
|
-
const [handoff, setHandoff] = useState(null);
|
|
317
|
-
const [loading, setLoading] = useState(false);
|
|
318
|
-
const [failure, setFailure] = useState(null);
|
|
319
248
|
const latest = useLatest(options);
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
const transactionId = search === void 0 ? null : readPaddleLinkTransaction(search);
|
|
326
|
-
if (transactionId === null) return;
|
|
327
|
-
if (asked.current === transactionId) return;
|
|
328
|
-
asked.current = transactionId;
|
|
329
|
-
setLoading(true);
|
|
330
|
-
resumeCheckout(transactionId, latest.current).then((outcome) => {
|
|
331
|
-
if (!live.current) return;
|
|
332
|
-
setLoading(false);
|
|
333
|
-
if (outcome.ok) setHandoff(outcome.value);
|
|
334
|
-
else setFailure(outcome.failure);
|
|
335
|
-
});
|
|
336
|
-
}, [latest, live]);
|
|
249
|
+
const [transactionId] = useState(arrivedTransaction);
|
|
250
|
+
const resumed = useRead(`paddle-link:${transactionId ?? ""}`, async () => transactionId === null ? null : unwrap(await resumeCheckout(transactionId, latest.current)), {
|
|
251
|
+
skip: transactionId === null,
|
|
252
|
+
once: true
|
|
253
|
+
});
|
|
337
254
|
return {
|
|
338
|
-
handoff,
|
|
339
|
-
loading,
|
|
340
|
-
failure
|
|
255
|
+
handoff: resumed.value ?? null,
|
|
256
|
+
loading: resumed.loading,
|
|
257
|
+
failure: refusalOf(resumed.failure)
|
|
341
258
|
};
|
|
342
259
|
}
|
|
343
260
|
/**
|
|
@@ -353,49 +270,43 @@ function usePaddleLink(options) {
|
|
|
353
270
|
* paywall back over a feature the user already owns, on nothing more than a failed second submission.
|
|
354
271
|
*/
|
|
355
272
|
function usePurchase(options) {
|
|
356
|
-
const [purchase, setPurchase] = useState(null);
|
|
357
|
-
const [entitlements, setEntitlements] = useState([]);
|
|
358
|
-
const [busy, setBusy] = useState(false);
|
|
359
|
-
const [failure, setFailure] = useState(null);
|
|
360
273
|
const latest = useLatest(options);
|
|
361
|
-
const
|
|
274
|
+
const write = useAction(async (_signal, call) => {
|
|
275
|
+
if (call.kind === "submit") {
|
|
276
|
+
const written = unwrap(await submitPurchase({
|
|
277
|
+
rail: call.rail,
|
|
278
|
+
receipt: call.receipt
|
|
279
|
+
}, latest.current));
|
|
280
|
+
return {
|
|
281
|
+
purchase: written.purchase,
|
|
282
|
+
entitlements: written.entitlements
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
const restored = unwrap(await restorePurchases({
|
|
286
|
+
rail: call.rail,
|
|
287
|
+
receipts: call.receipts
|
|
288
|
+
}, latest.current));
|
|
289
|
+
return {
|
|
290
|
+
purchase: restored.purchases[0] ?? null,
|
|
291
|
+
entitlements: restored.entitlements
|
|
292
|
+
};
|
|
293
|
+
});
|
|
294
|
+
const run = write.run;
|
|
362
295
|
return {
|
|
363
|
-
submit: useCallback(async (rail, receipt) => {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
setEntitlements(result.value.entitlements);
|
|
378
|
-
}, [latest, live]),
|
|
379
|
-
restore: useCallback(async (rail, receipts) => {
|
|
380
|
-
setBusy(true);
|
|
381
|
-
setFailure(null);
|
|
382
|
-
const result = await restorePurchases({
|
|
383
|
-
rail,
|
|
384
|
-
receipts
|
|
385
|
-
}, latest.current);
|
|
386
|
-
if (!live.current) return;
|
|
387
|
-
setBusy(false);
|
|
388
|
-
if (!result.ok) {
|
|
389
|
-
setFailure(result.failure);
|
|
390
|
-
return;
|
|
391
|
-
}
|
|
392
|
-
setPurchase(result.value.purchases[0] ?? null);
|
|
393
|
-
setEntitlements(result.value.entitlements);
|
|
394
|
-
}, [latest, live]),
|
|
395
|
-
purchase,
|
|
396
|
-
entitlements,
|
|
397
|
-
busy,
|
|
398
|
-
failure
|
|
296
|
+
submit: useCallback(async (rail, receipt) => await run({
|
|
297
|
+
kind: "submit",
|
|
298
|
+
rail,
|
|
299
|
+
receipt
|
|
300
|
+
}) !== void 0, [run]),
|
|
301
|
+
restore: useCallback(async (rail, receipts) => await run({
|
|
302
|
+
kind: "restore",
|
|
303
|
+
rail,
|
|
304
|
+
receipts
|
|
305
|
+
}) !== void 0, [run]),
|
|
306
|
+
purchase: write.value?.purchase ?? null,
|
|
307
|
+
entitlements: write.value?.entitlements ?? NO_ENTITLEMENTS,
|
|
308
|
+
busy: write.pending,
|
|
309
|
+
failure: refusalOf(write.failure)
|
|
399
310
|
};
|
|
400
311
|
}
|
|
401
312
|
/**
|
|
@@ -411,38 +322,18 @@ function usePurchase(options) {
|
|
|
411
322
|
* React-shaped view of it: a state a screen can render.
|
|
412
323
|
*/
|
|
413
324
|
function usePaddle(setup, options) {
|
|
414
|
-
const [paddle, setPaddle] = useState(null);
|
|
415
|
-
const [loading, setLoading] = useState(setup !== null);
|
|
416
|
-
const [failure, setFailure] = useState(null);
|
|
417
325
|
const latest = useLatest(options);
|
|
418
|
-
const live = useLive();
|
|
419
326
|
const clientToken = setup?.clientToken;
|
|
420
327
|
const environment = setup?.environment;
|
|
421
|
-
|
|
422
|
-
if (clientToken === void 0 || environment === void 0) {
|
|
423
|
-
setLoading(false);
|
|
424
|
-
return;
|
|
425
|
-
}
|
|
426
|
-
setLoading(true);
|
|
427
|
-
loadPaddle({
|
|
428
|
-
clientToken,
|
|
429
|
-
environment
|
|
430
|
-
}, latest.current).then((result) => {
|
|
431
|
-
if (!live.current) return;
|
|
432
|
-
setPaddle(result.ok ? result.value : null);
|
|
433
|
-
setFailure(result.ok ? null : result.failure);
|
|
434
|
-
setLoading(false);
|
|
435
|
-
});
|
|
436
|
-
}, [
|
|
328
|
+
const loaded = useRead(`paddle:${environment ?? ""}:${clientToken ?? ""}`, () => clientToken === void 0 || environment === void 0 ? Promise.resolve(null) : loadPaddle({
|
|
437
329
|
clientToken,
|
|
438
|
-
environment
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
]);
|
|
330
|
+
environment
|
|
331
|
+
}, latest.current), { skip: clientToken === void 0 || environment === void 0 });
|
|
332
|
+
const result = loaded.value;
|
|
442
333
|
return {
|
|
443
|
-
paddle,
|
|
444
|
-
loading,
|
|
445
|
-
failure
|
|
334
|
+
paddle: result?.ok === true ? result.value : null,
|
|
335
|
+
loading: loaded.loading,
|
|
336
|
+
failure: result?.ok === false ? result.failure : refusalOf(loaded.failure)
|
|
446
337
|
};
|
|
447
338
|
}
|
|
448
339
|
/**
|
|
@@ -453,71 +344,31 @@ function usePaddle(setup, options) {
|
|
|
453
344
|
* $5.44 in New York, $5.75 in Chicago, $5.00 in Berlin including €0.80 of VAT, and ¥725 in Tokyo without
|
|
454
345
|
* a table of tax rates aging in somebody's repository.
|
|
455
346
|
*
|
|
456
|
-
* `query` may be written inline. The
|
|
457
|
-
*
|
|
347
|
+
* `query` may be written inline. The read is keyed on {@link priceQueryKey} rather than on the object, so an
|
|
348
|
+
* object literal re-created on every render re-quotes when the request changed and not before.
|
|
458
349
|
*
|
|
459
|
-
* A failure clears the previous quote rather than leaving it
|
|
460
|
-
* value on a refusal, and this one deliberately does not: a re-quote happens because the *request*
|
|
350
|
+
* A failure clears the previous quote rather than leaving it, because a re-quote happens when the *request*
|
|
461
351
|
* changed, so the value it would be keeping is a price for something else.
|
|
462
352
|
*
|
|
463
|
-
* **Only the latest quote is rendered, whichever answers first.**
|
|
464
|
-
* an anonymous visitor's location
|
|
465
|
-
*
|
|
466
|
-
* screen whose entire job is showing a correct one. Superseded answers are *ignored*, not canceled:
|
|
467
|
-
* Paddle.js's `PricePreview` takes no `AbortSignal` and hands back a bare promise, so there is nothing to
|
|
468
|
-
* cancel and ignoring it is the whole of the fix. A superseded refusal is ignored the same way, which is
|
|
469
|
-
* what stops a dead request blanking a price the visitor is already reading.
|
|
353
|
+
* **Only the latest quote is rendered, whichever answers first.** A slower quote for an address the visitor
|
|
354
|
+
* has left — an anonymous visitor's location resolving under the query, or a country picker moving — is
|
|
355
|
+
* discarded rather than rendered, and so is a superseded refusal.
|
|
470
356
|
*/
|
|
471
357
|
function usePricePreview(setup, query, options) {
|
|
472
|
-
const [preview, setPreview] = useState(null);
|
|
473
|
-
const [loading, setLoading] = useState(setup !== null);
|
|
474
|
-
const [failure, setFailure] = useState(null);
|
|
475
358
|
const latest = useLatest(options);
|
|
476
359
|
const latestQuery = useLatest(query);
|
|
477
|
-
const live = useLive();
|
|
478
360
|
const clientToken = setup?.clientToken;
|
|
479
361
|
const environment = setup?.environment;
|
|
480
|
-
const
|
|
481
|
-
/**
|
|
482
|
-
* Which quote is the current one.
|
|
483
|
-
*
|
|
484
|
-
* A counter rather than a comparison against `queryKey`, because two quotes for the *same* query race
|
|
485
|
-
* too — a screen calling `refresh` twice is the ordinary way to get there — and a key would read those
|
|
486
|
-
* two as one and let either win. Every ask takes the next number; only the holder of the last one
|
|
487
|
-
* issued may write state.
|
|
488
|
-
*/
|
|
489
|
-
const issued = useRef(0);
|
|
490
|
-
const refresh = useCallback(() => {
|
|
491
|
-
if (clientToken === void 0 || environment === void 0) {
|
|
492
|
-
setLoading(false);
|
|
493
|
-
return;
|
|
494
|
-
}
|
|
495
|
-
const ticket = ++issued.current;
|
|
496
|
-
setLoading(true);
|
|
497
|
-
previewPrices({
|
|
498
|
-
clientToken,
|
|
499
|
-
environment
|
|
500
|
-
}, latestQuery.current, latest.current).then((result) => {
|
|
501
|
-
if (!live.current) return;
|
|
502
|
-
if (ticket !== issued.current) return;
|
|
503
|
-
setPreview(result.ok ? result.value : null);
|
|
504
|
-
setFailure(result.ok ? null : result.failure);
|
|
505
|
-
setLoading(false);
|
|
506
|
-
});
|
|
507
|
-
}, [
|
|
362
|
+
const quoted = useRead(`price:${environment ?? ""}:${clientToken ?? ""}:${priceQueryKey(query)}`, () => clientToken === void 0 || environment === void 0 ? Promise.resolve(null) : previewPrices({
|
|
508
363
|
clientToken,
|
|
509
|
-
environment
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
latestQuery,
|
|
513
|
-
live
|
|
514
|
-
]);
|
|
515
|
-
useEffect(refresh, [refresh]);
|
|
364
|
+
environment
|
|
365
|
+
}, latestQuery.current, latest.current), { skip: clientToken === void 0 || environment === void 0 });
|
|
366
|
+
const result = quoted.value;
|
|
516
367
|
return {
|
|
517
|
-
preview,
|
|
518
|
-
loading,
|
|
519
|
-
failure,
|
|
520
|
-
refresh
|
|
368
|
+
preview: result?.ok === true ? result.value : null,
|
|
369
|
+
loading: quoted.loading,
|
|
370
|
+
failure: result?.ok === false ? result.failure : refusalOf(quoted.failure),
|
|
371
|
+
refresh: quoted.refresh
|
|
521
372
|
};
|
|
522
373
|
}
|
|
523
374
|
//#endregion
|