import {z} from 'zod' export type CheckBilletDto = {digitable?: string | undefined, barcode?: string | undefined}; export type CreateApiV1MarketplacePaymentsCheckBilletArgs = {'integration-key': string, 'x-token': string, body: CheckBilletDto}; export type CreateApiV1MarketplacePaymentsCheckBilletOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const checkBilletRecipientResponse = z.object({name: z.string(), document: z.string()}); export const checkBilletPayerResponse = z.object({document: z.string(), name: z.string()}); export const checkBilletDetailsResponse = z.object({installments_paid: z.number(), total_amount_paid: z.number(), document_final_recipient: z.string().nullable(), final_recipient: z.string().nullable(), billet_type: z.string()}); export const checkBilletResponse = z.object({barcode: z.string(), digitable_line: z.string(), expected_on: z.string(), type: z.number(), amount: z.number(), nominal_amount: z.number(), discount: z.number(), fine: z.number(), interest: z.number(), max_amount: z.number().nullable(), min_amount: z.number().nullable(), transaction_id: z.string(), recipient: checkBilletRecipientResponse, payer: checkBilletPayerResponse, billet_details: checkBilletDetailsResponse}); 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 }; /** * Consultar os dados de um boleto antes do pagamento * * POST /v1/marketplace/payments/check-billet */ export const createApiV1MarketplacePaymentsCheckBillet = async (args: CreateApiV1MarketplacePaymentsCheckBilletArgs, options?: CreateApiV1MarketplacePaymentsCheckBilletOptions) => { 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/payments/check-billet${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(checkBilletResponse, await res.json()) };