import {z} from 'zod' export type GetApiV3RefundIdArgs = {id: string}; export type GetApiV3RefundIdOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const metadata = z.record(z.string(), z.string()).nullable(); export const refundData = z.object({id: z.string().optional(), object: z.string().optional(), payin: z.string().optional(), amount: z.number().int().optional(), currency: z.string().optional(), customer_receives: z.object({currency: z.string().optional(), amount: z.number().int().optional()}).optional(), payment_attempt: z.string().optional(), reason: z.string().optional(), metadata: metadata.optional(), status: z.string().optional(), status_description: z.string().optional(), webhook_url: z.string().optional(), created_at: z.string().optional(), balance_transaction: z.string().optional(), reference_id: z.string().optional(), holding_currency: z.string().optional()}); export const getApiV3RefundIdResponse = z.object({status: z.string().optional(), message: z.string().optional(), data: refundData.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 }; /** * Fetch Refund * * GET /v3/refund/{id} */ export const getApiV3RefundId = async (args: GetApiV3RefundIdArgs, options?: GetApiV3RefundIdOptions) => { const {baseUrl = "https://service-sandbox.tazapay.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/refund/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV3RefundIdResponse, await res.json()) };