import {z} from 'zod' export type PostChargeCancelRequest = {id?: string | undefined, initiator_id?: string | undefined, amount: number, reason?: string | undefined, initiator_operation_id?: string | undefined, card_transaction?: ({card?: ({cvv?: unknown | undefined, emv_data?: string | undefined, track_1?: string | undefined, track_2?: string | undefined, track_3?: string | undefined}) | undefined, authentication?: ({type: 'online_pin' | 'offline_pin', online_pin?: ({ksi?: string | undefined, ksn?: string | undefined, pin_block?: string | undefined}) | undefined}) | undefined}) | undefined}; export type CreateApiChargesCancelArgs = {Host: string, body: PostChargeCancelRequest}; export type CreateApiChargesCancelOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const postChargeCancelResponse = z.object({id: z.string(), amount: z.number().int(), payment_method: z.literal("card"), status: z.enum(["authorized", "paid", "canceled", "declined"]), card_transaction: z.object({result: z.enum(["success", "failed"]), transaction_id: z.string(), operation_id: z.string(), acquirer_message: z.string().optional(), acquirer_return_code: z.string().min(2).max(4), authorization_code: z.string().optional(), card: z.object({remaining_balance: z.number().int().optional()}).optional(), additional_data: z.array(z.object({name: z.string(), value: z.string()})).optional(), currency_conversion: z.object({converted_amount: z.number(), currency_exp: z.number().optional(), currency_label: z.string().min(3).max(3).optional(), exchange_rate: z.number().optional(), markup: z.number().optional()}).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 }; /** * Reversão ou Cancelamento de uma charge.
Pode ser utilizado o `id` ou o `initiator_id`.
Se os dois forem informados, será usado o `id`.

A autenticação no endpoint é feita da seguinte forma: * * POST /charges/cancel */ export const createApiChargesCancel = async (args: CreateApiChargesCancelArgs, options?: CreateApiChargesCancelOptions) => { const {baseUrl = "https://payments.stone.com.br/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Host != null) headers.set("Host", String(args.Host)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/charges/cancel${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(postChargeCancelResponse, await res.json()) };