import {z} from 'zod' export type PostCardsVerifyRequest = {type: 'credit' | 'debit' | 'voucher', local_datetime: unknown, initiator_id: string, reference_id?: string | undefined, statement_descriptor?: string | undefined, merchant_category_code?: string | undefined, card: {entry_mode?: ('magstripe' | 'emv' | 'emv_contacless' | 'contactless' | 'qr_code' | 'manual' | 'credentials_on_file') | undefined, number: unknown, expiration_date: unknown, holder_name?: string | undefined, cvv?: unknown | undefined, fallback?: boolean | undefined, brand?: ('visa' | 'mastercard' | 'amex' | 'hipercard' | 'elo' | 'jcb' | 'aura' | 'dinners') | undefined}}; export type CreateApiCardsVerifyArgs = {Host: string, body: PostCardsVerifyRequest}; export type CreateApiCardsVerifyOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const postCardsVerifyResponse = z.object({id: z.string(), initiator_id: z.string(), reference_id: z.string().optional(), status: z.enum(["authorized", "declined"]), result: z.enum(["success", "failed"]), created_at: z.string(), transaction_id: z.string(), operation_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), card: z.object({brand: z.string(), payment_account_reference: z.string().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 }; /** * Validação de um novo cartão.

A autenticação no endpoint é feita da seguinte forma: * * POST /cards/verify */ export const createApiCardsVerify = async (args: CreateApiCardsVerifyArgs, options?: CreateApiCardsVerifyOptions) => { 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(/\/+$/, '')}/cards/verify${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(postCardsVerifyResponse, await res.json()) };