import {z} from 'zod' export type Transaction = {amount?: (number | null) | undefined, categoryId?: number | undefined, title?: (string | null) | undefined, date?: (string | null) | undefined, info?: (string | null) | undefined, transactionId?: (string | null) | undefined, partner?: (string | null) | undefined}; export type Currency = {code?: (string | null) | undefined, creditTurnover?: (number | null) | undefined, debitTurnover?: (number | null) | undefined, endBalance?: (number | null) | undefined, startBalance?: (number | null) | undefined, transactions?: (Array | null) | undefined}; export type AccountHolder = {name?: (string | null) | undefined}; export type Account = {accountNumber?: (string | null) | undefined, bank?: (string | null) | undefined, country?: (string | null) | undefined, currencies?: (Array | null) | undefined, holders?: (Array | null) | undefined, iban?: (string | null) | undefined, isJointOwnership?: (boolean | null) | undefined, isSharedOwnership?: (boolean | null) | undefined, periodEnd?: (string | null) | undefined, periodStart?: (string | null) | undefined}; export type Debtor = {account: Account, message?: (string | null) | undefined, name?: (string | null) | undefined}; export type ScaMethodType = 'PSU_DECOUPLED' | 'PSU_REDIRECT'; export type PaymentAuthorizationRequest = {qrCodeRequired?: boolean | undefined, debtor?: Debtor | undefined, redirectUri?: (string | null) | undefined, selectedScaMethod: ScaMethodType}; export type CreateApiV1PaymentsPaymentIdAuthorizeArgs = {sessionId?: string | undefined, authorizationId?: string | undefined, paymentId: string, body: PaymentAuthorizationRequest}; export type CreateApiV1PaymentsPaymentIdAuthorizeOptions = {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 }; /** * Creating an authorization request for initiating payment. * * POST /v1/payments/{paymentId}/authorize */ export const createApiV1PaymentsPaymentIdAuthorize = async (args: CreateApiV1PaymentsPaymentIdAuthorizeArgs, options: CreateApiV1PaymentsPaymentIdAuthorizeOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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))}/authorize${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(paymentStatusResponse, await res.json()) };