import {z} from 'zod' export type CreateApiTransactionsProcessCashArgs = {Authorization: unknown, Accept: string, 'Content-Type': string, body: {remote_id?: string | undefined, merchant_id: string, amount: string, invoice: string, software?: string | undefined, ip_address?: string | undefined, clerk?: string | undefined, billing_street?: string | undefined, billing_city?: string | undefined, billing_state?: string | undefined, billing_zip?: string | undefined, comments?: string | undefined, description?: string | undefined, command?: string | undefined, tip_amount?: string | undefined, force_duplicate?: boolean | undefined, allow_partial_auth?: boolean | undefined, zip_code?: string | undefined, street_address?: string | undefined, tax_amount?: string | undefined, client_id?: string | undefined}}; export type CreateApiTransactionsProcessCashOptions = {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 createApiTransactionsProcessCashResponse = 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 }; /** * Send cash transaction to be processed. * * POST /transactions/process/cash */ export const createApiTransactionsProcessCash = async (args: CreateApiTransactionsProcessCashArgs, options?: CreateApiTransactionsProcessCashOptions) => { 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/process/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(createApiTransactionsProcessCashResponse, await res.json()) };