import type { PaymentClientSdk } from 'brainerce'; /** * Render-mode negotiation: predict which mode a payment intent will come back * with, so the checkout can pass the matching return URL BEFORE the intent * exists. An iframe provider returns the shopper inside the frame, to a * same-origin transition page (`/payment-complete`) that posts to the parent; * a redirect provider returns them straight to `/order-confirmation`. * * This is the platform's own rule, verbatim: * 1. no preference -> the provider's default (`renderType`) * 2. preference listed in displayModes -> the preference * 3. preference not listed -> the provider's default * 4. displayModes absent (legacy) -> treated as `[renderType]` * * The SDK exports the same function as `resolveRenderType` from 'brainerce'. * This local copy exists because scaffolded stores pin an exact SDK version * that must be at least seven days old on npm (see cli-shared/versions.ts), * and the version carrying the export is younger than that. Replace this * file's body with `export { resolveRenderType } from 'brainerce'` once the * pin reaches it. */ export type PreferredRenderType = 'redirect' | 'iframe'; /** * The mode this storefront asks the platform for, or `undefined` to take each * provider's default. Set to `'iframe'` only if the payment step has a frame * to render into AND the provider's iframe has been verified on a real * checkout; set to `'redirect'` to keep every provider on a full-page * redirect. The platform only honours a mode the provider declares, and the * checkout always branches on the `renderType` that comes BACK, so a wrong * preference degrades to the provider default rather than breaking payment. */ export const PREFERRED_RENDER_MODE: PreferredRenderType | undefined = undefined; type ProviderSdk = Pick & { /** Every mode the provider can serve. Sent by the platform; older SDK types omit it. */ displayModes?: PaymentClientSdk['renderType'][]; }; export function resolveRenderType( sdk: ProviderSdk | null | undefined, preferred?: PreferredRenderType ): PaymentClientSdk['renderType'] { if (!sdk) return 'redirect'; const declared = sdk.displayModes?.length ? sdk.displayModes : [sdk.renderType]; return preferred && declared.includes(preferred) ? preferred : sdk.renderType; }