Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/features/public/qr-book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { type CheckoutIntent, checkoutItem } from "#shared/payments.ts";
import { listingSupportsDirectCheckout } from "#shared/qr.ts";
import { type QrBookPayload, verifyQrBookToken } from "#shared/qr-token.ts";
import type { ListingWithCount } from "#shared/types.ts";
import { qrBookErrorPage } from "#templates/public/errors.tsx";
import { qrBookCheckoutErrorPage, qrBookErrorPage } from "#templates/public/errors.tsx";
import type {
BookingPrefill,
TicketPrefill,
Expand Down Expand Up @@ -106,15 +106,21 @@ const buildCheckoutIntent = (
special_instructions: "",
});

/** Redirect directly to Stripe checkout using the signed values */
/** Redirect directly to Stripe checkout using the signed values. The payment
* flow's error (a provider validation refusal at 400, or a null/failed session
* at 500 with a generic message) is passed straight through to the page rather
* than replaced with a generic 500, so the visitor sees why it failed. */
const skipToCheckout = (
request: Request,
listing: ListingWithCount,
payload: QrBookPayload,
): Promise<Response> => {
const intent = buildCheckoutIntent(listing, payload);
return runCheckoutFlow(`qr-book listing=${listing.id}`, request, intent, () =>
errorResponse(listing.slug, 500),
return runCheckoutFlow(
`qr-book listing=${listing.id}`,
request,
intent,
(msg, status) => htmlResponse(qrBookCheckoutErrorPage(listing.slug, msg), status),
);
};

Expand Down
2 changes: 2 additions & 0 deletions src/locales/en/public.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
"public.qr_book_error.heading": "QR code expired or invalid",
"public.qr_book_error.message": "This QR code has expired or the link has been tampered with. Ask the organiser to generate a new one, or use the normal booking page below.",
"public.qr_book_error.booking_link": "Go to booking page",
"public.qr_book_checkout_error.title": "Could not start payment",
"public.qr_book_checkout_error.heading": "Could not start your payment",
"public.rate_limited.title": "Too Many Requests",
"public.rate_limited.heading": "Too Many Requests",
"public.rate_limited.message": "You've hit too many invalid ticket links. Please wait a few minutes and try again.",
Expand Down
45 changes: 38 additions & 7 deletions src/ui/templates/public/errors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ export const notFoundPage = (): string =>
</Layout>,
);

/** The fallback "Go to booking page" link shown on a QR-book error page —
* only when the listing has a standalone /ticket/<slug> page. A `null` slug
* (the listing has no standalone page, e.g. a non-standalone child or a hidden
* package member whose `/ticket/<slug>` 404s) renders nothing, so the page
* never offers a dead link. */
const qrBookBookingLink = (slug: string | null): JSX.Element | false =>
slug !== null && (
<p>
<a href={`/ticket/${escapeHtml(slug)}`}>
{t("public.qr_book_error.booking_link")}
</a>
</p>
);

/**
* QR booking link error page shown when a signed link is invalid or expired.
* Includes a fallback link to the listing's normal booking page — but only when
Expand All @@ -53,13 +67,30 @@ export const qrBookErrorPage = (slug: string | null): string =>
)(
<>
<p>{t("public.qr_book_error.message")}</p>
{slug !== null && (
<p>
<a href={`/ticket/${escapeHtml(slug)}`}>
{t("public.qr_book_error.booking_link")}
</a>
</p>
)}
{qrBookBookingLink(slug)}
</>,
);

/**
* Error page shown when a QR direct-to-checkout booking could not start its
* payment session: the provider refused the booking (HTTP 400, with the
* provider's own message) or the session could not be created (HTTP 500, with
* a generic "try again" message). The payment flow supplies the message and
* status; the page offers the same fallback booking link as the token-error
* page, since a listing that can skip straight to checkout always has a
* standalone /ticket/<slug> page to fall back to.
*/
export const qrBookCheckoutErrorPage = (
slug: string,
message: string,
): string =>
simplePublicPage(
t("public.qr_book_checkout_error.title"),
t("public.qr_book_checkout_error.heading"),
)(
<>
<p>{message}</p>
{qrBookBookingLink(slug)}
</>,
);

Expand Down
64 changes: 44 additions & 20 deletions test/lib/server-qr-book.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { toMinorUnits } from "#shared/currency.ts";
import { addDays } from "#shared/dates.ts";
import { listingsTable } from "#shared/db/listings/records.ts";
import { settings } from "#shared/db/settings.ts";
import { paymentsApi } from "#shared/payments.ts";
import {
type CheckoutSessionResult,
paymentsApi,
} from "#shared/payments.ts";
import {
buildQrBookPayload,
QR_TOKEN_MAX_AGE_S,
Expand Down Expand Up @@ -127,6 +130,27 @@ const scanWithStripe = async (
});
};

/** Scan a direct-checkout QR with a fixed provider result. */
const scanWithCheckoutResult = async (
listing: { slug: string },
result: CheckoutSessionResult,
): Promise<Response> => {
const providerStub = stub(paymentsApi, "getConfiguredProvider", () =>
mockProviderType("stripe"),
);
const checkoutStub = stub(
stripePaymentProvider,
"createCheckoutSession",
() => Promise.resolve(result),
);
try {
return await scanRequest(listing);
} finally {
checkoutStub.restore();
providerStub.restore();
}
};
Comment thread
stefan-burke marked this conversation as resolved.
Resolved

describeWithEnv("qr-book scan handler", { db: true }, () => {
describe("error paths", () => {
test("missing ?t= token renders the error page", async () => {
Expand Down Expand Up @@ -340,26 +364,26 @@ describeWithEnv("qr-book scan handler", { db: true }, () => {
maxAttendees: 10,
unitPrice: 500,
});
const token = await bookToken(listing.slug);
const providerStub = stub(paymentsApi, "getConfiguredProvider", () =>
mockProviderType("stripe"),
);
const checkoutStub = stub(
stripePaymentProvider,
"createCheckoutSession",
() => Promise.resolve(null),
const response = await scanWithCheckoutResult(listing, null);
expect(response.status).toBe(500);
expect(await response.text()).toContain(
"Failed to create payment session. Please try again.",
);
try {
const response = await awaitTestRequest(
qrBookPath(listing.slug, token),
);
expect(response.status).toBe(500);
const body = await response.text();
expect(body).toContain("expired or invalid");
} finally {
checkoutStub.restore();
providerStub.restore();
}
});

test("returns a provider validation refusal with its HTTP 400 status", async () => {
const listing = await createTestListing({
fields: "",
maxAttendees: 10,
unitPrice: 500,
});
const refusal =
"The payment processor rejected the phone number as invalid. Please correct it and try again.";
const response = await scanWithCheckoutResult(listing, {
error: refusal,
});
expect(response.status).toBe(400);
expect(await response.text()).toContain(refusal);
});

test("falls through to the form when the listing requires email", async () => {
Expand Down