import {z} from 'zod' export type MinorUnit = number; export type AuthenticationConnectors = 'threedsecureio' | 'netcetera' | 'gpayments' | 'ctp_mastercard' | 'unified_authentication_service' | 'juspaythreedsserver' | 'ctp_visa' | 'cardinal'; 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 ScaExemptionType = 'low_value' | 'transaction_risk_analysis'; export type AcquirerDetails = {acquirer_bin?: (string | null) | undefined, acquirer_merchant_id?: (string | null) | undefined, merchant_country_code?: (string | null) | undefined}; export type DocumentKind = 'cpf' | 'cnpj' | 'psn' | 'other'; export type CustomerDocumentDetails = {document_type: DocumentKind, document_number: string}; export type CustomerDetails = {id?: (string | null) | undefined, name?: (string | null) | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined, phone_country_code?: (string | null) | undefined, tax_registration_id?: (string | null) | undefined, document_details?: (CustomerDocumentDetails | null) | undefined}; export type AuthenticationCreateRequest = {authentication_id?: (string | null) | undefined, profile_id?: (string | null) | undefined, amount: MinorUnit, authentication_connector?: (AuthenticationConnectors | null) | undefined, currency: Currency, return_url?: (string | null) | undefined, force_3ds_challenge?: (boolean | null) | undefined, psd2_sca_exemption_type?: (ScaExemptionType | null) | undefined, profile_acquirer_id?: (string | null) | undefined, acquirer_details?: (AcquirerDetails | null) | undefined, customer_details?: (CustomerDetails | null) | undefined}; export type CreateApiAuthenticationArgs = {body: AuthenticationCreateRequest}; export type CreateApiAuthenticationOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const authenticationStatus = z.enum(["started", "pending", "success", "failed"]); export const minorUnit = z.number().int(); export const currency = z.enum(["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 const authenticationConnectors = z.enum(["threedsecureio", "netcetera", "gpayments", "ctp_mastercard", "unified_authentication_service", "juspaythreedsserver", "ctp_visa", "cardinal"]); export const scaExemptionType = z.enum(["low_value", "transaction_risk_analysis"]); export const acquirerDetails = z.object({acquirer_bin: z.string().nullable().optional(), acquirer_merchant_id: z.string().nullable().optional(), merchant_country_code: z.string().nullable().optional()}); export const documentKind = z.enum(["cpf", "cnpj", "psn", "other"]); export const customerDocumentDetails = z.object({document_type: documentKind, document_number: z.string().max(255)}); export const customerDetails = z.object({id: z.string().min(1).max(64).nullable().optional(), name: z.string().max(255).nullable().optional(), email: z.string().max(255).nullable().optional(), phone: z.string().max(10).nullable().optional(), phone_country_code: z.string().max(2).nullable().optional(), tax_registration_id: z.string().max(255).nullable().optional(), document_details: customerDocumentDetails.nullable().optional()}); export const authenticationResponse = z.object({authentication_id: z.string(), merchant_id: z.string(), status: authenticationStatus, client_secret: z.string().nullable().optional(), amount: minorUnit, currency: currency, authentication_connector: authenticationConnectors.nullable().optional(), force_3ds_challenge: z.boolean().nullable().optional(), return_url: z.string().nullable().optional(), created_at: z.string().nullable().optional(), error_code: z.string().nullable().optional(), error_message: z.string().nullable().optional(), profile_id: z.string().nullable().optional(), psd2_sca_exemption_type: scaExemptionType.nullable().optional(), acquirer_details: acquirerDetails.nullable().optional(), profile_acquirer_id: z.string().nullable().optional(), customer_details: customerDetails.nullable().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 }; /** * Authentication - Create * * POST /authentication */ export const createApiAuthentication = async (args: CreateApiAuthenticationArgs, options: CreateApiAuthenticationOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/authentication${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(authenticationResponse, await res.json()) };