import {z} from 'zod' export type PostChargeCaptureRequest = {amount: number, emv_data?: string | undefined, initiator_operation_id?: string | undefined}; export type CreateApiChargesIdCaptureArgs = {id: string, Host: string, body: PostChargeCaptureRequest}; export type CreateApiChargesIdCaptureOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const postChargeResponse = z.object({id: z.string(), initiator_id: z.string(), reference_id: z.string().optional(), amount: z.number().int(), payment_method: z.literal("card"), status: z.enum(["authorized", "paid", "canceled", "declined"]), created_at: z.string(), card_transaction: z.object({type: z.enum(["credit", "debit", "voucher"]), funding_source: z.enum(["credit", "debit", "prepaid"]).optional(), result: z.enum(["success", "failed"]), transaction_id: z.string(), operation_id: z.string(), brand_transaction_id: z.string().optional(), authorization_code: z.string().min(6).max(6).optional(), acquirer_message: z.string().optional(), acquirer_return_code: z.string().min(2).max(4), brand_return_code: z.string().optional(), merchant_advice_code: z.string().optional(), additional_data: z.array(z.object({name: z.string(), value: z.string()})).optional(), card: z.object({brand: z.string(), remaining_balance: z.number().int().optional(), emv_response: z.string().optional(), payment_account_reference: 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 }; /** * Captura de uma charge autorizada.

A autenticação no endpoint é feita da seguinte forma: * * POST /charges/{id}/capture */ export const createApiChargesIdCapture = async (args: CreateApiChargesIdCaptureArgs, options?: CreateApiChargesIdCaptureOptions) => { 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/${encodeURIComponent(String(args.id))}/capture${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(postChargeResponse, await res.json()) };