import {z} from 'zod' export type Authenticate = {transaction?: ({payment_method_token: string, currency_code?: string | undefined, amount?: number | undefined, test_scenario?: ({scenario?: string | undefined, ucaf_override?: string | undefined}) | undefined, browser_info: string, force_daf?: boolean | undefined, exemption_type?: string | undefined, acquiring_bank_fraud_rate?: string | undefined, three_ds_requestor_challenge_ind?: string | undefined}) | undefined}; export type CreateApiScaProvidersScaProviderKeyAuthenticateArgs = {body: Authenticate}; export type CreateApiScaProvidersScaProviderKeyAuthenticateOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const authenticateProperties = z.object({token: z.string().optional(), succeeded: z.boolean().optional(), state: z.string().optional(), message: z.string().optional(), transaction_type: z.string().optional(), gateway_transaction_key: z.string().optional(), sca_provider_key: z.string().optional(), acs_transaction_id: z.string().optional(), three_ds_server_trans_id: z.string().optional(), directory_server_transaction_id: z.string().optional(), xid: z.string().optional(), three_ds_version: z.string().optional(), flow_performed: z.string().optional(), ecommerce_indicator: z.string().optional(), directory_response_status: z.string().optional(), authentication_response_status: z.string().optional(), authentication_value: z.string().optional(), trans_status_reason: z.string().optional(), daf: z.boolean().optional(), managed_order_token: z.string().optional(), payment_method_key: z.string().optional()}); export const authenticateResponse = z.object({transaction: authenticateProperties.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 }; /** * Authenticate a given payment method * * POST /sca/providers/{sca_provider_key}/authenticate */ export const createApiScaProvidersScaProviderKeyAuthenticate = async (args: CreateApiScaProvidersScaProviderKeyAuthenticateArgs, options?: CreateApiScaProvidersScaProviderKeyAuthenticateOptions) => { const {baseUrl = "https://core.spreedly.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/sca/providers/${encodeURIComponent(String(args.sca_provider_key))}/authenticate${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(authenticateResponse, await res.json()) };