caelum0x f1647a5a81 feat: embeddable checkout overlay (embed.js) with allow-listed success callback (G6, part 1)
apps/checkout/public/embed.js opens a payment link, checkout or invoice
from the same checkout origin in an overlay iframe on any site; links
marked data-settlekit-checkout bind automatically, SettleKit.open() is
available from code, and there is an open-in-new-tab fallback for
wallets that block iframes. The success page posts settlekit:success
only to a site the seller listed under Settings > Embedding
(org embedOrigins, validated https origins), and embed.js only accepts
messages from its checkout frame. Payment requests gain sendEmail and
successUrl so store plugins can redirect buyers and bring them back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 04:52:19 +03:00

33 lines
1.3 KiB
TypeScript

/**
* Embedded checkout (embed.js overlay). The success message goes only to a
* site the seller listed in their embed origins, never to an arbitrary
* embedder: a checkout framed by an unlisted site still works, it just does
* not report back.
*/
import { getBackend, type CheckoutBackend } from "./backend";
const ORIGIN_RE = /^https:\/\/[a-z0-9.-]+(:\d{2,5})?$|^http:\/\/localhost(:\d{2,5})?$/i;
/** Whether a string is a plain web origin (no path, query or credentials). */
export function isOrigin(value: string): boolean {
return ORIGIN_RE.test(value);
}
/** The seller's allowed embed origins for a checkout session. */
export async function embedOriginsForSession(
sessionId: string,
backend: CheckoutBackend = getBackend(),
): Promise<string[]> {
const session = await backend.checkouts.findById(sessionId);
if (!session || !backend.embedOrigins) return [];
const origins = await backend.embedOrigins(session.organizationId);
return origins.filter(isOrigin).map((o) => o.toLowerCase());
}
/** The origin to notify: the embedder's, only when the seller allows it. */
export function allowedTarget(embedOrigin: string | null | undefined, allowed: readonly string[]): string | null {
if (!embedOrigin) return null;
const origin = embedOrigin.toLowerCase();
return isOrigin(origin) && allowed.includes(origin) ? origin : null;
}