import React from 'react'; import type { VaultData } from '../types'; import type { IntentDetails, IntentStateDetails, InstallmentPlanData, LookupData, } from '../../models'; export type UseSecureCardFormRefMethods = { /** * @deprecated Use `collect` without arguments via PublicApiKey instead, * using accessToken will be removed in next major version */ collect(options: { intentId?: string; accessToken: string; shouldSaveCard?: boolean; }): Promise; collect(): Promise; pay: (options: { intentId: string; saveCard?: boolean; cardData: VaultData; shippingData?: Record; billingData?: Record; installmentPlanData?: InstallmentPlanData; }) => Promise; createCardToken: (options: { cardIntentId: string; cardData: VaultData; }) => Promise; binLookup(): Promise; binLookup(cardData: VaultData): Promise; }; type UseSecureCardFormReturn = UseSecureCardFormRefMethods & { cardFormRef: React.RefObject; isValid: boolean; onFormValidityChange: (isValid: boolean) => void; }; export function useSecureCardForm(): UseSecureCardFormReturn { const [isValid, setIsValid] = React.useState(false); const cardFormRef = React.useRef(null); return { cardFormRef, isValid, collect: React.useCallback((...args: []) => { if (!cardFormRef.current) { throw new Error('cardFormRef must be registered to '); } return cardFormRef.current.collect(...args); }, []), pay: React.useCallback((...args) => { if (!cardFormRef.current) { throw new Error('cardFormRef must be registered to '); } return cardFormRef.current.pay(...args); }, []), createCardToken: React.useCallback((...args) => { if (!cardFormRef.current) { throw new Error('cardFormRef must be registered to '); } return cardFormRef.current.createCardToken(...args); }, []), binLookup: React.useCallback((cardData?: VaultData) => { if (!cardFormRef.current) { throw new Error('cardFormRef must be registered to '); } if (cardData) { return cardFormRef.current.binLookup(cardData); } else { return cardFormRef.current.binLookup(); } }, []) as UseSecureCardFormRefMethods['binLookup'], onFormValidityChange: setIsValid, }; }