Setting the file. One moment.
Registration · Wix Headless Templates · wix/skills · Skills Docs
ContentsBack to the top of the page Bundled file Orders
events/rest/ registration.ts
TypeScript · 96 lines · 6 KB
12 import { wixRequest } from "../../shared/rest/client.js" ;
13 import {
14 availableTiersQuery,
15 checkoutCallbacks,
16 redirectResult,
17 reservationError,
18 reservationId,
19 reservationTickets,
20 rsvpBody,
21 rsvpFormOf,
22 rsvpResult,
23 toTiers,
24 type CheckoutEvent,
25 type CheckoutPaths,
26 type RsvpValues,
27 type TierEvent,
28 } from "../app/wix/events/registration-core.js" ;
29 import type { Raw } from "../app/wix/events/events-core.js" ;
30 import type { EventDetail, RegistrationResult, TicketSelection, TicketTier } from "../app/wix/events/types.js" ;
31
32 export { type CheckoutPaths, type RsvpValues, type TierEvent };
33
34 /**
35 * Ticket tiers for a TICKETING event, in the organizer's order — the VISITOR-public V3 read (hidden
36 * tiers excluded server-side), never the ticket-definitions management query (403 for a visitor).
37 * Pass the EventDetail: sale dates format in its zone, its tax settings price the notes.
38 * POST /events/v3/ticket-definitions/available/query { query: { filter: { eventId }, sort: [{ fieldName: "sortIndex", order: "ASC" }] } } → { ticketDefinitions }
39 */
40 export async function fetchTicketTiers ( event : TierEvent ) : Promise < TicketTier []> {
41 const res = await wixRequest < Raw >( "/events/v3/ticket-definitions/available/query" , { body: { query: availableTiersQuery (event.id) } });
42 return toTiers (res?.ticketDefinitions, event);
43 }
44
45 /**
46 * RSVP to a free (RSVP-type) event — completes fully client-side: no reservation, no redirect, no
47 * payment. `values` answer the ORGANIZER'S form (event.rsvpForm), keyed by input name; the body
48 * carries the built-in identity at the top level, the answers as `form.inputValues`, and
49 * `additionalGuestDetails` when the form has a guest control. "NO" only when the event's
50 * rsvpResponseType is "YES_AND_NO". Throws on closed registration, a guest limit, a missing
51 * mandatory answer (INVALID_FORM_RESPONSE), or an invalid email — surface the message. A full
52 * event with a waitlist answers "WAITLIST".
53 * POST /events/v2/rsvps { rsvp: { eventId, status, firstName, lastName, email, form: { inputValues: [{ inputName, value | values }] }, additionalGuestDetails?: { guestCount, guestNames } } } → { rsvp: { id, status } }
54 */
55 export async function submitRsvp (
56 event : Pick < EventDetail , "id" | "rsvpForm" >,
57 values : RsvpValues ,
58 status : "YES" | "NO" = "YES" ,
59 guests : { guestCount : number ; guestNames : string [] } = { guestCount: 0 , guestNames: [] },
60 ) : Promise < RegistrationResult > {
61 const res = await wixRequest < Raw >( "/events/v2/rsvps" , { body: { rsvp: rsvpBody (event.id, rsvpFormOf (event), values, status, guests) } });
62 return rsvpResult (res?.rsvp, status);
63 }
64
65 /**
66 * Ticketed checkout, the exact sequence: reserve the selected tiers (a PENDING hold that
67 * auto-expires), then mint the Wix-hosted checkout redirect and return its URL — the caller
68 * navigates the FULL document to it; Wix collects guest details + payment and emails the tickets.
69 * Selections carry the pricing option or the guest's price where the tier needs one (`ticketInfo`);
70 * pass `tiers` so a missing guest price is refused before the request. `origin` must be the site's
71 * real https origin as registered on the OAuth app's allowed domains (browser:
72 * window.location.origin). Never hand-build the checkout URL. `paths` overrides the return routes —
73 * a static site passes `{ confirmation: "/event-confirmation.html", event: "/event.html?slug=…" }`.
74 * POST /events/v1/ticket-reservations { ticketReservation: { tickets: [{ eventId, ticketDefinitionId, quantity, ticketInfo?: { pricingOptionId?, guestPrice? } }] } } → { ticketReservation: { id, status: "PENDING", expirationDate } }
75 * POST /headless/v1/redirect-session { eventsCheckout: { reservationId, eventSlug }, callbacks: { thankYouPageUrl, postFlowUrl } }
76 */
77 export async function startTicketCheckout (
78 event : CheckoutEvent ,
79 selections : TicketSelection [],
80 paths : CheckoutPaths = {},
81 tiers : TicketTier [] = [],
82 origin : string = typeof window !== "undefined" ? window.location.origin : "" ,
83 ) : Promise < RegistrationResult > {
84 const tickets = reservationTickets (event.id, selections, tiers);
85 let reservation : Raw | undefined ;
86 try {
87 // Until the site has a premium plan AND a payment method this is a 403 NO_PAYMENT_METHOD_CONFIGURED — a business gate, not a bug.
88 reservation = ( await wixRequest < Raw >( "/events/v1/ticket-reservations" , { body: { ticketReservation: { tickets } } }))?.ticketReservation;
89 } catch (e) {
90 throw reservationError (e);
91 }
92 const session = await wixRequest < Raw >( "/headless/v1/redirect-session" , {
93 body: { eventsCheckout: { reservationId: reservationId (reservation), eventSlug: event.slug }, callbacks: checkoutCallbacks (origin, event.slug, paths) },
94 });
95 return redirectResult (session);
96 }