import {z} from 'zod' export type GetApiTransactionsReceiptRenderArgs = {Authorization: unknown, Accept: string, ref_number: string, template_name?: string | undefined, receipt_data: {receipt_name: string, receipt_ref_num?: number | undefined} | {receipt_name?: string | undefined, receipt_ref_num: number}, merchant_id: string, receipt_format?: string | undefined, text_version?: boolean | undefined}; export type GetApiTransactionsReceiptRenderOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const version = z.string(); export const getApiTransactionsReceiptRenderResponse = z.object({receipt: z.string().optional(), v: version.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 }; /** * Render Receipt * * GET /transactions/receipt/render */ export const getApiTransactionsReceiptRender = async (args: GetApiTransactionsReceiptRenderArgs, options?: GetApiTransactionsReceiptRenderOptions) => { const {baseUrl = "https://tb.paymentfusion.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (args.ref_number != null) query.append("ref_number", String(args.ref_number)) if (args.template_name != null) query.append("template_name", String(args.template_name)) if (args.receipt_data != null) query.append("receipt_data", String(args.receipt_data)) if (args.merchant_id != null) query.append("merchant_id", String(args.merchant_id)) if (args.receipt_format != null) query.append("receipt_format", String(args.receipt_format)) if (args.text_version != null) query.append("text_version", String(args.text_version)) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/receipt/render${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(getApiTransactionsReceiptRenderResponse, await res.json()) };