import {z} from 'zod' export type Address = {company?: string | undefined, street?: string | undefined, street2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined, phone?: string | undefined, 'first-name'?: string | undefined, 'last-name'?: string | undefined}; export type PlatformFee = {'fee-name'?: ('DealManager' | 'FillMyBook') | undefined}; export type ClearentAchTransactionRequest = {type: 'CREDIT' | 'DEBIT', amount: string, billing?: Address | undefined, shipping?: Address | undefined, invoice?: string | undefined, description?: string | undefined, comments?: string | undefined, 'software-type'?: string | undefined, 'software-type-version'?: string | undefined, 'account-type'?: ('Checking' | 'Savings') | undefined, 'routing-number'?: string | undefined, 'account-number'?: string | undefined, 'individual-name': string, 'validate-account'?: ('true' | 'false') | undefined, 'standard-entry-class-code'?: string | undefined, id?: string | undefined, 'tip-amount'?: string | undefined, 'purchase-order'?: string | undefined, 'email-receipt'?: string | undefined, 'email-address'?: string | undefined, 'customer-id'?: string | undefined, 'order-id'?: string | undefined, 'client-ip'?: string | undefined, 'billing-is-shipping'?: string | undefined, 'create-token'?: string | undefined, 'token-description'?: string | undefined, 'check-field'?: string | undefined, 'check-field-mid'?: boolean | undefined, 'platform-fees'?: Array | undefined}; export type CreateApiRestV2AchMobileTransactionsDebitArgs = {'api-key': string, achmobilejwt: string, body: ClearentAchTransactionRequest}; export type CreateApiRestV2AchMobileTransactionsDebitOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const address = z.object({company: z.string().optional(), street: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), zip: z.string().optional(), country: z.string().optional(), phone: z.string().optional(), "first-name": z.string().optional(), "last-name": z.string().optional()}); export const achTokenResponse = z.object({"token-id": z.string().optional(), "times-used": z.number().int().optional(), "last-used": z.string().optional(), "merchant-id": z.string().optional(), "routing-number": z.string().optional(), "account-number": z.string().optional(), "account-type": z.enum(["Checking", "Savings"]).optional(), "individual-name": z.string().optional(), "token-description": z.string().optional(), created: z.string().optional(), updated: z.string().optional(), "customer-key": z.string().optional(), default: z.boolean().optional()}); export const achTransaction = z.object({type: z.enum(["Debit", "Credit"]), amount: z.string().optional(), status: z.enum(["Pending", "Settling", "Settled", "Returned", "Writing", "Declined", "Not_Confirmed", "Sent", "Rejected", "Funded", "Partially_Funded", "Completed"]).optional(), "routing-number": z.string(), "account-number": z.string(), "account-type": z.enum(["Checking", "Savings"]), "standard-entry-class-code": z.literal("PPD"), "individual-name": z.string().optional(), id: z.string().optional(), "check-status": z.enum(["Pending", "Rejected", "Debit Sent", "Cancelled", "Credit Sent", "Unprocessed"]).optional(), "funding-status": z.enum(["Pending", "No Credit", "Credit Sent", "Chargeback", "Debit Sent", "Unprocessed"]).optional(), invoice: z.string().optional(), "check-number": z.string().optional(), billing: address.optional(), shipping: address.optional(), "purchase-order": z.string().optional(), "order-id": z.string().optional(), "status-change-date": z.string().optional(), "customer-id": z.string().optional(), "email-address": z.string().optional(), description: z.string().optional(), comments: z.string().optional(), "billing-is-shipping": z.enum(["true", "false"]).optional(), "display-message": z.string().optional(), "provider-transaction-id": z.string().optional(), "trace-number": z.string().optional(), "settled-date": z.string().optional(), "returned-date": z.string().optional(), "returned-code": z.string().optional(), "returned-message": z.string().optional(), "ach-token": achTokenResponse.optional(), "merchant-id": z.string().optional(), "terminal-id": z.string().optional(), "software-type": z.string().optional(), "paya-authorization": z.string().optional(), "processing-type": z.string().optional(), "external-transaction-reference": z.string().optional(), "execution-date": z.string().optional(), "token-id": z.string().optional(), "create-date": z.string().optional(), "micr-data": z.string().optional(), "image-front-edoc-id": z.string().optional(), "image-back-edoc-id": z.string().optional(), "image-front": z.string().optional(), "image-back": 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 }; /** * Create ACH mobile debit transaction * * POST /rest/v2/ach/mobile/transactions/debit */ export const createApiRestV2AchMobileTransactionsDebit = async (args: CreateApiRestV2AchMobileTransactionsDebitArgs, options: CreateApiRestV2AchMobileTransactionsDebitOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['api-key'] != null) headers.set("api-key", String(args['api-key'])) if (args.achmobilejwt != null) headers.set("achmobilejwt", String(args.achmobilejwt)) const url = `${baseUrl.replace(/\/+$/, '')}/rest/v2/ach/mobile/transactions/debit${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(achTransaction, await res.json()) };