import {z} from 'zod' export type SdkType = '01' | '02' | '03' | '04' | '05'; export type DeviceDetails = {device_type?: (string | null) | undefined, device_brand?: (string | null) | undefined, device_os?: (string | null) | undefined, device_display?: (string | null) | undefined}; export type SdkInformation = {sdk_app_id: string, sdk_enc_data: string, sdk_ephem_pub_key: {[key: string]: string}, sdk_trans_id: string, sdk_reference_number: string, sdk_max_timeout: number, sdk_type?: (SdkType | null) | undefined, device_details?: (DeviceDetails | null) | undefined}; export type DeviceChannel = 'APP' | 'BRW'; export type ThreeDsCompletionIndicator = 'Y' | 'N' | 'U'; export type PaymentsExternalAuthenticationRequest = {client_secret?: (string | null) | undefined, sdk_information?: (SdkInformation | null) | undefined, device_channel: DeviceChannel, threeds_method_comp_ind: ThreeDsCompletionIndicator}; export type CreateApiPaymentsPaymentId3dsAuthenticationArgs = {payment_id: string, body: PaymentsExternalAuthenticationRequest}; export type CreateApiPaymentsPaymentId3dsAuthenticationOptions = {baseUrl: string, publishableKey?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionStatus = z.enum(["Y", "N", "U", "A", "R", "C", "D", "I"]); export const paymentsExternalAuthenticationResponse = z.object({trans_status: transactionStatus, acs_url: z.string().nullable().optional(), challenge_request: z.string().nullable().optional(), challenge_request_key: z.string().nullable().optional(), acs_reference_number: z.string().nullable().optional(), acs_trans_id: z.string().nullable().optional(), three_dsserver_trans_id: z.string().nullable().optional(), acs_signed_content: z.string().nullable().optional(), three_ds_requestor_url: z.string(), three_ds_requestor_app_url: z.string().nullable().optional(), error_message: z.string().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 }; /** * Payments - External 3DS Authentication * * POST /payments/{payment_id}/3ds/authentication */ export const createApiPaymentsPaymentId3dsAuthentication = async (args: CreateApiPaymentsPaymentId3dsAuthenticationArgs, options: CreateApiPaymentsPaymentId3dsAuthenticationOptions) => { const {baseUrl, fetch: fetchFn = fetch, publishableKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (publishableKey !== undefined) headers.set("api-key", publishableKey) const url = `${baseUrl.replace(/\/+$/, '')}/payments/${encodeURIComponent(String(args.payment_id))}/3ds/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(paymentsExternalAuthenticationResponse, await res.json()) };