import {z} from 'zod' export type PaymentRequestAmount = {currency: string, value: number}; export type PaymentAccountType = 'IBAN' | 'BBAN' | 'BG' | 'PG'; export type PaymentAccount = {accountNumber: string, accountType: PaymentAccountType, bic?: (string | null) | undefined, currencyCode?: (string | null) | undefined}; export type PaymentCreditor = {account: PaymentAccount, message?: (string | null) | undefined, name: string}; export type PaymentDebtor = {account: PaymentAccount, message?: (string | null) | undefined, name?: (string | null) | undefined}; export type PaymentRequestRemittanceInformationType = 'UNSTRUCTURED' | 'OCR'; export type PaymentRequestRemittanceInformation = {value: string, type: PaymentRequestRemittanceInformationType}; export type CreatePaymentRequest = {amount: PaymentRequestAmount, creditor: PaymentCreditor, debtor?: PaymentDebtor | undefined, executionDate?: (number | null) | undefined, remittanceInformation: PaymentRequestRemittanceInformation, region: string, paymentProductId: string, paymentTemplateId?: (string | null) | undefined}; export type CreateApiV1PaymentsArgs = {body: CreatePaymentRequest}; export type CreateApiV1PaymentsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentRequestAmount = z.object({currency: z.string(), value: z.number()}); export const paymentAccountType = z.enum(["IBAN", "BBAN", "BG", "PG"]); export const paymentAccount = z.object({accountNumber: z.string(), accountType: paymentAccountType, bic: z.string().nullable().optional(), currencyCode: z.string().nullable().optional()}); export const paymentCreditor = z.object({account: paymentAccount, message: z.string().nullable().optional(), name: z.string()}); export const paymentDebtor = z.object({account: paymentAccount, message: z.string().nullable().optional(), name: z.string().nullable().optional()}); export const paymentRequestRemittanceInformationType = z.enum(["UNSTRUCTURED", "OCR"]); export const paymentRequestRemittanceInformation = z.object({value: z.string(), type: paymentRequestRemittanceInformationType}); export const paymentRequestModel = z.object({id: z.string(), clientId: z.string().nullable().optional(), ClientAccess: z.string().nullable().optional(), status: z.string().nullable().optional(), amount: paymentRequestAmount, creditor: paymentCreditor, debtor: paymentDebtor.optional(), executionDate: z.number().int().nullable().optional(), remittanceInformation: paymentRequestRemittanceInformation, region: z.string(), paymentProductId: z.string(), paymentTemplateId: 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 a Payment Request. * * POST /v1/payments */ export const createApiV1Payments = async (args: CreateApiV1PaymentsArgs, options: CreateApiV1PaymentsOptions) => { 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 (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/v1/payments${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(paymentRequestModel, await res.json()) };