import {z} from 'zod' export type DeleteApiChargesChargeIdArgs = {charge_id: string, body: {amount?: number | undefined}}; export type DeleteApiChargesChargeIdOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const deleteApiChargesChargeIdResponse = z.object({id: z.string().optional(), code: z.string().optional(), amount: z.number().int().optional(), paid_amount: z.number().int().optional(), canceled_amount: z.number().int().optional(), status: z.string().optional(), currency: z.string().optional(), payment_method: z.string().optional(), paid_at: z.string().optional(), canceled_at: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), customer: z.object({id: z.string().optional(), name: z.string().optional(), email: z.string().optional(), delinquent: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), phones: z.object({}).optional()}).optional(), last_transaction: z.object({id: z.string().optional(), transaction_type: z.string().optional(), funding_source: z.string().optional(), amount: z.number().int().optional(), status: z.string().optional(), success: z.boolean().optional(), installments: z.number().int().optional(), statement_descriptor: z.string().optional(), acquirer_tid: z.string().optional(), acquirer_nsu: z.string().optional(), acquirer_auth_code: z.string().optional(), acquirer_message: z.string().optional(), acquirer_return_code: z.string().optional(), operation_type: z.string().optional(), card: z.object({id: z.string().optional(), first_six_digits: z.string().optional(), last_four_digits: z.string().optional(), brand: z.string().optional(), holder_name: z.string().optional(), exp_month: z.number().int().optional(), exp_year: z.number().int().optional(), status: z.string().optional(), type: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), billing_address: z.object({zip_code: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), line_1: z.string().optional()}).optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), gateway_response: z.object({code: z.string().optional(), errors: z.array(z.object({})).optional()}).optional()}).optional(), metadata: z.object({code: z.string().optional()}).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 }; /** * Cancelar cobrança * * DELETE /charges/{charge_id}/ */ export const deleteApiChargesChargeId = async (args: DeleteApiChargesChargeIdArgs, options?: DeleteApiChargesChargeIdOptions) => { const {baseUrl = "https://api.mundipagg.com/core/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/charges/${encodeURIComponent(String(args.charge_id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", 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(deleteApiChargesChargeIdResponse, await res.json()) };