import {z} from 'zod' export type BknBankPagamentosBusinessTransacaoSplit = {ClienteId?: string | undefined, Valor?: number | undefined, ValorRoyalties?: number | undefined, Decisao?: string | undefined}; export type BknBankPagamentosBusinessDescontoDataFixa = {Data?: string | undefined, ValorDesconto?: number | undefined}; export type BknBankPagamentosBusinessCriaTransacaoPixSplitRequest = {SplitValores?: Array | undefined, Tipo: string, Valor?: number | undefined, SegundosExpiracao?: number | undefined, DataVencimento?: string | undefined, DataLimitePagamento?: string | undefined, URLConfirmacao?: string | undefined, ClienteDescricao: string, ClienteIP?: string | undefined, ClienteCPFCNPJ: string, ClienteNome: string, ClienteEmail?: string | undefined, NumeroPedido?: string | undefined, Referencia?: string | undefined, TipoMulta?: ('NaoTemMulta' | 'ValorFixo' | 'ValorPercentual') | undefined, ValorMulta?: number | undefined, TipoJuros?: ('ValorAoDia' | 'TaxaMensal' | 'Isento') | undefined, ValorJuros?: number | undefined, TipoDesconto?: ('NaoTemDesconto' | 'ValorFixoAteDataInformada' | 'PercentualADataInformada') | undefined, ValorDesconto?: number | undefined, DescontosDataFixa?: Array | undefined, Pdv?: boolean | undefined}; export type CreateApiApiMarketplacePixArgs = {Aeskey: string, body: BknBankPagamentosBusinessCriaTransacaoPixSplitRequest}; export type CreateApiApiMarketplacePixOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const bknBankPagamentosBusinessCriaTransacaoPixSplitResponse = z.object({Retorno: z.enum(["OK", "ERRO"]).optional(), DetalhesErro: z.string().optional(), Identificador: z.string().optional(), QRCodeTexto: z.string().optional(), QRCodeImagem: z.string().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 }; /** * Cria uma transação pix * * POST /api/marketplace/pix */ export const createApiApiMarketplacePix = async (args: CreateApiApiMarketplacePixArgs, options?: CreateApiApiMarketplacePixOptions) => { const {baseUrl = "https://pagamentos.meuk8.com.br/PagamentosAPI", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Aeskey != null) headers.set("Aeskey", String(args.Aeskey)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/marketplace/pix${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(bknBankPagamentosBusinessCriaTransacaoPixSplitResponse, await res.json()) };