import {z} from 'zod' export type GetApiV1MarketplaceTransfersIdArgs = {'integration-key': string, 'x-token': string, id: string}; export type GetApiV1MarketplaceTransfersIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transferEntity = z.object({id: z.number(), first_name: z.string(), last_name: z.string(), document: z.string()}); export const transferAccount = z.object({establishment: transferEntity, marketplace: transferEntity, representative: transferEntity.optional(), document: z.string(), bank_code: z.string().optional(), bank_ispb: z.string().optional(), bank_name: z.string().optional(), routing_number: z.string().optional(), routing_check_digit: z.string().optional(), account_number: z.string(), account_check_digit: z.string(), type: z.enum(["SAVING", "CHECKING", "PAYMENT"]), pix_key: z.string().optional()}); export const transferHistory = z.object({status: z.enum(["CREATED", "PENDING", "PAID", "FAILED", "REFUNDED", "CANCELED", "PROCESSING"]), created_at: z.string(), _id: z.string()}); export const transferUser = z.object({id: z.number(), first_name: z.string(), last_name: z.string().optional(), profile_id: z.number()}); export const transferResponse = z.object({_id: z.string(), type: z.enum(["TED", "P2P", "PIX"]), status: z.enum(["CREATED", "PENDING", "PAID", "FAILED", "REFUNDED", "CANCELED", "PROCESSING"]), amount: z.number(), original_amount: z.number().optional(), establishment_id: z.string(), gateway_key: z.string().optional(), gateway_authorization: z.string(), expected_at: z.string(), recipient: transferAccount, payer: transferAccount, history: z.array(transferHistory), method: z.enum(["IN", "OUT"]), pixKey: z.string().optional(), end_to_end: z.string().optional(), description: z.string().optional(), reprocessed: z.boolean(), created_at: z.string(), updated_at: z.string(), fees: z.number().optional(), user: transferUser.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 }; /** * Detalhar transferĂȘncia * * GET /v1/marketplace/transfers/{id} */ export const getApiV1MarketplaceTransfersId = async (args: GetApiV1MarketplaceTransfersIdArgs, options?: GetApiV1MarketplaceTransfersIdOptions) => { 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/transfers/${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(transferResponse, await res.json()) };