@semics-tech/emdash-events 0.0.0-stage → 1.0.0-rc.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/LICENSE +21 -0
- package/README.md +82 -2
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +392 -0
- package/dist/manifest.json +401 -0
- package/dist/plugin.d.mts +11 -0
- package/dist/plugin.mjs +7 -0
- package/dist/ts/astro/lib/call.d.ts +54 -0
- package/dist/ts/astro/lib/call.js +121 -0
- package/dist/ts/integration/index.d.ts +48 -0
- package/dist/ts/integration/index.js +76 -0
- package/dist/ts/ui/index.d.ts +6 -0
- package/dist/ts/ui/index.js +46 -0
- package/emdash-plugin.jsonc +206 -0
- package/package.json +77 -3
- package/src/astro/AddToCalendar.astro +47 -0
- package/src/astro/BookingBlock.astro +30 -0
- package/src/astro/BookingPage.astro +193 -0
- package/src/astro/BookingStatus.astro +106 -0
- package/src/astro/BookingWidget.astro +581 -0
- package/src/astro/CheckIn.astro +186 -0
- package/src/astro/DoorSignPage.astro +62 -0
- package/src/astro/EventCard.astro +81 -0
- package/src/astro/EventList.astro +83 -0
- package/src/astro/OptOutPage.astro +58 -0
- package/src/astro/PaymentReturn.astro +96 -0
- package/src/astro/TicketPass.astro +95 -0
- package/src/astro/checkout-endpoint.ts +11 -0
- package/src/astro/index.js +18 -0
- package/src/astro/lib/call.ts +185 -0
- package/src/astro/lib/checkout-form.ts +271 -0
- package/src/astro/lib/list.ts +99 -0
- package/src/astro/lib/opt-out.ts +49 -0
- package/src/astro/lib/pass.ts +35 -0
- package/src/astro/lib/payment.ts +73 -0
- package/src/astro/lib/promo.ts +162 -0
- package/src/astro/lib/questions.ts +107 -0
- package/src/astro/lib/receipt.ts +132 -0
- package/src/astro/lib/waitlist.ts +72 -0
- package/src/astro/lib/widget.ts +278 -0
- package/src/astro/promo-endpoint.ts +11 -0
- package/src/astro/virtual.d.ts +16 -0
- package/src/astro/waitlist-endpoint.ts +12 -0
- package/src/client/booking.ts +188 -0
- package/src/client/checkin.ts +260 -0
- package/src/client/payment.ts +76 -0
- package/src/client/receipt.ts +75 -0
- package/src/i18n/ar.ts +213 -0
- package/src/i18n/en.ts +219 -0
- package/src/i18n/index.ts +121 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Astro component exports for the Events UI. Auto-wired through componentsEntry.
|
|
2
|
+
import AddToCalendar from "./AddToCalendar.astro";
|
|
3
|
+
import BookingBlock from "./BookingBlock.astro";
|
|
4
|
+
import BookingStatus from "./BookingStatus.astro";
|
|
5
|
+
import BookingWidget from "./BookingWidget.astro";
|
|
6
|
+
import EventCard from "./EventCard.astro";
|
|
7
|
+
import EventList from "./EventList.astro";
|
|
8
|
+
import PaymentReturn from "./PaymentReturn.astro";
|
|
9
|
+
import TicketPass from "./TicketPass.astro";
|
|
10
|
+
|
|
11
|
+
export const blockComponents = {
|
|
12
|
+
"events-booking": BookingBlock,
|
|
13
|
+
"events-list": EventList,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// The only way UI code calls the core (in-process public routes, fetch fallback).
|
|
17
|
+
export { callPublicRoute, EVENTS_PLUGIN_ID } from "./lib/call.ts";
|
|
18
|
+
export { AddToCalendar, BookingBlock, BookingStatus, BookingWidget, EventCard, EventList, PaymentReturn, TicketPass };
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// i18n-ignore-file: these errors are for developers (the UI shows its own text by error code, T102).
|
|
2
|
+
import { getPublicPluginApiRouteHandler, type PublicPluginRuntimeLocals } from "emdash/plugin-utils";
|
|
3
|
+
|
|
4
|
+
/** Plugin ID (manifest slug) of the sandboxed Events core. */
|
|
5
|
+
export const EVENTS_PLUGIN_ID = "semics-events";
|
|
6
|
+
|
|
7
|
+
export type PublicRouteMethod = "GET" | "POST";
|
|
8
|
+
|
|
9
|
+
export interface PublicRouteCall {
|
|
10
|
+
/** Route name as declared in the core, e.g. `public/events`. A leading slash is ignored. */
|
|
11
|
+
path: string;
|
|
12
|
+
/** Defaults to GET. */
|
|
13
|
+
method?: PublicRouteMethod;
|
|
14
|
+
/** JSON body for POST; query parameters for GET. */
|
|
15
|
+
body?: unknown;
|
|
16
|
+
/** Defaults to the Events core. */
|
|
17
|
+
pluginId?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The parts of the Astro global the helper needs; pass `Astro` itself. */
|
|
21
|
+
export interface PublicRouteContext {
|
|
22
|
+
locals: PublicPluginRuntimeLocals | null | undefined;
|
|
23
|
+
url: URL;
|
|
24
|
+
/**
|
|
25
|
+
* The visitor's request (`Astro.request`). Only its client-IP headers (and Cloudflare's `cf`)
|
|
26
|
+
* are passed on, so the core's per-IP limits see the visitor rather than one shared unknown
|
|
27
|
+
* address; the host still decides which of them to trust. Cookies and credentials never are.
|
|
28
|
+
*/
|
|
29
|
+
request?: Request;
|
|
30
|
+
/** Injected for tests. Defaults to the global `fetch`. */
|
|
31
|
+
fetch?: typeof fetch;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface PublicRouteIssue {
|
|
35
|
+
path: string;
|
|
36
|
+
code: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface PublicRouteError {
|
|
40
|
+
code: string;
|
|
41
|
+
message: string;
|
|
42
|
+
/** Input validation issues, when the route reported them (`BAD_REQUEST`). */
|
|
43
|
+
issues?: PublicRouteIssue[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type PublicRouteResult<T> =
|
|
47
|
+
| { ok: true; data: T; via: "in-process" | "fetch" }
|
|
48
|
+
| { ok: false; error: PublicRouteError; via: "in-process" | "fetch" };
|
|
49
|
+
|
|
50
|
+
interface Envelope {
|
|
51
|
+
success?: boolean;
|
|
52
|
+
data?: unknown;
|
|
53
|
+
error?: { code?: unknown; message?: unknown };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The only way UI code calls the core. Dispatches in-process through
|
|
58
|
+
* `getPublicPluginApiRouteHandler(Astro.locals)` (Spike S2: reaches sandboxed public routes on the
|
|
59
|
+
* workerd runner), and falls back to `fetch(Astro.url.origin + route)` when the in-process handler
|
|
60
|
+
* isn't on `locals`. Never throws: every failure comes back as `{ ok: false, error }` so components
|
|
61
|
+
* can render a defined error state.
|
|
62
|
+
*/
|
|
63
|
+
export async function callPublicRoute<T = unknown>(
|
|
64
|
+
ctx: PublicRouteContext,
|
|
65
|
+
call: PublicRouteCall,
|
|
66
|
+
): Promise<PublicRouteResult<T>> {
|
|
67
|
+
const method = call.method ?? "GET";
|
|
68
|
+
const path = call.path.replace(/^\/+/, "");
|
|
69
|
+
const pluginId = call.pluginId ?? EVENTS_PLUGIN_ID;
|
|
70
|
+
const handler = getPublicPluginApiRouteHandler(ctx.locals);
|
|
71
|
+
const via = handler ? "in-process" : "fetch";
|
|
72
|
+
|
|
73
|
+
const unsafeKey = method === "GET" ? tokenLikeKey(call.body) : undefined;
|
|
74
|
+
if (unsafeKey !== undefined) {
|
|
75
|
+
return {
|
|
76
|
+
ok: false,
|
|
77
|
+
error: {
|
|
78
|
+
code: "TOKEN_IN_QUERY",
|
|
79
|
+
message: `"${unsafeKey}" looks like a token; send it with POST, never in a URL`,
|
|
80
|
+
},
|
|
81
|
+
via,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const request = buildRequest(ctx.url.origin, pluginId, method, path, call.body, ctx.request);
|
|
85
|
+
|
|
86
|
+
if (handler) {
|
|
87
|
+
try {
|
|
88
|
+
return toResult<T>(await handler(pluginId, method, path, request), "in-process");
|
|
89
|
+
} catch (error) {
|
|
90
|
+
return failure(error, "in-process");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
const response = await (ctx.fetch ?? fetch)(request);
|
|
96
|
+
const envelope = (await response.json().catch(() => ({}))) as Envelope;
|
|
97
|
+
if (!response.ok && envelope.success !== false) {
|
|
98
|
+
return {
|
|
99
|
+
ok: false,
|
|
100
|
+
error: { code: `HTTP_${response.status}`, message: response.statusText || "Request failed" },
|
|
101
|
+
via: "fetch",
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return toResult<T>(envelope, "fetch");
|
|
105
|
+
} catch (error) {
|
|
106
|
+
return failure(error, "fetch");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Tokens must never travel in URLs (BUILD-PLAN R10), so GET bodies may not carry token-like keys. */
|
|
111
|
+
const TOKEN_LIKE = /^(t|.*token.*|.*receipt.*|.*secret.*|.*otp.*)$/i;
|
|
112
|
+
|
|
113
|
+
function tokenLikeKey(body: unknown): string | undefined {
|
|
114
|
+
if (!body || typeof body !== "object") return undefined;
|
|
115
|
+
return Object.keys(body).find((key) => TOKEN_LIKE.test(key));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Headers that carry the visitor's address; the host trusts them per its own configuration. */
|
|
119
|
+
const CLIENT_IP_HEADERS = ["cf-connecting-ip", "x-forwarded-for", "x-real-ip", "true-client-ip", "fly-client-ip"];
|
|
120
|
+
|
|
121
|
+
function clientInit(incoming: Request | undefined): { headers: Headers; cf?: unknown } {
|
|
122
|
+
const headers = new Headers();
|
|
123
|
+
if (!incoming) return { headers };
|
|
124
|
+
for (const name of CLIENT_IP_HEADERS) {
|
|
125
|
+
const value = incoming.headers.get(name);
|
|
126
|
+
if (value) headers.set(name, value);
|
|
127
|
+
}
|
|
128
|
+
const cf = (incoming as Request & { cf?: unknown }).cf;
|
|
129
|
+
return cf === undefined ? { headers } : { headers, cf };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function buildRequest(
|
|
133
|
+
origin: string,
|
|
134
|
+
pluginId: string,
|
|
135
|
+
method: string,
|
|
136
|
+
path: string,
|
|
137
|
+
body: unknown,
|
|
138
|
+
incoming?: Request,
|
|
139
|
+
): Request {
|
|
140
|
+
const url = new URL(`/_emdash/api/plugins/${encodeURIComponent(pluginId)}/${path}`, origin);
|
|
141
|
+
const init = clientInit(incoming);
|
|
142
|
+
if (method === "GET") {
|
|
143
|
+
if (body && typeof body === "object") {
|
|
144
|
+
for (const [key, value] of Object.entries(body)) {
|
|
145
|
+
for (const item of Array.isArray(value) ? value : [value]) {
|
|
146
|
+
if (item !== undefined && item !== null) url.searchParams.append(key, String(item));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return new Request(url, { ...init, method });
|
|
151
|
+
}
|
|
152
|
+
init.headers.set("content-type", "application/json");
|
|
153
|
+
return new Request(url, {
|
|
154
|
+
...init,
|
|
155
|
+
method,
|
|
156
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** A sandboxed route's own refusal (`{ ok: false, error, issues? }`), which the host wraps as a success. */
|
|
161
|
+
function refusal(data: unknown): PublicRouteError | undefined {
|
|
162
|
+
if (!data || typeof data !== "object") return undefined;
|
|
163
|
+
const body = data as { ok?: unknown; error?: { code?: unknown; message?: unknown }; issues?: unknown };
|
|
164
|
+
if (body.ok !== false || typeof body.error?.code !== "string") return undefined;
|
|
165
|
+
return {
|
|
166
|
+
code: body.error.code,
|
|
167
|
+
message: typeof body.error.message === "string" ? body.error.message : "Request failed",
|
|
168
|
+
...(Array.isArray(body.issues) ? { issues: body.issues as PublicRouteIssue[] } : {}),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function toResult<T>(envelope: Envelope, via: "in-process" | "fetch"): PublicRouteResult<T> {
|
|
173
|
+
if (envelope.success === true) {
|
|
174
|
+
const refused = refusal(envelope.data);
|
|
175
|
+
return refused ? { ok: false, error: refused, via } : { ok: true, data: envelope.data as T, via };
|
|
176
|
+
}
|
|
177
|
+
const code = typeof envelope.error?.code === "string" ? envelope.error.code : "ROUTE_CALL_FAILED";
|
|
178
|
+
const message = typeof envelope.error?.message === "string" ? envelope.error.message : "Request failed";
|
|
179
|
+
return { ok: false, error: { code, message }, via };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function failure<T>(error: unknown, via: "in-process" | "fetch"): PublicRouteResult<T> {
|
|
183
|
+
const message = error instanceof Error ? error.message : "Request failed";
|
|
184
|
+
return { ok: false, error: { code: "ROUTE_CALL_FAILED", message }, via };
|
|
185
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { callPublicRoute, type PublicRouteContext } from "./call";
|
|
2
|
+
import { isPayUrl } from "./payment";
|
|
3
|
+
import { answersFromForm } from "./questions";
|
|
4
|
+
import {
|
|
5
|
+
fieldsFromIssues,
|
|
6
|
+
newIdempotencyKey,
|
|
7
|
+
promoRefusal,
|
|
8
|
+
safeReturnTo,
|
|
9
|
+
validateCheckoutFields,
|
|
10
|
+
type WidgetField,
|
|
11
|
+
widgetErrorLocation,
|
|
12
|
+
} from "./widget";
|
|
13
|
+
|
|
14
|
+
export interface CheckoutFormOptions {
|
|
15
|
+
/** The core's plugin id. Defaults to the slug; registry installs pass their `r_…` id. */
|
|
16
|
+
pluginId?: string;
|
|
17
|
+
/** Injected for tests: makes the idempotency key when the form carries none. */
|
|
18
|
+
newKey?: () => string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface CheckoutRedirect {
|
|
22
|
+
location: string;
|
|
23
|
+
/** The booking's access token, for an HttpOnly cookie scoped to `/booking`. Never in a URL. */
|
|
24
|
+
cookie?: { name: string; value: string };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const BOOKING_PATH = "/booking";
|
|
28
|
+
/** Where the booking widget's form posts (injected by `eventsIntegration`). */
|
|
29
|
+
export const CHECKOUT_PATH = "/_events/checkout";
|
|
30
|
+
export const bookingCookieName = (ref: string) => `semics_booking_${ref}`;
|
|
31
|
+
|
|
32
|
+
export const QTY_PREFIX = "qty:";
|
|
33
|
+
|
|
34
|
+
/** The booking cookie: HttpOnly, scoped to the booking page, 30 days. */
|
|
35
|
+
export function bookingCookieOptions(url: URL) {
|
|
36
|
+
return {
|
|
37
|
+
path: BOOKING_PATH,
|
|
38
|
+
httpOnly: true,
|
|
39
|
+
sameSite: "lax" as const,
|
|
40
|
+
secure: url.protocol === "https:",
|
|
41
|
+
maxAge: 60 * 60 * 24 * 30,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* True for a form post another site made (CSRF): the browser says so in `Sec-Fetch-Site`, or, from
|
|
47
|
+
* older browsers, an `Origin` that is not this site's. The booking forms check this themselves so
|
|
48
|
+
* they stay safe on a site that turns off Astro's `security.checkOrigin`; the SameSite=Lax cookie
|
|
49
|
+
* alone would still let another site start bookings in a visitor's name. Safe methods and clients
|
|
50
|
+
* that send neither header (not browsers) pass.
|
|
51
|
+
*/
|
|
52
|
+
export function isCrossSiteRequest(request: Request, url: URL): boolean {
|
|
53
|
+
if (request.method === "GET" || request.method === "HEAD" || request.method === "OPTIONS") return false;
|
|
54
|
+
const site = request.headers.get("sec-fetch-site");
|
|
55
|
+
if (site) return site !== "same-origin" && site !== "none";
|
|
56
|
+
const origin = request.headers.get("origin");
|
|
57
|
+
if (origin === null || origin === url.origin) return false;
|
|
58
|
+
// Astro builds `url` from `localhost` unless the host is in `security.allowedDomains`; the Host
|
|
59
|
+
// the browser sent is the site it posted to, and another site cannot set it.
|
|
60
|
+
try {
|
|
61
|
+
return new URL(origin).host !== request.headers.get("host");
|
|
62
|
+
} catch {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The answer to a refused cross-site post (another site's form, not a page a visitor reads). */
|
|
68
|
+
export const crossSiteResponse = () =>
|
|
69
|
+
// i18n-ignore
|
|
70
|
+
new Response("Cross-site form submissions are not allowed.", {
|
|
71
|
+
status: 403,
|
|
72
|
+
headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" },
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const REF_PATTERN = /^[0-9A-HJKMNP-TV-Z]{10}$/;
|
|
76
|
+
/** What the booking page shows for an emailed link that is malformed, expired or already used. */
|
|
77
|
+
export const LINK_INVALID = "LINK_INVALID";
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The emailed link (`/booking?ref=&k=<one-time code>`, R34): only ever called from the booking
|
|
81
|
+
* page's POST (its "Open my booking" button), never on GET, so a mail scanner that prefetches the
|
|
82
|
+
* link cannot use up the code. Swaps the code for a fresh access token through
|
|
83
|
+
* `public/booking/redeem` and lands on `/booking?ref=` without `k`, the token in the cookie.
|
|
84
|
+
*/
|
|
85
|
+
export async function redeemBookingLink(
|
|
86
|
+
ctx: PublicRouteContext,
|
|
87
|
+
link: { ref: string; code: string },
|
|
88
|
+
options: Pick<CheckoutFormOptions, "pluginId">,
|
|
89
|
+
): Promise<CheckoutRedirect> {
|
|
90
|
+
if (!REF_PATTERN.test(link.ref)) return fail(LINK_INVALID);
|
|
91
|
+
if (!link.code) return fail(LINK_INVALID, link.ref);
|
|
92
|
+
const redeemed = await callPublicRoute<{ bookingRef: string; accessToken: string }>(ctx, {
|
|
93
|
+
method: "POST",
|
|
94
|
+
path: "public/booking/redeem",
|
|
95
|
+
pluginId: options.pluginId,
|
|
96
|
+
body: { bookingRef: link.ref, code: link.code },
|
|
97
|
+
});
|
|
98
|
+
if (!redeemed.ok || !redeemed.data.accessToken) return fail(LINK_INVALID, link.ref);
|
|
99
|
+
return {
|
|
100
|
+
location: `${BOOKING_PATH}?ref=${encodeURIComponent(link.ref)}`,
|
|
101
|
+
cookie: { name: bookingCookieName(link.ref), value: redeemed.data.accessToken },
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** What the booking page shows for a waitlist offer link that lapsed, was used, or is not an offer. */
|
|
106
|
+
export const OFFER_INVALID = "OFFER_INVALID";
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The emailed waitlist offer (`/booking?ref=&k=<one-time code>&offer=1`, T081): like the booking link,
|
|
110
|
+
* only ever from the page's POST ("Claim my tickets"). Claims it through `public/waitlist/claim`, keeps
|
|
111
|
+
* the fresh token in the cookie and continues the booking as "Pay now" does: a free booking is
|
|
112
|
+
* confirmed, a paid one goes to the payment page.
|
|
113
|
+
*/
|
|
114
|
+
export async function claimOfferLink(
|
|
115
|
+
ctx: PublicRouteContext,
|
|
116
|
+
link: { ref: string; code: string },
|
|
117
|
+
options: Pick<CheckoutFormOptions, "pluginId">,
|
|
118
|
+
): Promise<CheckoutRedirect> {
|
|
119
|
+
const ref = REF_PATTERN.test(link.ref) ? link.ref : "";
|
|
120
|
+
if (!ref || !link.code) return fail(OFFER_INVALID, ref);
|
|
121
|
+
const claimed = await callPublicRoute<{ accessToken: string }>(ctx, {
|
|
122
|
+
method: "POST",
|
|
123
|
+
path: "public/waitlist/claim",
|
|
124
|
+
pluginId: options.pluginId,
|
|
125
|
+
body: { bookingRef: ref, code: link.code },
|
|
126
|
+
});
|
|
127
|
+
if (!claimed.ok || !claimed.data.accessToken) return fail(OFFER_INVALID, ref);
|
|
128
|
+
const token = claimed.data.accessToken;
|
|
129
|
+
const next = await resumePayment(ctx, { ref, token }, options);
|
|
130
|
+
return { ...next, cookie: { name: bookingCookieName(ref), value: token } };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function text(form: FormData, key: string): string {
|
|
134
|
+
const value = form.get(key);
|
|
135
|
+
return typeof value === "string" ? value.trim() : "";
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function fail(code: string, ref?: string, cookie?: CheckoutRedirect["cookie"]): CheckoutRedirect {
|
|
139
|
+
const query = new URLSearchParams();
|
|
140
|
+
if (ref) query.set("ref", ref);
|
|
141
|
+
query.set("error", code);
|
|
142
|
+
return { location: `${BOOKING_PATH}?${query}`, ...(cookie ? { cookie } : {}) };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The no-JS checkout (`POST /_events/checkout`): reserve, then continue, through the public routes
|
|
147
|
+
* only. The access token goes back as a cookie value, never in the redirect URL.
|
|
148
|
+
*
|
|
149
|
+
* A form from the booking widget carries `returnTo` (its own page): anything refused before a
|
|
150
|
+
* booking exists goes back there with the error code and the names of the fields to fix, never
|
|
151
|
+
* the values typed. Without a usable `returnTo`, refusals land on the booking page as before.
|
|
152
|
+
*/
|
|
153
|
+
export async function handleCheckoutForm(
|
|
154
|
+
ctx: PublicRouteContext,
|
|
155
|
+
form: FormData,
|
|
156
|
+
options: CheckoutFormOptions,
|
|
157
|
+
): Promise<CheckoutRedirect> {
|
|
158
|
+
const pluginId = options.pluginId;
|
|
159
|
+
const lines: Array<{ ticketTypeId: string; qty: number }> = [];
|
|
160
|
+
for (const [key, value] of form.entries()) {
|
|
161
|
+
if (!key.startsWith(QTY_PREFIX) || typeof value !== "string") continue;
|
|
162
|
+
const qty = Number.parseInt(value, 10);
|
|
163
|
+
if (qty > 0) lines.push({ ticketTypeId: key.slice(QTY_PREFIX.length), qty });
|
|
164
|
+
}
|
|
165
|
+
const eventId = text(form, "eventId");
|
|
166
|
+
const returnTo = safeReturnTo(text(form, "returnTo"), ctx.url.origin);
|
|
167
|
+
const refuse = (code: string, fields: WidgetField[] = []) =>
|
|
168
|
+
returnTo && eventId ? { location: widgetErrorLocation(returnTo, eventId, code, fields) } : fail(code);
|
|
169
|
+
if (!eventId) return fail("BAD_REQUEST");
|
|
170
|
+
|
|
171
|
+
const occurrenceId = text(form, "occurrenceId");
|
|
172
|
+
const buyer = { name: text(form, "name"), email: text(form, "email") };
|
|
173
|
+
const invalid = validateCheckoutFields({
|
|
174
|
+
...buyer,
|
|
175
|
+
lines: lines.length,
|
|
176
|
+
needsOccurrence: form.get("needsOccurrence") === "1",
|
|
177
|
+
occurrenceId,
|
|
178
|
+
});
|
|
179
|
+
if (invalid.length > 0) return refuse("BAD_REQUEST", invalid);
|
|
180
|
+
|
|
181
|
+
const turnstileToken = text(form, "cf-turnstile-response");
|
|
182
|
+
// Only a typed code is sent: Free's reserve refuses unknown keys, and only Pro offers the field.
|
|
183
|
+
const promoCode = text(form, "promoCode");
|
|
184
|
+
// Likewise answers: only a widget that showed questions (Pro's `public/event` lists them) sends any.
|
|
185
|
+
const answers = answersFromForm(form);
|
|
186
|
+
const reserved = await callPublicRoute<{ bookingRef: string; accessToken?: string }>(ctx, {
|
|
187
|
+
method: "POST",
|
|
188
|
+
path: "public/checkout/reserve",
|
|
189
|
+
pluginId,
|
|
190
|
+
body: {
|
|
191
|
+
eventId,
|
|
192
|
+
...(occurrenceId ? { occurrenceId } : {}),
|
|
193
|
+
lines,
|
|
194
|
+
buyer,
|
|
195
|
+
idempotencyKey: text(form, "idempotencyKey") || (options.newKey ?? newIdempotencyKey)(),
|
|
196
|
+
...(turnstileToken ? { turnstileToken } : {}),
|
|
197
|
+
...(promoCode ? { promoCode } : {}),
|
|
198
|
+
...(answers ? { answers } : {}),
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
if (!reserved.ok) {
|
|
202
|
+
const { issues } = reserved.error;
|
|
203
|
+
const code = promoCode ? promoRefusal(reserved.error.code) : reserved.error.code;
|
|
204
|
+
const fields: WidgetField[] = code.startsWith("PROMO_") ? ["promo"] : [];
|
|
205
|
+
return refuse(code, code === "BAD_REQUEST" ? fieldsFromIssues(issues) : fields);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const { bookingRef, accessToken } = reserved.data;
|
|
209
|
+
const done = `${BOOKING_PATH}?ref=${encodeURIComponent(bookingRef)}`;
|
|
210
|
+
// A resubmit of a booking that is already confirmed gets no new token: this browser has its cookie.
|
|
211
|
+
if (!accessToken) return { location: done };
|
|
212
|
+
const cookie = { name: bookingCookieName(bookingRef), value: accessToken };
|
|
213
|
+
const next = await callPublicRoute<{ next?: { type?: string; url?: string } }>(ctx, {
|
|
214
|
+
method: "POST",
|
|
215
|
+
path: "public/checkout/continue",
|
|
216
|
+
pluginId,
|
|
217
|
+
body: { bookingRef, accessToken },
|
|
218
|
+
});
|
|
219
|
+
if (!next.ok) return fail(next.error.code, bookingRef, cookie);
|
|
220
|
+
|
|
221
|
+
const step = next.data.next;
|
|
222
|
+
if (step?.type === "pay")
|
|
223
|
+
return isPayUrl(step.url) ? { location: step.url, cookie } : fail("PAYMENT_FAILED", bookingRef, cookie);
|
|
224
|
+
return { location: done, cookie };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* "Pay now" on the booking page (EU-FR-09): continues the held or awaiting-payment booking again
|
|
229
|
+
* with the cookie's token (Events Pro answers with the payment page again, or a new one) and follows
|
|
230
|
+
* the `pay` step. Anything else lands on the booking page, which shows the booking as it is.
|
|
231
|
+
*/
|
|
232
|
+
export async function resumePayment(
|
|
233
|
+
ctx: PublicRouteContext,
|
|
234
|
+
input: { ref: string; token: string },
|
|
235
|
+
options: Pick<CheckoutFormOptions, "pluginId">,
|
|
236
|
+
): Promise<{ location: string }> {
|
|
237
|
+
if (!REF_PATTERN.test(input.ref)) return { location: BOOKING_PATH };
|
|
238
|
+
const page = `${BOOKING_PATH}?ref=${encodeURIComponent(input.ref)}`;
|
|
239
|
+
if (!input.token) return { location: page };
|
|
240
|
+
const next = await callPublicRoute<{ next?: { type?: string; url?: unknown } }>(ctx, {
|
|
241
|
+
method: "POST",
|
|
242
|
+
path: "public/checkout/continue",
|
|
243
|
+
pluginId: options.pluginId,
|
|
244
|
+
body: { bookingRef: input.ref, accessToken: input.token },
|
|
245
|
+
});
|
|
246
|
+
if (!next.ok) return fail(next.error.code, input.ref);
|
|
247
|
+
const step = next.data.next;
|
|
248
|
+
if (step?.type === "pay") return isPayUrl(step.url) ? { location: step.url } : fail("PAYMENT_FAILED", input.ref);
|
|
249
|
+
return { location: page };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** What `POST /_events/checkout` gets from Astro. */
|
|
253
|
+
export interface CheckoutEndpointContext {
|
|
254
|
+
request: Request;
|
|
255
|
+
url: URL;
|
|
256
|
+
locals: PublicRouteContext["locals"];
|
|
257
|
+
redirect(location: string, status?: number): Response;
|
|
258
|
+
cookies: { set(name: string, value: string, options: Record<string, unknown>): void };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** `POST /_events/checkout`: refuse cross-site posts, then run the form and redirect (303). */
|
|
262
|
+
export async function handleCheckoutRequest(
|
|
263
|
+
{ request, url, locals, redirect, cookies }: CheckoutEndpointContext,
|
|
264
|
+
options: CheckoutFormOptions,
|
|
265
|
+
): Promise<Response> {
|
|
266
|
+
if (isCrossSiteRequest(request, url)) return crossSiteResponse();
|
|
267
|
+
const form = await request.formData().catch(() => new FormData());
|
|
268
|
+
const result = await handleCheckoutForm({ locals, url, request }, form, options);
|
|
269
|
+
if (result.cookie) cookies.set(result.cookie.name, result.cookie.value, bookingCookieOptions(url));
|
|
270
|
+
return redirect(result.location, 303);
|
|
271
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// The UI ships as source, so it cannot import the private @semics-tech/shared (scripts/ui-imports.test.mjs).
|
|
2
|
+
function isValidTimeZone(timeZone: string): boolean {
|
|
3
|
+
try {
|
|
4
|
+
new Intl.DateTimeFormat("en-GB", { timeZone });
|
|
5
|
+
return true;
|
|
6
|
+
} catch {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function formatInZone(utcMs: number, timeZone: string, locale: string, options: Intl.DateTimeFormatOptions): string {
|
|
12
|
+
return new Intl.DateTimeFormat(locale, { ...options, timeZone }).format(new Date(utcMs));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
import { t } from "../../i18n/index";
|
|
16
|
+
import { formatMoney } from "./widget";
|
|
17
|
+
|
|
18
|
+
/** One row of `public/events` (T046): public fields only, never counters. */
|
|
19
|
+
export interface PublicEventSummary {
|
|
20
|
+
eventId: string;
|
|
21
|
+
title: string;
|
|
22
|
+
status: "on_sale" | "paused" | "closed" | "cancelled";
|
|
23
|
+
startsAt: string;
|
|
24
|
+
endsAt?: string | null;
|
|
25
|
+
timezone?: string | null;
|
|
26
|
+
venue?: string | null;
|
|
27
|
+
currency: string;
|
|
28
|
+
priceFromMinor?: number;
|
|
29
|
+
/** Seats left (never negative). */
|
|
30
|
+
available: number;
|
|
31
|
+
/** The entry's slug (T056); absent for an event whose info predates it. */
|
|
32
|
+
slug?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Where a card links by default: the site's `/events/<slug>` page, or nowhere without a slug. */
|
|
36
|
+
export const EVENTS_PATH = "/events";
|
|
37
|
+
export function eventHref(event: Pick<PublicEventSummary, "slug">): string | undefined {
|
|
38
|
+
return event.slug ? `${EVENTS_PATH}/${encodeURIComponent(event.slug)}` : undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface CardView {
|
|
42
|
+
eventId: string;
|
|
43
|
+
title: string;
|
|
44
|
+
/** Start in the event's own zone, with the zone's short name. */
|
|
45
|
+
when: string;
|
|
46
|
+
/** The start as an ISO instant, for `<time datetime>`. */
|
|
47
|
+
datetime: string;
|
|
48
|
+
venue: string | null;
|
|
49
|
+
price: string | null;
|
|
50
|
+
/** "Sold out", "Cancelled", ... or `null` when the event can be booked. */
|
|
51
|
+
badge: string | null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const WHEN: Intl.DateTimeFormatOptions = {
|
|
55
|
+
weekday: "short",
|
|
56
|
+
day: "numeric",
|
|
57
|
+
month: "short",
|
|
58
|
+
year: "numeric",
|
|
59
|
+
hour: "2-digit",
|
|
60
|
+
minute: "2-digit",
|
|
61
|
+
hourCycle: "h23",
|
|
62
|
+
timeZoneName: "short",
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const BADGES = { cancelled: "list.badge.cancelled", closed: "list.badge.closed", paused: "list.badge.paused" } as const;
|
|
66
|
+
|
|
67
|
+
/** The start in the event's own zone; a missing or unknown zone shows UTC rather than failing. */
|
|
68
|
+
export function eventWhen(startsAt: string, timezone: string | null | undefined, locale: string): string {
|
|
69
|
+
const zone = timezone && isValidTimeZone(timezone) ? timezone : "UTC";
|
|
70
|
+
return formatInZone(Date.parse(startsAt), zone, locale, WHEN);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function cardView(event: PublicEventSummary, locale: string): CardView {
|
|
74
|
+
const badge =
|
|
75
|
+
event.status !== "on_sale"
|
|
76
|
+
? t(locale, BADGES[event.status])
|
|
77
|
+
: event.available === 0
|
|
78
|
+
? t(locale, "list.badge.soldOut")
|
|
79
|
+
: null;
|
|
80
|
+
let price: string | null = null;
|
|
81
|
+
if (event.priceFromMinor !== undefined) {
|
|
82
|
+
const from = formatMoney(event.priceFromMinor, event.currency, locale);
|
|
83
|
+
price = event.priceFromMinor === 0 ? from : t(locale, "list.priceFrom", { price: from });
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
eventId: event.eventId,
|
|
87
|
+
title: event.title,
|
|
88
|
+
when: eventWhen(event.startsAt, event.timezone, locale),
|
|
89
|
+
datetime: event.startsAt,
|
|
90
|
+
venue: event.venue ?? null,
|
|
91
|
+
price,
|
|
92
|
+
badge,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The first `limit` events (as `public/events` sorted them: soonest first) as cards. */
|
|
97
|
+
export function listView(events: PublicEventSummary[], options: { limit: number; locale: string }): CardView[] {
|
|
98
|
+
return events.slice(0, Math.max(0, options.limit)).map((event) => cardView(event, options.locale));
|
|
99
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The opt-out page for reminder and update emails (EV-FR-08, T083, R57). Events Pro's reminders and
|
|
3
|
+
* update notices link to `/_events/opt-out?ref=<booking ref>&w=<recipient>&s=<signature>`. Opening the
|
|
4
|
+
* link changes nothing (a mail scanner that prefetches it unsubscribes no one); the page shows a
|
|
5
|
+
* button whose same-site POST asks the core's `public/mail/opt-out`. The route is Events Pro's: a Free
|
|
6
|
+
* core answers NOT_FOUND, which the page shows as an invalid link. Cancellation notices are always sent.
|
|
7
|
+
*/
|
|
8
|
+
import { t } from "../../i18n/index";
|
|
9
|
+
import { callPublicRoute, type PublicRouteContext } from "./call";
|
|
10
|
+
|
|
11
|
+
export const OPT_OUT_PATH = "/_events/opt-out";
|
|
12
|
+
|
|
13
|
+
export interface OptOutLink {
|
|
14
|
+
bookingRef: string;
|
|
15
|
+
/** `buyer`, or the seat's attendee id. */
|
|
16
|
+
who: string;
|
|
17
|
+
sig: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The outcomes in English (the page shows `optOut.<outcome>` in its language, T102). */
|
|
21
|
+
export const OPT_OUT_MESSAGES = { done: t("en", "optOut.done"), invalid: t("en", "optOut.invalid") } as const;
|
|
22
|
+
|
|
23
|
+
const REF = /^[0-9A-Z]{10}$/;
|
|
24
|
+
const WHO = /^[\w-]{1,120}$/;
|
|
25
|
+
const SIG = /^[0-9a-f]{32}$/;
|
|
26
|
+
|
|
27
|
+
/** The link's values from the page's query (GET) or its form (POST); null when malformed. */
|
|
28
|
+
export function readOptOutLink(source: { get(name: string): unknown }): OptOutLink | null {
|
|
29
|
+
const [bookingRef, who, sig] = ["ref", "w", "s"].map((k) => {
|
|
30
|
+
const v = source.get(k);
|
|
31
|
+
return typeof v === "string" ? v : "";
|
|
32
|
+
}) as [string, string, string];
|
|
33
|
+
return REF.test(bookingRef) && WHO.test(who) && SIG.test(sig) ? { bookingRef, who, sig } : null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The button's POST: `done`, or `invalid` for any refusal (also a core without the route). */
|
|
37
|
+
export async function optOut(
|
|
38
|
+
ctx: PublicRouteContext,
|
|
39
|
+
link: OptOutLink,
|
|
40
|
+
options: { pluginId?: string },
|
|
41
|
+
): Promise<keyof typeof OPT_OUT_MESSAGES> {
|
|
42
|
+
const result = await callPublicRoute(ctx, {
|
|
43
|
+
method: "POST",
|
|
44
|
+
path: "public/mail/opt-out",
|
|
45
|
+
pluginId: options.pluginId,
|
|
46
|
+
body: link,
|
|
47
|
+
});
|
|
48
|
+
return result.ok ? "done" : "invalid";
|
|
49
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Draws a ticket pass's QR code (EV-FR-11, T082). The matrix comes from the core: Events Pro's
|
|
3
|
+
* `public/booking` adds `qr: { size, bits }` beside each pass's `code` (row-major modules, four per
|
|
4
|
+
* hex digit, most significant first, 1 = dark); Free sends none and the pass shows the code as text
|
|
5
|
+
* only. This only turns a well-formed matrix into an SVG path of horizontal runs; anything else is
|
|
6
|
+
* `null`, so nothing but numbers and path commands ever reaches the markup.
|
|
7
|
+
*/
|
|
8
|
+
export interface QrPath {
|
|
9
|
+
size: number;
|
|
10
|
+
d: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** QR sizes run from 21 (version 1) to 177 (version 40) modules, in steps of 4. */
|
|
14
|
+
export function qrPath(qr: unknown): QrPath | null {
|
|
15
|
+
if (!qr || typeof qr !== "object") return null;
|
|
16
|
+
const { size, bits } = qr as { size?: unknown; bits?: unknown };
|
|
17
|
+
if (typeof size !== "number" || !Number.isInteger(size) || size < 21 || size > 177 || (size - 17) % 4 !== 0) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
if (typeof bits !== "string" || bits.length !== Math.ceil((size * size) / 4) || !/^[0-9a-f]+$/.test(bits)) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const dark = (i: number) => ((Number.parseInt(bits[i >> 2] as string, 16) >> (3 - (i & 3))) & 1) === 1;
|
|
24
|
+
let d = "";
|
|
25
|
+
for (let y = 0; y < size; y++) {
|
|
26
|
+
for (let x = 0; x < size; x++) {
|
|
27
|
+
if (!dark(y * size + x)) continue;
|
|
28
|
+
let run = 1;
|
|
29
|
+
while (x + run < size && dark(y * size + x + run)) run++;
|
|
30
|
+
d += `M${x} ${y}h${run}v1h-${run}z`;
|
|
31
|
+
x += run;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { size, d };
|
|
35
|
+
}
|