import {z} from 'zod' export type Address = {street: string, number: string, complement?: string | undefined, neighborhood: string, city: string, state: '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: string}; export type BilletClientDto = {first_name: string, last_name?: string | undefined, document: string, email: string, address: Address}; export type BilletLateFee = {mode: 'PERCENTAGE', amount: number}; export type BilletInterestDto = {mode: 'MONTHLY_PERCENTAGE', amount: number}; export type BilletDiscountDto = {mode: 'PERCENTAGE', amount: number, limit_date: string}; export type BilletInstructionDto = {booklet: boolean, description?: string | undefined, late_fee?: BilletLateFee | undefined, interest?: BilletInterestDto | undefined, discount?: BilletDiscountDto | undefined}; export type CreateBilletDto = {amount: number, expiration: string, payment_limit_date?: string | undefined, recharge?: boolean | undefined, reference_id?: (string | number) | undefined, client: BilletClientDto, instruction: BilletInstructionDto}; export type CreateApiV1MarketplaceBilletsArgs = {'integration-key': string, 'x-token': string, body: CreateBilletDto}; export type CreateApiV1MarketplaceBilletsOptions = {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 }; /** * Criar boleto * * POST /v1/marketplace/billets */ export const createApiV1MarketplaceBillets = async (args: CreateApiV1MarketplaceBilletsArgs, options?: CreateApiV1MarketplaceBilletsOptions) => { 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 (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/marketplace/billets${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(createBilletResponse, await res.json()) };