import {z} from 'zod' export type PixPaymentInitDto = {type: 'HASH' | 'CPF' | 'CNPJ' | 'EMAIL' | 'PHONE' | 'RANDOM', key?: string | undefined, hash_code?: string | undefined}; export type CreateApiV1MarketplaceBankingTransfersPixInitArgs = {mTLS: string, 'integration-key': string, 'x-token': string, body: PixPaymentInitDto}; export type CreateApiV1MarketplaceBankingTransfersPixInitOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pixPaymentFeeDetail = z.object({name: z.string(), amount: z.number(), description: z.string(), _id: z.string()}); export const pixPaymentRecipient = z.object({first_name: z.string(), document: z.string(), bank_ispb: z.string(), bank_name: z.string(), routing_number: z.string(), routing_check_digit: z.string(), account_number: z.string(), account_check_digit: z.string(), account_type: z.string(), pix_key: z.string(), pix_key_type: z.string(), type: z.string()}); export const pixPaymentPayerEstablishment = z.object({id: z.number(), first_name: z.string(), last_name: z.string(), document: z.string(), type: z.string(), access_type: z.string(), risk: z.string(), active: z.boolean(), mcc: z.string()}); export const pixPaymentPayerMarketplace = z.object({id: z.number(), nickname: z.string(), first_name: z.string(), last_name: z.string(), document: z.string(), type: z.string(), active: z.boolean()}); export const pixPaymentPayer = z.object({bank_code: z.string(), bank_name: z.string(), routing_number: z.string(), account_number: z.string(), account_check_digit: z.string(), establishment: pixPaymentPayerEstablishment, marketplace: pixPaymentPayerMarketplace, type: z.string()}); export const pixPaymentHistory = z.object({status: z.string(), created_at: z.string(), _id: z.string()}); export const pixPaymentInitResponse = z.object({_id: z.string(), type: z.string(), status: z.string(), establishment_id: z.string(), init_type: z.string(), transaction_identification: z.string().nullable().optional(), gateway_authorization: z.string(), fees_details: z.array(pixPaymentFeeDetail), expected_at: z.string(), pixKey: z.string(), recipient: pixPaymentRecipient, payer: pixPaymentPayer, history: z.array(pixPaymentHistory), method: z.string(), description: z.string(), init_id: z.string(), end_to_end: z.string(), reprocessed: z.boolean(), commission_id: z.string().nullable().optional(), reversal_transaction_id: z.string().nullable().optional(), transfer_id_fees_origin: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string(), __v: z.number()}); 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 }; /** * Iniciar pagamento Pix * * POST /v1/marketplace/banking/transfers/pix-init */ export const createApiV1MarketplaceBankingTransfersPixInit = async (args: CreateApiV1MarketplaceBankingTransfersPixInitArgs, options?: CreateApiV1MarketplaceBankingTransfersPixInitOptions) => { 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.mTLS != null) headers.set("mTLS", String(args.mTLS)) 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/banking/transfers/pix-init${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(pixPaymentInitResponse, await res.json()) };