///
import type { components } from "@amos.com/node";
import { appearanceWithDefaults } from "./appearance-defaults";
import { embedIframeSearchParams } from "./embed-src";
import { armIframeHandshakeReload } from "./handshake-reload";
import { getEmbedOrigin } from "./jwt";
import {
confirmPayment,
sendParentReadyMessage,
updateAmount as sendUpdateAmount,
updateAppearance as sendUpdateAppearance,
updateGooglePayButton as sendUpdateGooglePayButton,
updateMerchantName as sendUpdateMerchantName,
} from "./messaging";
import type {
ConfirmPaymentResult,
GooglePayButtonElementProps,
Message,
WalletContactRequirements,
WalletCustomerCreateAttributes,
} from "./types";
/**
* Build the iframe `src` URL for the embedded Google Pay button.
*/
export function getGooglePayButtonSrc(renderToken: string): string {
const params = embedIframeSearchParams({ token: renderToken });
return `${getEmbedOrigin(renderToken)}/iframe/google-pay?${params}`;
}
/**
* Default iframe pixel height for the Google Pay button.
*/
export function getGooglePayButtonInitialHeight(): string {
return "48px";
}
/**
* Options accepted by {@link attachGooglePayButtonListeners}.
*/
export type GooglePayButtonListenerOptions = WalletContactRequirements & {
/**
* Major-currency decimal string shown in the Google Pay sheet
* (e.g. `"50.00"` for $50.00). Converted to cents in
* `paymentIntentCreateAttributes.amount`.
*/
amount: string;
/** A user-visible merchant name. */
merchantName: string;
/**
* Painted Google Pay button height. CSS length (e.g. `"48px"`).
* @default "48px"
*/
height?: string;
/**
* Native Google Pay button attributes and inner style. Omitted fields
* keep Amos paint defaults (`buttonType: "plain"`,
* `buttonSizeMode: "fill"`). The button fills the iframe — size the
* mount slot, not the button. Compact: `buttonSizeMode: "static"` and
* `style.width`.
*/
buttonProps?: GooglePayButtonElementProps;
/**
* Called whenever the iframe asks the host page to resize it. Update
* the iframe's `height` style here.
*/
onHeightChange?: (height: string) => void;
/**
* Called once the iframe has applied the requested appearance and is
* ready to be revealed.
*/
onAppearanceReady?: () => void;
/**
* Called when the iframe posts `IFRAME_READY` (embed JS is running).
*/
onIframeReady?: () => void;
/**
* Called when the buyer authorizes in the Google Pay sheet. Create a
* payment intent on your server, then `await confirmPayment(token)`.
* `customerCreateAttributes` is {@link WalletCustomerCreateAttributes},
* not Amos `CreateCustomerInput`.
*/
onConfirm: ({
paymentIntentCreateAttributes,
customerCreateAttributes,
confirmPayment,
}: {
paymentIntentCreateAttributes: components["schemas"]["CreatePaymentIntentInput"];
customerCreateAttributes: WalletCustomerCreateAttributes;
confirmPayment: (token: string) => Promise;
}) => Promise;
};
/**
* Controller returned by {@link attachGooglePayButtonListeners} and
* {@link mountAmosGooglePayButton}.
*/
export type GooglePayButtonController = {
/**
* Update one or more listener options without re-attaching the
* message listener. Pass `amount` or `merchantName` to push the new
* value into the iframe; pass `height`, `buttonProps`,
* `phoneRequired`, or `shippingAddressRequired` to restyle the Google
* Pay button or change sheet contact fields.
*/
update: (patch: Partial) => void;
/**
* Detach the iframe message listener.
*/
destroy: () => void;
};
/**
* Wire up the host-page side of the Google Pay iframe message protocol
* on an existing `