UNPKG

dodopayments-checkout

Version:

A TypeScript library for embedding Dodo Payments checkout (overlay and inline).

162 lines (161 loc) 5.9 kB
/** * SDK Type Definitions */ export type CheckoutMode = "test" | "live"; export type DisplayType = "overlay" | "inline"; /** @deprecated static and dynamic link types are deprecated and will be removed in v2.0.0. Use 'session' instead */ export type LinkType = "static" | "dynamic" | "session"; /** @deprecated No longer used, pass theme in checkout session */ export type ThemeType = "light" | "dark"; export type EventType = "checkout.opened" | "checkout.payment_page_opened" | "checkout.closed" | "checkout.error" | "checkout.customer_details_submitted" | "checkout.redirect" | "checkout.resize" | "checkout.breakdown" | "checkout.link_expired" | "checkout.status" | "checkout.redirect_requested" | "checkout.pay_button_clicked" | "checkout.keys_provided" | "checkout.form_ready"; export interface CheckoutEvent { event_type: EventType; data?: Record<string, unknown>; } export type CheckoutEventCallback = (event: CheckoutEvent) => void; export type EventCallback = (data: Record<string, unknown>) => void; export type EventMap = Record<string, EventCallback[]>; export interface CheckoutConfig { mode: CheckoutMode; onEvent?: CheckoutEventCallback; /** @deprecated static and dynamic link types are deprecated and will be removed in v2.0.0. Use 'session' instead. Defaults to 'session' */ linkType?: LinkType; /** @deprecated No longer used, pass theme in checkout session */ theme?: ThemeType; displayType?: DisplayType; } export interface ProductItem { productId: string; quantity: number; } export interface ThemeModeConfig { bgPrimary?: string; bgSecondary?: string; borderPrimary?: string; borderSecondary?: string; textPrimary?: string; textSecondary?: string; textPlaceholder?: string; textError?: string; textSuccess?: string; buttonPrimary?: string; buttonPrimaryHover?: string; buttonTextPrimary?: string; buttonSecondary?: string; buttonSecondaryHover?: string; buttonTextSecondary?: string; inputFocusBorder?: string; } export interface ThemeConfig { light?: ThemeModeConfig; dark?: ThemeModeConfig; radius?: string; } export type FontSize = "xs" | "sm" | "md" | "lg" | "xl" | "2xl"; export type FontWeight = "normal" | "medium" | "bold" | "extraBold"; export interface CheckoutOptions { showTimer?: boolean; showSecurityBadge?: boolean; manualRedirect?: boolean; themeConfig?: ThemeConfig; payButtonText?: string; fontSize?: FontSize; fontWeight?: FontWeight; } export interface OpenCheckoutOptions { /** @deprecated Use checkoutUrl instead. Will be removed in v2.0.0 */ products?: ProductItem[]; /** @deprecated Use checkoutUrl instead. Will be removed in v2.0.0 */ queryParams?: Record<string, string>; /** @deprecated Use checkoutUrl instead. Will be removed in v2.0.0 */ paymentLink?: string; /** @deprecated Use checkoutUrl instead. Will be removed in v2.0.0 */ redirectUrl?: string; checkoutUrl?: string; elementId?: string; options?: CheckoutOptions; } export type DodoWalletDisplayMode = "auto" | "never"; export interface DodoWalletsConfig { applePay?: DodoWalletDisplayMode; googlePay?: DodoWalletDisplayMode; payPal?: DodoWalletDisplayMode; } /** * Subset of the SDK's PaymentMethodSubType that this library uses for * `allowedPaymentMethods`. The SDK accepts the full union; we only need the * wallet/card identifiers here. */ export type DodoPaymentMethodSubType = "apple_pay" | "google_pay" | "credit" | "debit"; export interface DodoElementsOptions { clientSecret: string; appearance?: { theme?: string; variables?: Record<string, string>; }; locale?: string; walletReturnUrl?: string; wallets?: DodoWalletsConfig; /** Restricts which payment methods render. Omitted/empty shows all; when set, * only the listed methods appear. Saved methods stay governed by * displaySavedPaymentMethods. */ allowedPaymentMethods?: DodoPaymentMethodSubType[]; /** When false, the SDK does not show saved customer payment methods. */ displaySavedPaymentMethods?: boolean; manualRedirect?: boolean; } export type DodoElementType = "payment" | "card" | "wallet"; export interface DodoPaymentElement { mount: (selector: string) => void; unmount: () => void; destroy: () => void; on: (event: string, handler: (data: unknown) => void) => void; } export interface DodoElements { create: (type: DodoElementType, options?: { layout?: "tabs" | "accordion"; }) => DodoPaymentElement; update: (options: Partial<DodoElementsOptions>) => void; } export interface DodoInstance { elements: (options: DodoElementsOptions) => DodoElements; } export interface DodoLoadOptions { apiUrl?: string; baseUrl?: string; locale?: string; } export interface DodoCheckoutWebSDK { loadDodoPayments: (publishableKey: string, options?: DodoLoadOptions) => DodoInstance; } export interface CheckoutState { iframe: HTMLIFrameElement | null; container: HTMLDivElement | null; config: CheckoutConfig; currentProducts: ProductItem[]; baseUrl: string; allowedOrigin: string; iframeId: string; eventListeners: EventMap; dodoInstance: DodoInstance | null; walletElement: DodoPaymentElement | null; walletElementContainer: HTMLDivElement | null; dodoScriptPromise: Promise<void> | null; walletResizeObserver: ResizeObserver | null; lastCheckoutIframeHeight: number; wrapperDiv: HTMLDivElement | null; iframeResizerInstance: Array<{ iFrameResizer: { close: () => void; }; }> | null; } export interface CheckoutBreakdownData { subTotal?: number; discount?: number; total?: number; tax?: number; currency?: string; finalTotal?: number; finalTotalCurrency?: string; }