import {z} from 'zod' export type SimulateFeesDto = {amount: number, flag_id: number, gateway_id?: number | undefined, modality: string, interest: 'STORE' | 'CLIENT', antifraud_type?: string | undefined}; export type CreateApiV1MarketplaceTransactionsSimulateArgs = {'integration-key': string, 'x-token': string, establishment_id: string, body: SimulateFeesDto}; export type CreateApiV1MarketplaceTransactionsSimulateOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const simulationAmounts = z.object({total: z.number(), installment: z.number()}); export const simulationCredit = z.object({"1x": simulationAmounts, "2x": simulationAmounts, "3x": simulationAmounts, "4x": simulationAmounts, "5x": simulationAmounts, "6x": simulationAmounts, "7x": simulationAmounts, "8x": simulationAmounts, "9x": simulationAmounts, "10x": simulationAmounts, "11x": simulationAmounts, "12x": simulationAmounts, "13x": simulationAmounts, "14x": simulationAmounts, "15x": simulationAmounts, "16x": simulationAmounts, "17x": simulationAmounts, "18x": simulationAmounts, "19x": simulationAmounts, "20x": simulationAmounts, "21x": simulationAmounts, "22x": simulationAmounts, "23x": simulationAmounts, "24x": simulationAmounts}); export const simulationFees = z.object({credit: simulationCredit.optional(), debit: z.number().optional(), pix: z.number(), billet_barcode: z.number().optional(), billet_qrcode: z.number().optional()}); export const simulateFeesClientResponse = z.object({amount: z.number(), simulation: simulationFees}); 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 }; /** * Simulação de valores da transação * * POST /v1/marketplace/transactions/simulate */ export const createApiV1MarketplaceTransactionsSimulate = async (args: CreateApiV1MarketplaceTransactionsSimulateArgs, options?: CreateApiV1MarketplaceTransactionsSimulateOptions) => { const {baseUrl = "https://api.sandbox.paytime.com.br", 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['integration-key'] != null) headers.set("integration-key", String(args['integration-key'])) if (args['x-token'] != null) headers.set("x-token", String(args['x-token'])) if (args.establishment_id != null) headers.set("establishment_id", String(args.establishment_id)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/marketplace/transactions/simulate${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(simulateFeesClientResponse, await res.json()) };