import {z} from 'zod' export type BarCode = {type: number, digitable?: (string | null) | undefined, barCode?: (string | null) | undefined}; export type VMBillPaymentAuthorize = {externalTerminal?: (string | null) | undefined, externalNSU?: number | undefined, barCode?: BarCode | undefined}; export type CreateApiV5TransactionsBillpaymentsAuthorizeArgs = {body: VMBillPaymentAuthorize}; export type CreateApiV5TransactionsBillpaymentsAuthorizeOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const registerData = z.object({documentRecipient: z.string().nullable().optional(), documentPayer: z.string().nullable().optional(), payDueDate: z.string().nullable().optional(), nextBusinessDay: z.string().optional(), dueDateRegister: z.string().optional(), allowChangeValue: z.boolean().optional(), recipient: z.string().nullable().optional(), payer: z.string().nullable().optional(), discountValue: z.number().optional(), interestValueCalculated: z.number().optional(), maxValue: z.number().optional(), minValue: z.number().optional(), fineValueCalculated: z.number().optional(), originalValue: z.number().optional(), totalUpdated: z.number().optional(), totalWithDiscount: z.number().optional(), totalWithAdditional: z.number().optional()}); export const authorizeResponse = z.object({assignor: z.string().nullable().optional(), registerData: registerData.optional(), settleDate: z.string().nullable().optional(), dueDate: z.string().optional(), endHour: z.string().nullable().optional(), initeHour: z.string().nullable().optional(), nextSettle: z.string().nullable().optional(), digitable: z.string().nullable().optional(), transactionId: z.number().int().optional(), type: z.number().int().optional(), value: z.number().optional(), maxValue: z.number().optional(), minValue: z.number().optional(), errorCode: z.string().nullable().optional(), message: z.string().nullable().optional(), status: z.number().int().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 }; /** * Consulta um pagamento de conta * * POST /v5/transactions/billpayments/authorize */ export const createApiV5TransactionsBillpaymentsAuthorize = async (args: CreateApiV5TransactionsBillpaymentsAuthorizeArgs, options?: CreateApiV5TransactionsBillpaymentsAuthorizeOptions) => { const {baseUrl = "https://sandbox.openfinance.celcoin.dev", 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(/\/+$/, '')}/v5/transactions/billpayments/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(authorizeResponse, await res.json()) };