UNPKG

@payos-inc/payos-js

Version:

PayOS JavaScript SDK for browser-based checkout and wallet onboarding

219 lines (213 loc) 5.57 kB
interface CheckoutOptions { token: string; mode?: 'popup' | 'redirect'; environment?: 'sandbox' | 'production'; returnUrl?: string; baseUrl?: string; customParams?: Record<string, string>; onReady?: () => void; onComplete?: (result: any) => void; onError?: (error: Error) => void; onCancel?: () => void; } interface PayOSConfig { baseUrl?: string; defaultParams?: Record<string, string>; } declare class CheckoutClient { private defaultUrl; private defaultParams?; private popup; private messageHandler; private currentState; private popupCheckInterval; constructor(config?: PayOSConfig); /** * Open PayOS Checkout with a token */ open(options: CheckoutOptions): void; /** * Close checkout */ close(): void; private openRedirect; private openPopup; private setupMessageListener; } /** * WalletOnboardClient - Instance-based client for PayOS Wallet Onboard * Provides a consistent API similar to CheckoutClient */ interface WalletOnboardOptions { /** * Authentication token from your backend */ token: string; /** * Display mode: 'popup' or 'redirect' */ mode?: 'popup' | 'redirect'; /** * Custom base URL for hosted UI */ baseUrl?: string; /** * Custom query params for testing */ customParams?: Record<string, string>; /** * Environment: 'sandbox' or 'production' */ environment?: 'sandbox' | 'production'; /** * Return URL for redirect mode */ returnUrl?: string; /** * Merchant name to display */ merchantName?: string; /** * Wallet user ID if already known */ walletUserId?: string; /** * Callback when onboarding is complete */ onComplete?: (data: { walletUserId?: string; linkedCardIds?: string[]; }) => void; /** * Callback when an error occurs */ onError?: (error: Error) => void; /** * Callback when user cancels */ onCancel?: () => void; /** * Callback when ready (iframe/popup loaded) */ onReady?: () => void; } interface WalletOnboardInstance { close: () => void; } declare class WalletOnboardClient { private defaultUrl; private defaultParams?; private popup; constructor(config?: PayOSConfig); /** * Open wallet onboard with specified options * Supports popup and redirect modes */ open(options: WalletOnboardOptions): WalletOnboardInstance; /** * Open wallet onboard in popup mode */ private openPopup; /** * Open wallet onboard in redirect mode */ private openRedirect; /** * Close the current wallet onboard instance */ private close; } /** * PayOS.js - Client-side JavaScript SDK for PayOS * Combines checkout and wallet onboarding functionality */ interface SimpleWalletOptions { token: string; mode?: "popup" | "redirect"; environment?: "sandbox" | "production"; merchantName?: string; returnUrl?: string; onSuccess?: (walletUserId?: string) => void; onClose?: () => void; onError?: (error: Error) => void; } interface PayOSWalletOnboardInstance { close: () => void; } /** * Main PayOS.js class * Provides checkout and wallet onboarding functionality */ declare class PayOS { private _checkout; private _walletOnboard; constructor(config?: PayOSConfig); /** * Checkout module for payment authentication * Opens hosted checkout UI with a token from your backend * * @example * ```javascript * // Get token from your backend * const { token } = await fetch('/api/create-checkout-token', {...}); * * // Open checkout * payos.checkout.open({ * token: token, * mode: 'iframe', * onComplete: (result) => console.log('Payment complete', result) * }); * ``` */ get checkout(): CheckoutClient; /** * Wallet Onboard module for adding payment methods * Opens hosted wallet onboard UI with a token from your backend * * @example * ```javascript * // Get token from your backend * const { token } = await fetch('/api/create-onboard-token', {...}); * * // Open wallet onboard * payos.walletOnboard.open({ * token: token, * mode: 'iframe', * onComplete: (result) => console.log('Card added', result) * }); * ``` */ get walletOnboard(): WalletOnboardClient; /** * Initialize PayOS Wallet Onboard * * @example * ```javascript * // Simple: init with just token * payos.walletOnboard.init('token'); * * // With options * payos.walletOnboard.init({ token: 'token', mode: 'popup' }); * ``` */ static walletOnboard: { init: typeof initWalletOnboard; }; /** * Static method to check if PayOS.js is loaded */ static isLoaded(): boolean; /** * Version of the SDK */ static get version(): string; } /** * Initialize PayOS Wallet Onboard using popup or redirect mode * Supports multiple usage patterns: * * 1. Simple: init('token') * 2. With options: init({ token: 'token', ...options }) */ declare function initWalletOnboard(tokenOrOptions: string | SimpleWalletOptions): PayOSWalletOnboardInstance; export { CheckoutClient, CheckoutOptions, PayOS, PayOSConfig, PayOSWalletOnboardInstance, WalletOnboardClient, WalletOnboardOptions, PayOS as default };