import {z} from 'zod' export type CreateApiV1PaymentsPaymentIdExecuteArgs = {sessionId?: string | undefined, authorizationId?: string | undefined, paymentId: string}; export type CreateApiV1PaymentsPaymentIdExecuteOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentStatusType = z.enum(["CREATED", "RECEIVED", "CANCELED", "FAILED", "REJECTED", "RECIPIENT_AUTHORIZATION_REQUIRED", "AUTHORIZATION_REQUIRED", "IN_PROGRESS_CODE", "IN_PROGRESS", "RECIPIENT_AUTHORIZED", "AUTHORIZED", "COMPLETED", "DELETED"]); export const scaMethodType = z.enum(["PSU_DECOUPLED", "PSU_REDIRECT"]); export const scaMethod = z.object({href: z.string().nullable().optional(), downloadLink: z.string().nullable().optional(), downloadInterval: z.number().int().nullable().optional(), scaMethodType: scaMethodType}); export const paymentStatusResponse = z.object({id: z.string(), status: paymentStatusType, psuMessage: z.string().nullable().optional(), scaMethods: z.array(scaMethod).nullable().optional(), tppMessages: z.array(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 }; /** * Execute payment * * POST /v1/payments/{paymentId}/execute */ export const createApiV1PaymentsPaymentIdExecute = async (args: CreateApiV1PaymentsPaymentIdExecuteArgs, options: CreateApiV1PaymentsPaymentIdExecuteOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sessionId != null) headers.set("sessionId", String(args.sessionId)) if (args.authorizationId != null) headers.set("authorizationId", String(args.authorizationId)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/v1/payments/${encodeURIComponent(String(args.paymentId))}/execute${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paymentStatusResponse, await res.json()) };