import {z} from 'zod' export type CardTokenAgenticData = {max_amount: unknown, currency: 'AED' | 'AFN' | 'ALL' | 'AMD' | 'ARS' | 'AUD' | 'AZN' | 'BAM' | 'BDT' | 'BGN' | 'BHD' | 'BIF' | 'BND' | 'BOB' | 'BRL' | 'BWP' | 'BYN' | 'BZD' | 'CAD' | 'CDF' | 'CHF' | 'CLP' | 'CNY' | 'COP' | 'CRC' | 'CVE' | 'CZK' | 'DJF' | 'DKK' | 'DOP' | 'DZD' | 'EEK' | 'EGP' | 'ERN' | 'ETB' | 'EUR' | 'GBP' | 'GEL' | 'GHS' | 'GNF' | 'GTQ' | 'HKD' | 'HNL' | 'HRK' | 'HUF' | 'IDR' | 'ILS' | 'INR' | 'IQD' | 'IRR' | 'ISK' | 'JMD' | 'JOD' | 'JPY' | 'KES' | 'KHR' | 'KMF' | 'KRW' | 'KWD' | 'KZT' | 'LBP' | 'LKR' | 'LTL' | 'LVL' | 'LYD' | 'MAD' | 'MDL' | 'MGA' | 'MKD' | 'MMK' | 'MOP' | 'MRU' | 'MUR' | 'MXN' | 'MYR' | 'MZN' | 'NAD' | 'NGN' | 'NIO' | 'NOK' | 'NPR' | 'NZD' | 'OMR' | 'PAB' | 'PEN' | 'PHP' | 'PKR' | 'PLN' | 'PYG' | 'QAR' | 'RON' | 'RSD' | 'RUB' | 'RWF' | 'SAR' | 'SDG' | 'SEK' | 'SGD' | 'SOS' | 'SYP' | 'THB' | 'TND' | 'TOP' | 'TRY' | 'TTD' | 'TWD' | 'TZS' | 'UAH' | 'UGX' | 'USD' | 'UYU' | 'UZS' | 'VEF' | 'VND' | 'XAF' | 'XOF' | 'YER' | 'ZAR' | 'ZMK' | 'ZWL', max_usage_count?: number | undefined, valid_from: string, expires_at: string, agent_id: string}; export type CreateCardToken = {id: string, customer: string, webhook_url: string, payment_provider?: string | undefined, card_token_type?: ('UNIVERSAL' | 'PROVIDER') | undefined, successful_redirect_url?: string | undefined, failed_redirect_url?: string | undefined, custom_webhook_headers: unknown, vault_region?: ('sa' | 'default' | '') | undefined, network_tokenize?: boolean | undefined, agentic_data?: CardTokenAgenticData | undefined, created: string, modified: string, redirect_strategy?: ('redirect_inside_embed' | 'redirect_entire_window' | 'redirect_to_new_tab' | '' | 'null' | null) | undefined, hide_header?: boolean | undefined, form_only?: boolean | undefined, status?: string | undefined, for_sandbox: boolean, custom_fields: unknown, merchant_reference?: (string | null) | undefined}; export type CreateApiApiV14TokensCardsArgs = {body: CreateCardToken}; export type CreateApiApiV14TokensCardsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cardTokenIntent = z.object({id: z.string(), hide_header: z.boolean().optional(), standalone_tokenization: z.boolean(), form_only: z.boolean().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create card token * * POST /api/v1.4/tokens/cards/ * * Also accepts auth: AccessTokenAuth */ export const createApiApiV14TokensCards = async (args: CreateApiApiV14TokensCardsArgs, options?: CreateApiApiV14TokensCardsOptions) => { const {baseUrl = "https://web.moneyhash.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1.4/tokens/cards/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(cardTokenIntent, await res.json()) };