import {z} from 'zod' export type GetApiV1MarketplaceBilletsIdArgs = {'integration-key': string, 'x-token': string, id: string}; export type GetApiV1MarketplaceBilletsIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const billetFeesBankingResponse = z.object({name: z.string(), description: z.string(), fees: z.number()}); export const address = z.object({street: z.string(), number: z.string(), complement: z.string().optional(), neighborhood: z.string(), city: z.string(), state: z.enum(["AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MS", "MT", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SP", "SE", "TO"]), zip_code: z.string().min(8).max(8)}); export const billetClientDto = z.object({first_name: z.string(), last_name: z.string().optional(), document: z.string(), email: z.string(), address: address}); export const billetLateFeeResponseDto = z.object({mode: z.literal("PERCENTAGE"), amount: z.number()}); export const billetInterestResponseDto = z.object({mode: z.literal("MONTHLY_PERCENTAGE"), amount: z.number()}); export const billetDiscountResponseDto = z.object({mode: z.literal("PERCENTAGE"), amount: z.number(), limit_date: z.string()}); export const billetInstructionResponseDto = z.object({booklet: z.boolean(), description: z.string().optional(), late_fee: billetLateFeeResponseDto.optional(), interest: billetInterestResponseDto.optional(), discount: billetDiscountResponseDto.optional()}); export const createBilletResponse = z.object({_id: z.string(), type: z.string(), gateway_key: z.string(), establishment_id: z.string(), description: z.string(), amount: z.number(), original_amount: z.number(), barcode: z.string(), digitable_line: z.string(), url: z.string(), status: z.enum(["PROCESSING", "CREATED", "PENDING", "CANCELED", "PAID", "FAILED"]), expiration_at: z.string(), payment_limit_date: z.string(), fees: z.number(), fees_banking: billetFeesBankingResponse, establishment: billetFeesBankingResponse, client: billetClientDto, billet_instructions: billetInstructionResponseDto, recharge: z.boolean(), pix_emv: z.string(), reference_id: z.union([z.string(), z.number()]).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 }; /** * Exibir boleto * * GET /v1/marketplace/billets/{id} */ export const getApiV1MarketplaceBilletsId = async (args: GetApiV1MarketplaceBilletsIdArgs, options?: GetApiV1MarketplaceBilletsIdOptions) => { const {baseUrl = "https://api.sandbox.paytime.com.br", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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 (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/marketplace/billets/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createBilletResponse, await res.json()) };