ferst-core 0.10.2 → 0.11.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/bin/ferst-modules.mjs +18 -2
- package/components/BlockRenderer.astro +2 -0
- package/components/CookieBanner.astro +3 -1
- package/components/recipes/ContactForm.astro +23 -1
- package/components/recipes/Pay.astro +150 -0
- package/content/blocks.ts +23 -2
- package/content/recipe-questions.ts +1 -0
- package/lib/pay.ts +62 -0
- package/lib/preview-frame.ts +19 -0
- package/package.json +1 -1
package/bin/ferst-modules.mjs
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// ferst-modules — what this site has from its organisation's modules in the Ferst portal:
|
|
3
|
-
// the forms it can place and the calendar it shows. Run it in a client repo root:
|
|
3
|
+
// the forms it can place, what it can take payment for, and the calendar it shows. Run it in a client repo root:
|
|
4
4
|
//
|
|
5
5
|
// npx --no-install ferst-modules # the site named in ferst-site.json
|
|
6
6
|
// npx --no-install ferst-modules --site <address> # or ask by the site's address
|
|
7
7
|
// npx --no-install ferst-modules --json # the raw answer
|
|
8
8
|
//
|
|
9
9
|
// Forms and calendars live in the portal, never in the repo: place a form with a
|
|
10
|
-
// `{ "type": "form", "form": "<its name on this site>" }` block,
|
|
10
|
+
// `{ "type": "form", "form": "<its name on this site>" }` block, take a payment with a
|
|
11
|
+
// `{ "type": "pay", "item": "<its id>" }` block, and show the calendar by setting
|
|
11
12
|
// calendarSettings.embedUrl to the address printed here.
|
|
12
13
|
import { readFileSync, realpathSync } from 'node:fs';
|
|
13
14
|
import { resolve } from 'node:path';
|
|
14
15
|
import { pathToFileURL } from 'node:url';
|
|
15
16
|
import { resolveGitBase } from '../lib/git-base.mjs';
|
|
16
17
|
|
|
18
|
+
/** £40, £40.50 (lib/pay formatPrice, for a plain .mjs bin). */
|
|
19
|
+
const price = (amount, currency = 'gbp') => {
|
|
20
|
+
const units = amount / 100;
|
|
21
|
+
const text = Number.isInteger(units) ? String(units) : units.toFixed(2);
|
|
22
|
+
return currency.toLowerCase() === 'gbp' ? `£${text}` : `${text} ${currency.toUpperCase()}`;
|
|
23
|
+
};
|
|
24
|
+
|
|
17
25
|
/**
|
|
18
26
|
* True when THIS file is run as a CLI (not imported). Each bin needs its own copy:
|
|
19
27
|
* `import.meta.url` is per file, so one borrowed from another bin only ever matches that
|
|
@@ -52,6 +60,14 @@ export function formatModules(data) {
|
|
|
52
60
|
}
|
|
53
61
|
out.push('');
|
|
54
62
|
|
|
63
|
+
out.push('Payments');
|
|
64
|
+
if (!data.modules.payments) out.push(' The Payments module is off for this site, or has nothing to take payment for yet (portal: Payments).');
|
|
65
|
+
for (const p of data.pay ?? []) {
|
|
66
|
+
out.push(` ${p.title}: ${price(p.amount, p.currency)}`);
|
|
67
|
+
out.push(` place it with: { "type": "pay", "item": "${p.id}" }`);
|
|
68
|
+
}
|
|
69
|
+
out.push('');
|
|
70
|
+
|
|
55
71
|
out.push('Events');
|
|
56
72
|
if (!data.events) out.push(' The Events module is off for this site.');
|
|
57
73
|
else {
|
|
@@ -27,6 +27,7 @@ import Gallery from './recipes/Gallery.astro';
|
|
|
27
27
|
import Testimonial from './recipes/Testimonial.astro';
|
|
28
28
|
import ContactForm from './recipes/ContactForm.astro';
|
|
29
29
|
import Form from './recipes/Form.astro';
|
|
30
|
+
import Pay from './recipes/Pay.astro';
|
|
30
31
|
import Accordion from './recipes/Accordion.astro';
|
|
31
32
|
import Tiles from './recipes/Tiles.astro';
|
|
32
33
|
import Banner from './recipes/Banner.astro';
|
|
@@ -81,6 +82,7 @@ const registry: Record<BlockType, any> = {
|
|
|
81
82
|
testimonial: Testimonial,
|
|
82
83
|
contactForm: ContactForm,
|
|
83
84
|
form: Form,
|
|
85
|
+
pay: Pay,
|
|
84
86
|
accordion: Accordion,
|
|
85
87
|
tiles: Tiles,
|
|
86
88
|
banner: Banner,
|
|
@@ -29,9 +29,11 @@ const { policyHref = '/cookie-policy/' } = Astro.props;
|
|
|
29
29
|
|
|
30
30
|
<script>
|
|
31
31
|
import { isDecided, setConsent } from '../lib/consent-client';
|
|
32
|
+
import { inPreviewFrame } from '../lib/preview-frame';
|
|
32
33
|
|
|
33
34
|
const banner = document.getElementById('cookie-banner');
|
|
34
|
-
|
|
35
|
+
// A preview in the portal isn't a visit: the banner stays out of its way (lib/preview-frame).
|
|
36
|
+
if (banner && !isDecided() && !inPreviewFrame()) {
|
|
35
37
|
banner.hidden = false;
|
|
36
38
|
// Next frame so the entry transition runs.
|
|
37
39
|
requestAnimationFrame(() => banner.classList.add('visible'));
|
|
@@ -28,6 +28,12 @@ interface Props {
|
|
|
28
28
|
submitLabel?: string;
|
|
29
29
|
note?: string;
|
|
30
30
|
fields?: Field[];
|
|
31
|
+
/** Sent with the form, unseen (e.g. the page to come back to). */
|
|
32
|
+
hidden?: Record<string, string>;
|
|
33
|
+
/** Add a field only bots fill (`_gotcha`): the platform refuses a form that has it filled. */
|
|
34
|
+
honeypot?: boolean;
|
|
35
|
+
/** Prefixes the fields' ids, so two forms on one page don't share them. */
|
|
36
|
+
idPrefix?: string;
|
|
31
37
|
}
|
|
32
38
|
const {
|
|
33
39
|
title,
|
|
@@ -37,6 +43,9 @@ const {
|
|
|
37
43
|
submitLabel = 'Send message',
|
|
38
44
|
note,
|
|
39
45
|
fields = [],
|
|
46
|
+
hidden = {},
|
|
47
|
+
honeypot = false,
|
|
48
|
+
idPrefix = 'cf',
|
|
40
49
|
} = Astro.props;
|
|
41
50
|
// Alignment overrides (BlockLayout): the title + intro default to centred; the block's
|
|
42
51
|
// own `align`, or `headingAlign` for the title alone, overrides it from the page data.
|
|
@@ -52,8 +61,14 @@ const headAlign = headingAlign ?? blockAlign ?? 'center';
|
|
|
52
61
|
</div>
|
|
53
62
|
)}
|
|
54
63
|
<form class="b-contact-form__form" action={action} method={method}>
|
|
64
|
+
{Object.entries(hidden).map(([name, value]) => <input type="hidden" name={name} value={value} />)}
|
|
65
|
+
{honeypot && (
|
|
66
|
+
<div class="b-contact-form__trap" aria-hidden="true">
|
|
67
|
+
<label>Leave this empty <input type="text" name="_gotcha" tabindex="-1" autocomplete="off" /></label>
|
|
68
|
+
</div>
|
|
69
|
+
)}
|
|
55
70
|
{fields.map((field) => {
|
|
56
|
-
const id =
|
|
71
|
+
const id = `${idPrefix}-${field.name}`;
|
|
57
72
|
const type = field.type ?? 'text';
|
|
58
73
|
return (
|
|
59
74
|
<div class="b-contact-form__field">
|
|
@@ -164,6 +179,13 @@ const headAlign = headingAlign ?? blockAlign ?? 'center';
|
|
|
164
179
|
border-color: var(--gold);
|
|
165
180
|
box-shadow: 0 0 0 3px var(--gold-light);
|
|
166
181
|
}
|
|
182
|
+
.b-contact-form__trap {
|
|
183
|
+
position: absolute;
|
|
184
|
+
left: -10000px;
|
|
185
|
+
width: 1px;
|
|
186
|
+
height: 1px;
|
|
187
|
+
overflow: hidden;
|
|
188
|
+
}
|
|
167
189
|
.b-contact-form__action {
|
|
168
190
|
margin-top: 0.25rem;
|
|
169
191
|
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Pay — a payment for one of the organisation's items from the Ferst Payments module ("Course fee",
|
|
4
|
+
* £40). The item's name and price are read from the platform at build time (lib/pay) and again in
|
|
5
|
+
* the browser, so a new price or a sale shows without a rebuild; the page never says what to
|
|
6
|
+
* charge. The visitor gives a name and email; the form posts to the platform, which sends them to
|
|
7
|
+
* Stripe's page on the organisation's own account, and Stripe brings them back here with
|
|
8
|
+
* `?payment=done` or `?payment=cancelled` (and this block's anchor), where the block says so.
|
|
9
|
+
* Works without JavaScript: a plain form post, answered with a redirect to Stripe.
|
|
10
|
+
*/
|
|
11
|
+
import ContactForm from './ContactForm.astro';
|
|
12
|
+
import Heading from '../blocks/Heading.astro';
|
|
13
|
+
import { formatPrice, loadPayItem } from '../../lib/pay';
|
|
14
|
+
import { resolveGitBase, resolveSiteId } from '../../lib/git-base.mjs';
|
|
15
|
+
|
|
16
|
+
interface Props {
|
|
17
|
+
item: string;
|
|
18
|
+
title?: string;
|
|
19
|
+
intro?: string;
|
|
20
|
+
note?: string;
|
|
21
|
+
buttonLabel?: string;
|
|
22
|
+
}
|
|
23
|
+
const { item, title, intro, note, buttonLabel = 'Pay' } = Astro.props;
|
|
24
|
+
const def = await loadPayItem(item, { base: resolveGitBase(), siteId: resolveSiteId() });
|
|
25
|
+
const anchor = `pay-${def.id}`;
|
|
26
|
+
const price = formatPrice(def.amount, def.currency);
|
|
27
|
+
const { align: blockAlign, headingAlign } = Astro.props as { align?: 'start' | 'center' | 'end'; headingAlign?: 'start' | 'center' | 'end' };
|
|
28
|
+
const headAlign = headingAlign ?? blockAlign ?? 'center';
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
<section class="b-pay" id={anchor} data-pay={def.definition} data-label={buttonLabel} data-amount={def.amount}>
|
|
32
|
+
<div class="b-pay__head" data-recipe-head data-content-align={headAlign}>
|
|
33
|
+
<Heading text={title ?? def.title} level={2} />
|
|
34
|
+
<p class="b-pay__price" data-pay-price>{price}</p>
|
|
35
|
+
{intro && <p class="b-pay__intro">{intro}</p>}
|
|
36
|
+
</div>
|
|
37
|
+
<div class="b-pay__notice b-pay__notice--done" role="status" data-pay-done hidden>
|
|
38
|
+
<strong>Thank you: your payment has gone through.</strong> A receipt is on its way to your email.
|
|
39
|
+
</div>
|
|
40
|
+
<div class="b-pay__notice" role="status" data-pay-cancelled hidden>
|
|
41
|
+
Your payment wasn’t taken. You can try again whenever you’re ready.
|
|
42
|
+
</div>
|
|
43
|
+
<div class="b-pay__notice" role="status" data-pay-unavailable hidden>
|
|
44
|
+
This payment isn’t available just now.
|
|
45
|
+
</div>
|
|
46
|
+
<ContactForm
|
|
47
|
+
action={def.action}
|
|
48
|
+
method="post"
|
|
49
|
+
submitLabel={`${buttonLabel} ${price}`}
|
|
50
|
+
note={note ?? 'You’ll pay on Stripe’s secure page, and get a receipt by email.'}
|
|
51
|
+
fields={[
|
|
52
|
+
{ name: 'name', label: 'Your name', type: 'text', required: true },
|
|
53
|
+
{ name: 'email', label: 'Your email', type: 'email', required: true },
|
|
54
|
+
]}
|
|
55
|
+
hidden={{ return_to: `${Astro.url.pathname}#${anchor}` }}
|
|
56
|
+
honeypot
|
|
57
|
+
idPrefix={anchor}
|
|
58
|
+
/>
|
|
59
|
+
</section>
|
|
60
|
+
|
|
61
|
+
<script>
|
|
62
|
+
const price = (amount: number, currency: string) => {
|
|
63
|
+
const units = amount / 100;
|
|
64
|
+
const text = Number.isInteger(units) ? String(units) : units.toFixed(2);
|
|
65
|
+
return currency.toLowerCase() === 'gbp' ? `£${text}` : `${text} ${currency.toUpperCase()}`;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
function setUp(block: HTMLElement, onlyOne: boolean) {
|
|
69
|
+
// Back from Stripe: say how it went, on the block that was paid (its anchor), then tidy the address.
|
|
70
|
+
const outcome = new URLSearchParams(location.search).get('payment');
|
|
71
|
+
if ((outcome === 'done' || outcome === 'cancelled') && (location.hash === `#${block.id}` || (onlyOne && !location.hash))) {
|
|
72
|
+
const notice = block.querySelector<HTMLElement>(outcome === 'done' ? '[data-pay-done]' : '[data-pay-cancelled]');
|
|
73
|
+
if (notice) notice.hidden = false;
|
|
74
|
+
const url = new URL(location.href);
|
|
75
|
+
url.searchParams.delete('payment');
|
|
76
|
+
history.replaceState(history.state, '', url);
|
|
77
|
+
}
|
|
78
|
+
// The price now (a new price or a sale since the build); gone from offer, the form is closed.
|
|
79
|
+
const src = block.dataset.pay;
|
|
80
|
+
if (!src) return;
|
|
81
|
+
fetch(src, { headers: { Accept: 'application/json' } })
|
|
82
|
+
.then(async (res) => {
|
|
83
|
+
const form = block.querySelector<HTMLFormElement>('form');
|
|
84
|
+
if (res.status === 404) {
|
|
85
|
+
block.querySelector<HTMLElement>('[data-pay-unavailable]')!.hidden = false;
|
|
86
|
+
form?.querySelectorAll<HTMLInputElement | HTMLButtonElement>('input, button').forEach((el) => { el.disabled = true; });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (!res.ok) return;
|
|
90
|
+
const d = (await res.json()) as { amount: number; currency?: string };
|
|
91
|
+
if (String(d.amount) === block.dataset.amount) return;
|
|
92
|
+
const text = price(d.amount, d.currency ?? 'gbp');
|
|
93
|
+
const shown = block.querySelector<HTMLElement>('[data-pay-price]');
|
|
94
|
+
if (shown) shown.textContent = text;
|
|
95
|
+
const button = form?.querySelector<HTMLButtonElement>('button[type="submit"]');
|
|
96
|
+
if (button) button.textContent = `${block.dataset.label ?? 'Pay'} ${text}`;
|
|
97
|
+
})
|
|
98
|
+
.catch(() => { /* the price from the build stands; the platform reads the real one when they pay */ });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const blocks = Array.from(document.querySelectorAll<HTMLElement>('[data-pay]'));
|
|
102
|
+
blocks.forEach((b) => setUp(b, blocks.length === 1));
|
|
103
|
+
</script>
|
|
104
|
+
|
|
105
|
+
<style>
|
|
106
|
+
.b-pay {
|
|
107
|
+
display: flex;
|
|
108
|
+
flex-direction: column;
|
|
109
|
+
gap: 1.25rem;
|
|
110
|
+
max-width: 40rem;
|
|
111
|
+
margin-inline: auto;
|
|
112
|
+
padding-inline: 1rem;
|
|
113
|
+
}
|
|
114
|
+
.b-pay :global(.b-contact-form) {
|
|
115
|
+
padding-inline: 0;
|
|
116
|
+
}
|
|
117
|
+
.b-pay__head {
|
|
118
|
+
display: flex;
|
|
119
|
+
flex-direction: column;
|
|
120
|
+
gap: 0.5rem;
|
|
121
|
+
}
|
|
122
|
+
.b-pay__price {
|
|
123
|
+
margin: 0;
|
|
124
|
+
font-family: var(--font-heading, var(--font-body));
|
|
125
|
+
font-size: 1.75rem;
|
|
126
|
+
font-weight: 700;
|
|
127
|
+
color: var(--fg);
|
|
128
|
+
font-variant-numeric: tabular-nums;
|
|
129
|
+
}
|
|
130
|
+
.b-pay__intro {
|
|
131
|
+
margin: 0;
|
|
132
|
+
color: var(--muted);
|
|
133
|
+
line-height: 1.6;
|
|
134
|
+
}
|
|
135
|
+
.b-pay__notice {
|
|
136
|
+
padding: 0.85rem 1rem;
|
|
137
|
+
border: 1px solid var(--border);
|
|
138
|
+
border-radius: var(--radius-sm);
|
|
139
|
+
background: var(--bg-section);
|
|
140
|
+
color: var(--fg);
|
|
141
|
+
line-height: 1.5;
|
|
142
|
+
}
|
|
143
|
+
.b-pay__notice--done {
|
|
144
|
+
border-color: var(--gold);
|
|
145
|
+
background: var(--gold-light);
|
|
146
|
+
}
|
|
147
|
+
.b-pay__notice[hidden] {
|
|
148
|
+
display: none;
|
|
149
|
+
}
|
|
150
|
+
</style>
|
package/content/blocks.ts
CHANGED
|
@@ -242,6 +242,25 @@ export const formBlock = z.object({
|
|
|
242
242
|
note: z.string().optional(),
|
|
243
243
|
});
|
|
244
244
|
|
|
245
|
+
/** A payment for one of the organisation's items, from the Ferst Payments module ("Course fee",
|
|
246
|
+
* £40). The block names the item; its name and price are the organisation's, read from the
|
|
247
|
+
* platform at build time (lib/pay) and again in the browser, never written here: the platform
|
|
248
|
+
* reads the price itself when the visitor pays. The visitor gives a name and email and pays on
|
|
249
|
+
* Stripe's page. List what a site can take payment for with `npx ferst-modules`. */
|
|
250
|
+
export const payBlock = z.object({
|
|
251
|
+
type: z.literal('pay'),
|
|
252
|
+
enabled: z.boolean().default(true),
|
|
253
|
+
/** The item's id in the portal (e.g. `pi_8c1d…`). */
|
|
254
|
+
item: z.string().min(1),
|
|
255
|
+
/** Defaults to the item's name. */
|
|
256
|
+
title: z.string().optional(),
|
|
257
|
+
intro: z.string().optional(),
|
|
258
|
+
/** Small print under the button (e.g. what happens after paying). */
|
|
259
|
+
note: z.string().optional(),
|
|
260
|
+
/** The button's words; the price follows them ("Pay £40"). */
|
|
261
|
+
buttonLabel: z.string().max(40).optional(),
|
|
262
|
+
});
|
|
263
|
+
|
|
245
264
|
/** A contact form recipe. `fields` is generic (any labelled input list) so the
|
|
246
265
|
* same recipe serves a contact page, an enquiry form, a sign-up, etc. `action`
|
|
247
266
|
* is optional: the core ships no backend, so with none it renders a standard
|
|
@@ -727,6 +746,7 @@ export type RecipeBlock =
|
|
|
727
746
|
| z.infer<typeof testimonialBlock>
|
|
728
747
|
| z.infer<typeof contactFormBlock>
|
|
729
748
|
| z.infer<typeof formBlock>
|
|
749
|
+
| z.infer<typeof payBlock>
|
|
730
750
|
| z.infer<typeof accordionBlock>
|
|
731
751
|
| z.infer<typeof tilesBlock>
|
|
732
752
|
| z.infer<typeof bannerBlock>
|
|
@@ -870,6 +890,7 @@ export const blockSchema = z.discriminatedUnion(
|
|
|
870
890
|
testimonialBlock,
|
|
871
891
|
contactFormBlock,
|
|
872
892
|
formBlock,
|
|
893
|
+
payBlock,
|
|
873
894
|
accordionBlock,
|
|
874
895
|
tilesBlock,
|
|
875
896
|
bannerBlock,
|
|
@@ -960,7 +981,7 @@ export const blockTypes = (
|
|
|
960
981
|
blockSchema as unknown as { options: ReadonlyArray<{ shape: { type: { value: BlockType } } }> }
|
|
961
982
|
).options.map((o) => o.shape.type.value);
|
|
962
983
|
|
|
963
|
-
/** Blocks whose content comes from the Ferst platform at build time (a form
|
|
984
|
+
/** Blocks whose content comes from the Ferst platform at build time (a form, an item to pay for).
|
|
964
985
|
* The public showcase can't render them without the platform, so its galleries show the
|
|
965
986
|
* look through their local twin instead (`form` renders with the contactForm recipe). */
|
|
966
|
-
export const platformBlockTypes: readonly BlockType[] = ['form'];
|
|
987
|
+
export const platformBlockTypes: readonly BlockType[] = ['form', 'pay'];
|
|
@@ -29,6 +29,7 @@ const QUESTIONS: Record<string, RecipeQuestion | null> = {
|
|
|
29
29
|
testimonial: ask('What people say', 'What have customers said about you?', 'Paste a kind word or review, with the person’s name or initials if they’re happy to be named.'),
|
|
30
30
|
contactForm: ask('Contact form', 'What should people be able to ask you through this form?', 'Who answers, what you need to know from them, and how soon they can expect a reply.'),
|
|
31
31
|
form: note('Form', 'This shows one of your organisation’s forms. Choose or create it in the portal under Forms.'),
|
|
32
|
+
pay: note('Payment', 'This takes a payment for one of the things your sites take payment for. Set what it’s for and its price in the portal, under Payments.'),
|
|
32
33
|
accordion: ask('Expandable sections', 'What information should people be able to open up and read here?', 'Give each topic a short heading and the detail that goes under it.'),
|
|
33
34
|
tiles: ask('Quick links', 'Where do visitors most often want to go from here?', 'List the places or tasks, a few words each, for example: opening times, book now, prices.'),
|
|
34
35
|
banner: ask('Banner', 'What message deserves a bold band across the page?', 'A short statement or invitation, and where it should lead if anywhere.'),
|
package/lib/pay.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A "Pay" block takes a payment for one of the organisation's items (the Ferst Payments module).
|
|
3
|
+
* The block names the item; its name and price are the organisation's, read from the platform at
|
|
4
|
+
* build time (and again in the browser, so a new price or a sale shows without a rebuild). The page
|
|
5
|
+
* never says what to charge: the platform reads the price itself when the visitor pays.
|
|
6
|
+
*
|
|
7
|
+
* A payment is always the site's own, so the site must say who it is (`siteId` in
|
|
8
|
+
* ferst-site.json). An item that isn't on offer, or a platform that can't be reached, fails the
|
|
9
|
+
* build: better the last good deploy stays live than a Pay button that can't take a payment.
|
|
10
|
+
*/
|
|
11
|
+
export interface PayItem {
|
|
12
|
+
id: string;
|
|
13
|
+
title: string;
|
|
14
|
+
/** In the smallest unit (pence). */
|
|
15
|
+
amount: number;
|
|
16
|
+
currency: string;
|
|
17
|
+
/** Where the browser posts the payment (the platform sends the visitor on to Stripe). */
|
|
18
|
+
action: string;
|
|
19
|
+
/** Where the page re-reads the current price. */
|
|
20
|
+
definition: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type FetchFn = (input: string, init?: RequestInit) => Promise<Response>;
|
|
24
|
+
|
|
25
|
+
/** £40, £40.50, £0.99; other currencies as "15 EUR". */
|
|
26
|
+
export function formatPrice(amount: number, currency: string): string {
|
|
27
|
+
const units = amount / 100;
|
|
28
|
+
const text = Number.isInteger(units) ? String(units) : units.toFixed(2);
|
|
29
|
+
return currency.toLowerCase() === 'gbp' ? `£${text}` : `${text} ${currency.toUpperCase()}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const cache = new Map<string, Promise<PayItem>>();
|
|
33
|
+
|
|
34
|
+
export function loadPayItem(item: string, opts: { base: string; siteId?: string | null; fetch?: FetchFn }): Promise<PayItem> {
|
|
35
|
+
const key = `${opts.base}|${opts.siteId ?? ''}|${item}`;
|
|
36
|
+
let hit = cache.get(key);
|
|
37
|
+
if (!hit) {
|
|
38
|
+
hit = fetchItem(item, opts.base, opts.siteId ?? null, opts.fetch ?? fetch);
|
|
39
|
+
cache.set(key, hit);
|
|
40
|
+
hit.catch(() => cache.delete(key)); // a failure is retried by the next caller
|
|
41
|
+
}
|
|
42
|
+
return hit;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function fetchItem(item: string, base: string, siteId: string | null, fetchFn: FetchFn): Promise<PayItem> {
|
|
46
|
+
if (!siteId) {
|
|
47
|
+
throw new Error(`Pay block: this site doesn't say who it is, so it can't take payments. Add "siteId" to ferst-site.json (or set FERST_SITE_ID).`);
|
|
48
|
+
}
|
|
49
|
+
const url = `${base}/sites/${encodeURIComponent(siteId)}/pay/${encodeURIComponent(item)}`;
|
|
50
|
+
let res: Response;
|
|
51
|
+
try {
|
|
52
|
+
res = await fetchFn(url, { headers: { Accept: 'application/json' } });
|
|
53
|
+
} catch (e) {
|
|
54
|
+
throw new Error(`Pay block: couldn’t reach the Ferst platform for item "${item}" (${url}): ${e instanceof Error ? e.message : String(e)}`);
|
|
55
|
+
}
|
|
56
|
+
if (res.status === 404) {
|
|
57
|
+
throw new Error(`Pay block: "${item}" isn’t on offer on this site. Check it in the portal (the Payments module: what your sites take payment for, and Payments switched on for the site), or run \`npx ferst-modules\` to list what this site can take payment for.`);
|
|
58
|
+
}
|
|
59
|
+
if (!res.ok) throw new Error(`Pay block: the Ferst platform answered ${res.status} for item "${item}" (${url}).`);
|
|
60
|
+
const d = (await res.json()) as { id: string; title: string; amount: number; currency?: string; action: string };
|
|
61
|
+
return { id: d.id, title: d.title, amount: d.amount, currency: d.currency ?? 'gbp', action: d.action, definition: url };
|
|
62
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Is this page being shown as a preview inside the portal (Make it yours, the Design tab)? It is
|
|
3
|
+
* when it's framed and was opened with `?preview` or is already in a preview session (links
|
|
4
|
+
* clicked inside the frame drop the query). The live theme listener and the cookie banner both ask:
|
|
5
|
+
* the banner stays out of the way of a preview, where nobody is visiting the site.
|
|
6
|
+
*/
|
|
7
|
+
export function inPreviewFrame(win: Pick<Window, 'parent' | 'location' | 'sessionStorage'> = window): boolean {
|
|
8
|
+
try {
|
|
9
|
+
if (win.parent === (win as unknown)) return false;
|
|
10
|
+
} catch {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
if (new URLSearchParams(win.location.search).has('preview')) return true;
|
|
14
|
+
try {
|
|
15
|
+
return win.sessionStorage.getItem('ferst-preview-on') === '1';
|
|
16
|
+
} catch {
|
|
17
|
+
return false; // sessionStorage blocked: only the ?preview query counts
|
|
18
|
+
}
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ferst-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Ferst Core — the shared, brand-agnostic client-site system: Astro components, layouts, content schemas, the layered config resolver, and a neutral styling architecture that every client overrides.",
|
|
6
6
|
"license": "BUSL-1.1",
|