import {z} from 'zod' export type GetApiV1MarketplacePaymentsIdArgs = {'integration-key': string, 'x-token': string, id: string}; export type GetApiV1MarketplacePaymentsIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentDetails = z.object({balance: z.number(), card: z.number(), total: z.number(), fees: z.number()}); export const recipientInfo = z.object({name: z.string(), document: z.string(), _id: z.string()}); export const billetDetails = 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(), _id: z.string()}); export const paymentUser = z.object({id: z.number(), first_name: z.string(), last_name: z.string().nullable(), profile_id: z.number()}); export const establishmentPaymentInfo = z.object({id: z.number(), type: z.enum(["INDIVIDUAL", "BUSINESS"]), access_type: z.string(), active: z.number(), first_name: z.string(), last_name: z.string(), document: z.string()}); export const marketplacePaymentInfo = z.object({id: z.number(), type: z.enum(["WHITELABEL", "LICENSED", "REPRESENTATIVE"]), nickname: z.string(), active: z.number(), first_name: z.string(), last_name: z.string(), document: z.string()}); export const historyEntry = z.object({status: z.enum(["PAID", "PENDING", "PROCESSING", "REFUNDED", "CANCELED"]), created_at: z.string(), _id: z.string()}); export const paymentFindOneResponse = z.object({_id: z.string(), type: z.string(), status: z.enum(["PAID", "PENDING", "PROCESSING", "REFUNDED", "CANCELED"]), barcode: z.string(), digitable_line: z.string(), expected_on: z.string(), amount: z.number(), nominal_amount: z.number(), discount: z.number(), fine: z.number(), interest: z.number(), gateway_key: z.string(), document_number: z.string(), gateway_authorization: z.string(), payment_details: paymentDetails, recipient: recipientInfo, billet_details: billetDetails, establishment_id: z.string(), user: paymentUser, establishment: establishmentPaymentInfo, marketplace: marketplacePaymentInfo, history: z.array(historyEntry), reprocessed: z.boolean(), created_at: z.string(), updated_at: z.string(), payment_date: z.string()}); 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 um pagamento especĂ­fico * * GET /v1/marketplace/payments/{id} */ export const getApiV1MarketplacePaymentsId = async (args: GetApiV1MarketplacePaymentsIdArgs, options?: GetApiV1MarketplacePaymentsIdOptions) => { 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/payments/${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(paymentFindOneResponse, await res.json()) };