import {z} from 'zod' export type MonetaryAmount = {amount?: number | undefined, currency?: string | undefined}; export type PaymentRequest = {amount?: MonetaryAmount | undefined, payee?: ('CREDITOR' | 'TRUEACCORD') | undefined, transactionReference?: string | undefined, transactionType?: ('PAYMENT' | 'RETURNED_PAYMENT' | 'REFUND') | undefined, note?: string | undefined, paymentTimestamp?: number | undefined}; export type CreateApiCustomersCustomerIdDebtsDebtIdPaymentsArgs = {body: PaymentRequest}; export type CreateApiCustomersCustomerIdDebtsDebtIdPaymentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const timeStamp = z.number().int(); export const monetaryAmount = z.object({amount: z.number().int().optional(), currency: z.string().optional()}); export const payment = z.object({meta: z.object({id: z.string().optional(), timeCreated: timeStamp.optional()}).optional(), amount: monetaryAmount.optional(), payee: z.enum(["CREDITOR", "TRUEACCORD"]).optional(), transactionReference: z.string().optional(), transactionType: z.enum(["PAYMENT", "RETURNED_PAYMENT", "REFUND"]).optional(), note: z.string().optional(), paymentTimestamp: z.number().int().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 }; /** * Report a payment * * POST /customers/{customerId}/debts/{debtId}/payments/ */ export const createApiCustomersCustomerIdDebtsDebtIdPayments = async (args: CreateApiCustomersCustomerIdDebtsDebtIdPaymentsArgs, options?: CreateApiCustomersCustomerIdDebtsDebtIdPaymentsOptions) => { const {baseUrl = "https://api.trueaccord.com/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.customerId))}/debts/${encodeURIComponent(String(args.debtId))}/payments/${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(payment, await res.json()) };