import {z} from 'zod' export type CreateApiTransactionsRefundCashArgs = {Authorization: unknown, Accept: string, 'Content-Type': string, body: {ref_number: string, refund_amount: string, merchant_id: string, remote_id?: string | undefined, client_id?: string | undefined}}; export type CreateApiTransactionsRefundCashOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const amount = z.string(); export const authAmount = z.string(); export const gatewayAuthCode = z.string(); export const cardType = z.string(); export const createdAt = z.string(); export const gatewayIdentifier = z.enum(["usaepay", "payment_fusion"]); export const gatewayReferenceNumber = z.string(); export const gatewayResponseMessage = z.string(); export const gatewayResultCode = z.string(); export const gatewayResultText = z.string(); export const merchantId = z.string(); export const mid = z.string(); export const remoteId = z.string(); export const terminalId = z.string(); export const tipAmount = z.string(); export const transactionStatus = z.string(); export const version = z.string(); export const createApiTransactionsRefundCashResponse = z.object({amount: amount.optional(), auth_amount: authAmount.optional(), gateway_auth_code: gatewayAuthCode.optional(), card_type: cardType.optional(), created_at: createdAt.optional(), gateway_identifier: gatewayIdentifier.optional(), gateway_reference_number: gatewayReferenceNumber.optional(), gateway_response_message: gatewayResponseMessage.optional(), gateway_result_code: gatewayResultCode.optional(), gateway_result_text: gatewayResultText.optional(), merchant_id: merchantId.optional(), mid: mid.optional(), remote_id: remoteId.optional(), tid: terminalId.optional(), tip_amount: tipAmount.optional(), transaction_status: transactionStatus.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 }; /** * Refund a transaction * * POST /transactions/refund/cash */ export const createApiTransactionsRefundCash = async (args: CreateApiTransactionsRefundCashArgs, options?: CreateApiTransactionsRefundCashOptions) => { const {baseUrl = "https://tb.paymentfusion.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") 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['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/refund/cash${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiTransactionsRefundCashResponse, await res.json()) };