import {z} from 'zod' export type CountryAlpha2 = 'AF' | 'AX' | 'AL' | 'DZ' | 'AS' | 'AD' | 'AO' | 'AI' | 'AQ' | 'AG' | 'AR' | 'AM' | 'AW' | 'AU' | 'AT' | 'AZ' | 'BS' | 'BH' | 'BD' | 'BB' | 'BY' | 'BE' | 'BZ' | 'BJ' | 'BM' | 'BT' | 'BO' | 'BQ' | 'BA' | 'BW' | 'BV' | 'BR' | 'IO' | 'BN' | 'BG' | 'BF' | 'BI' | 'KH' | 'CM' | 'CA' | 'CV' | 'KY' | 'CF' | 'TD' | 'CL' | 'CN' | 'CX' | 'CC' | 'CO' | 'KM' | 'CG' | 'CD' | 'CK' | 'CR' | 'CI' | 'HR' | 'CU' | 'CW' | 'CY' | 'CZ' | 'DK' | 'DJ' | 'DM' | 'DO' | 'EC' | 'EG' | 'SV' | 'GQ' | 'ER' | 'EE' | 'ET' | 'FK' | 'FO' | 'FJ' | 'FI' | 'FR' | 'GF' | 'PF' | 'TF' | 'GA' | 'GM' | 'GE' | 'DE' | 'GH' | 'GI' | 'GR' | 'GL' | 'GD' | 'GP' | 'GU' | 'GT' | 'GG' | 'GN' | 'GW' | 'GY' | 'HT' | 'HM' | 'VA' | 'HN' | 'HK' | 'HU' | 'IS' | 'IN' | 'ID' | 'IR' | 'IQ' | 'IE' | 'IM' | 'IL' | 'IT' | 'JM' | 'JP' | 'JE' | 'JO' | 'KZ' | 'KE' | 'KI' | 'KP' | 'KR' | 'KW' | 'KG' | 'LA' | 'LV' | 'LB' | 'LS' | 'LR' | 'LY' | 'LI' | 'LT' | 'LU' | 'MO' | 'MK' | 'MG' | 'MW' | 'MY' | 'MV' | 'ML' | 'MT' | 'MH' | 'MQ' | 'MR' | 'MU' | 'YT' | 'MX' | 'FM' | 'MD' | 'MC' | 'MN' | 'ME' | 'MS' | 'MA' | 'MZ' | 'MM' | 'NA' | 'NR' | 'NP' | 'NL' | 'NC' | 'NZ' | 'NI' | 'NE' | 'NG' | 'NU' | 'NF' | 'MP' | 'NO' | 'OM' | 'PK' | 'PW' | 'PS' | 'PA' | 'PG' | 'PY' | 'PE' | 'PH' | 'PN' | 'PL' | 'PT' | 'PR' | 'QA' | 'RE' | 'RO' | 'RU' | 'RW' | 'BL' | 'SH' | 'KN' | 'LC' | 'MF' | 'PM' | 'VC' | 'WS' | 'SM' | 'ST' | 'SA' | 'SN' | 'RS' | 'SC' | 'SL' | 'SG' | 'SX' | 'SK' | 'SI' | 'SB' | 'SO' | 'ZA' | 'GS' | 'SS' | 'ES' | 'LK' | 'SD' | 'SR' | 'SJ' | 'SZ' | 'SE' | 'CH' | 'SY' | 'TW' | 'TJ' | 'TZ' | 'TH' | 'TL' | 'TG' | 'TK' | 'TO' | 'TT' | 'TN' | 'TR' | 'TM' | 'TC' | 'TV' | 'UG' | 'UA' | 'AE' | 'GB' | 'UM' | 'UY' | 'UZ' | 'VU' | 'VE' | 'VN' | 'VG' | 'VI' | 'WF' | 'EH' | 'YE' | 'ZM' | 'ZW' | 'US'; export type Currency = 'AED' | 'AFN' | 'ALL' | 'AMD' | 'ANG' | 'AOA' | 'ARS' | 'AUD' | 'AWG' | 'AZN' | 'BAM' | 'BBD' | 'BDT' | 'BGN' | 'BHD' | 'BIF' | 'BMD' | 'BND' | 'BOB' | 'BRL' | 'BSD' | 'BTN' | 'BWP' | 'BYN' | 'BZD' | 'CAD' | 'CDF' | 'CHF' | 'CLF' | 'CLP' | 'CNY' | 'COP' | 'CRC' | 'CUC' | 'CUP' | 'CVE' | 'CZK' | 'DJF' | 'DKK' | 'DOP' | 'DZD' | 'EGP' | 'ERN' | 'ETB' | 'EUR' | 'FJD' | 'FKP' | 'GBP' | 'GEL' | 'GHS' | 'GIP' | 'GMD' | 'GNF' | 'GTQ' | 'GYD' | 'HKD' | 'HNL' | 'HRK' | 'HTG' | 'HUF' | 'IDR' | 'ILS' | 'INR' | 'IQD' | 'IRR' | 'ISK' | 'JMD' | 'JOD' | 'JPY' | 'KES' | 'KGS' | 'KHR' | 'KMF' | 'KPW' | 'KRW' | 'KWD' | 'KYD' | 'KZT' | 'LAK' | 'LBP' | 'LKR' | 'LRD' | 'LSL' | 'LYD' | 'MAD' | 'MDL' | 'MGA' | 'MKD' | 'MMK' | 'MNT' | 'MOP' | 'MRU' | 'MUR' | 'MVR' | 'MWK' | 'MXN' | 'MYR' | 'MZN' | 'NAD' | 'NGN' | 'NIO' | 'NOK' | 'NPR' | 'NZD' | 'OMR' | 'PAB' | 'PEN' | 'PGK' | 'PHP' | 'PKR' | 'PLN' | 'PYG' | 'QAR' | 'RON' | 'RSD' | 'RUB' | 'RWF' | 'SAR' | 'SBD' | 'SCR' | 'SDG' | 'SEK' | 'SGD' | 'SHP' | 'SLE' | 'SLL' | 'SOS' | 'SRD' | 'SSP' | 'STD' | 'STN' | 'SVC' | 'SYP' | 'SZL' | 'THB' | 'TJS' | 'TMT' | 'TND' | 'TOP' | 'TRY' | 'TTD' | 'TWD' | 'TZS' | 'UAH' | 'UGX' | 'USD' | 'UYU' | 'UZS' | 'VES' | 'VND' | 'VUV' | 'WST' | 'XAF' | 'XCD' | 'XOF' | 'XPF' | 'YER' | 'ZAR' | 'ZMW' | 'ZWL'; export type CardNetwork = 'Visa' | 'Mastercard' | 'AmericanExpress' | 'JCB' | 'DinersClub' | 'Discover' | 'CartesBancaires' | 'UnionPay' | 'Interac' | 'RuPay' | 'Maestro' | 'Star' | 'Pulse' | 'Accel' | 'Nyce' | 'Prop' | 'PrivateLabel' | 'Dinacard'; export type GetApiCustomersPaymentMethodsArgs = {client_secret?: (string | null) | undefined, accepted_countries?: (Array | null) | undefined, accepted_currencies?: (Array | null) | undefined, amount?: (number | null) | undefined, recurring_enabled?: (boolean | null) | undefined, installment_payment_enabled?: (boolean | null) | undefined, limit?: (number | null) | undefined, card_networks?: (Array | null) | undefined}; export type GetApiCustomersPaymentMethodsOptions = {baseUrl: string, publishableKey?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentMethod = z.enum(["card", "card_redirect", "pay_later", "wallet", "bank_redirect", "bank_transfer", "crypto", "bank_debit", "reward", "real_time_payment", "upi", "voucher", "gift_card", "open_banking", "mobile_payment", "network_token"]); export const paymentMethodType = z.enum(["ach", "affirm", "afterpay_clearpay", "alfamart", "ali_pay", "ali_pay_hk", "alma", "amazon_pay", "paysera", "apple_pay", "atome", "bacs", "bancontact_card", "becs", "benefit", "bizum", "blik", "bluecode", "boleto", "bca_bank_transfer", "bni_va", "breadpay", "bri_va", "bhn_card_network", "card_redirect", "cimb_va", "classic", "credit", "crypto_currency", "cashapp", "dana", "danamon_va", "debit", "duit_now", "efecty", "eft", "eft_debit_order", "eps", "flexiti", "fps", "evoucher", "giropay", "givex", "google_pay", "go_pay", "gcash", "ideal", "interac", "indomaret", "klarna", "kakao_pay", "local_bank_redirect", "mandiri_va", "knet", "mb_way", "mobile_pay", "momo", "momo_atm", "multibanco", "online_banking_thailand", "online_banking_czech_republic", "online_banking_finland", "online_banking_fpx", "online_banking_poland", "online_banking_slovakia", "oxxo", "pago_efectivo", "permata_bank_transfer", "open_banking_uk", "pay_bright", "payjustnow", "paypal", "paze", "pix", "pix_key", "pix_emv", "pix_qr", "pix_automatico_qr", "pix_automatico_push", "pay_safe_card", "przelewy24", "prompt_pay", "pse", "qris", "red_compra", "red_pagos", "samsung_pay", "sepa", "sepa_bank_transfer", "sepa_guarenteed_debit", "skrill", "sofort", "swish", "touch_n_go", "trustly", "twint", "upi_collect", "upi_intent", "upi_qr", "vipps", "viet_qr", "venmo", "walley", "we_chat_pay", "seven_eleven", "lawson", "mini_stop", "family_mart", "seicomart", "pay_easy", "local_bank_transfer", "mifinity", "open_banking_pis", "direct_carrier_billing", "instant_bank_transfer", "instant_bank_transfer_finland", "instant_bank_transfer_poland", "revolut_pay", "indonesian_bank_transfer", "open_banking", "network_token"]); export const countryAlpha2 = z.enum(["AF", "AX", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CD", "CK", "CR", "CI", "HR", "CU", "CW", "CY", "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM", "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", "KP", "KR", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RE", "RO", "RU", "RW", "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", "SC", "SL", "SG", "SX", "SK", "SI", "SB", "SO", "ZA", "GS", "SS", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW", "US"]); export const cardNetwork = z.enum(["Visa", "Mastercard", "AmericanExpress", "JCB", "DinersClub", "Discover", "CartesBancaires", "UnionPay", "Interac", "RuPay", "Maestro", "Star", "Pulse", "Accel", "Nyce", "Prop", "PrivateLabel", "Dinacard"]); export const cardDetailFromLocker = z.object({issuer_country: countryAlpha2.nullable().optional(), last4_digits: z.string().nullable().optional(), expiry_month: z.string().nullable().optional(), expiry_year: z.string().nullable().optional(), card_holder_name: z.string().nullable().optional(), card_fingerprint: z.string().nullable().optional(), nick_name: z.string().nullable().optional(), card_network: cardNetwork.nullable().optional(), card_isin: z.string().nullable().optional(), card_issuer: z.string().nullable().optional(), card_type: z.string().nullable().optional(), saved_to_locker: z.boolean()}); export const bankDebitDetailsPaymentMethod = z.object({ach_bank_debit: z.object({account_number_last4_digits: z.string(), routing_number_last4_digits: z.string(), bank_account_holder_name: z.string().nullable().optional(), bank_name: z.string(), bank_type: z.string(), bank_holder_type: z.string()})}); export const achBankTransfer = z.object({bank_name: z.string().nullable().optional(), bank_country_code: countryAlpha2.nullable().optional(), bank_city: z.string().nullable().optional(), bank_account_number: z.string(), bank_routing_number: z.string(), account_holder_name: z.string().nullable().optional()}); export const bacsBankTransfer = z.object({bank_name: z.string().nullable().optional(), bank_country_code: countryAlpha2.nullable().optional(), bank_city: z.string().nullable().optional(), bank_account_number: z.string(), bank_sort_code: z.string(), account_holder_name: z.string().nullable().optional()}); export const trustlyBankTransfer = z.object({iban: z.string(), country_code: countryAlpha2, account_number: z.string(), bank_number: z.string()}); export const sepaBankTransfer = z.object({bank_name: z.string().nullable().optional(), bank_country_code: countryAlpha2.nullable().optional(), bank_city: z.string().nullable().optional(), iban: z.string(), bic: z.string(), account_holder_name: z.string().nullable().optional()}); export const pixBankTransfer = z.object({bank_name: z.string().nullable().optional(), bank_branch: z.string().nullable().optional(), bank_account_number: z.string(), pix_key: z.string(), tax_id: z.string().nullable().optional(), emv: z.string(), ispb: z.string().nullable().optional()}); export const openBanking = z.object({account_holder_name: z.string(), iban: z.string()}); export const bank = z.union([achBankTransfer, bacsBankTransfer, trustlyBankTransfer, sepaBankTransfer, pixBankTransfer, openBanking]); export const paymentMethodDataWalletInfo = z.object({last4: z.string().nullable().optional(), card_network: z.string().nullable().optional(), type: z.string().nullable().optional(), card_exp_month: z.string().nullable().optional(), card_exp_year: z.string().nullable().optional(), auth_code: z.string().nullable().optional(), email: z.string().nullable().optional()}); export const paypalRedirection = z.object({email: z.string().max(255).nullable().optional()}); export const walletPaymentMethodData = z.union([z.object({apple_pay: paymentMethodDataWalletInfo}), z.object({google_pay: paymentMethodDataWalletInfo}), z.object({pay_pal: paypalRedirection})]); export const paymentMethodListData = z.union([z.object({card: cardDetailFromLocker}), z.object({bank_debit: bankDebitDetailsPaymentMethod}), z.object({bank: bank}), z.object({wallet: walletPaymentMethodData})]); export const addressDetails = z.object({city: z.string().max(50).nullable().optional(), country: countryAlpha2.nullable().optional(), line1: z.string().max(200).nullable().optional(), line2: z.string().max(50).nullable().optional(), line3: z.string().max(50).nullable().optional(), zip: z.string().max(50).nullable().optional(), state: z.string().nullable().optional(), first_name: z.string().max(255).nullable().optional(), last_name: z.string().max(255).nullable().optional(), origin_zip: z.string().max(50).nullable().optional()}); export const phoneDetails = z.object({number: z.string().nullable().optional(), country_code: z.string().nullable().optional()}); export const address = z.object({address: addressDetails.nullable().optional(), phone: phoneDetails.nullable().optional(), email: z.string().nullable().optional()}); export const networkTokenDetailsPaymentMethod = z.object({last4_digits: z.string().nullable().optional(), issuer_country: countryAlpha2.nullable().optional(), network_token_expiry_month: z.string().nullable().optional(), network_token_expiry_year: z.string().nullable().optional(), nick_name: z.string().nullable().optional(), card_holder_name: z.string().nullable().optional(), card_isin: z.string().nullable().optional(), card_issuer: z.string().nullable().optional(), card_network: cardNetwork.nullable().optional(), card_type: z.string().nullable().optional(), saved_to_locker: z.boolean().optional(), par: z.string().nullable().optional()}); export const networkTokenResponse = z.object({payment_method_data: networkTokenDetailsPaymentMethod}); export const connectorTokenDetails = z.object({token: z.string(), connector_token_request_reference_id: z.string().nullable().optional()}); export const paymentMethodResponseItem = z.object({id: z.string(), customer_id: z.string().min(32).max(64), payment_method_type: paymentMethod, payment_method_subtype: paymentMethodType, recurring_enabled: z.boolean().nullable().optional(), payment_method_data: paymentMethodListData.nullable().optional(), created: z.string(), requires_cvv: z.boolean(), last_used_at: z.string().optional(), is_default: z.boolean(), billing: address.nullable().optional(), network_tokenization: networkTokenResponse.nullable().optional(), connector_tokens: z.array(connectorTokenDetails).nullable().optional(), network_transaction_id: z.string().nullable().optional()}); export const customerPaymentMethodsListResponse = z.object({customer_payment_methods: z.array(paymentMethodResponseItem)}); 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 }; /** * List customer saved payment methods for a Payment * * GET /customers/payment_methods */ export const getApiCustomersPaymentMethods = async (args: GetApiCustomersPaymentMethodsArgs, options: GetApiCustomersPaymentMethodsOptions) => { const {baseUrl, fetch: fetchFn = fetch, publishableKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.client_secret != null) query.append("client_secret", String(args.client_secret)) for (const item of args.accepted_countries ?? []) query.append("accepted_countries", String(item)) for (const item of args.accepted_currencies ?? []) query.append("accepted_currencies", String(item)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.recurring_enabled != null) query.append("recurring_enabled", String(args.recurring_enabled)) if (args.installment_payment_enabled != null) query.append("installment_payment_enabled", String(args.installment_payment_enabled)) if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.card_networks ?? []) query.append("card_networks", String(item)) if (publishableKey !== undefined) headers.set("api-key", publishableKey) const url = `${baseUrl.replace(/\/+$/, '')}/customers/payment_methods${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(customerPaymentMethodsListResponse, await res.json()) };