import {z} from 'zod' export type GetApiV3AccountAccountIdArgs = {accountId: unknown, auditUser: string, transactionsOnly?: boolean | undefined, fromDate?: string | undefined, toDate?: string | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiV3AccountAccountIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const id = z.string(); export const linkId = z.string(); export const transactionDate = z.string(); export const postingDate = z.string(); export const postingStatus = z.enum(["POSTED", "POSTED_DISPUTE", "CLOSED", "PENDING", "REJECTED"]); export const responseCode = z.string(); export const transactionType = z.enum(["RETAIL", "CASH", "ATM", "CREDIT", "UNIQUE", "P2P_CREDIT", "P2P_DEBIT", "BALANCE_INQUIRY", "CASH_DISBURSEMENT"]); export const acquiringInstitutionIdentificationCode = z.string(); export const currency = z.string(); export const settlementAmount = z.object({amount: z.number().optional(), currency: currency.optional()}); export const money = z.object({amount: z.number().optional(), currency: currency.optional()}); export const country = z.string(); export const merchant = z.object({id: z.string().optional(), acquirerId: z.string().optional(), country: country.optional(), city: z.string().optional(), name: z.string().optional(), mcc: z.string().optional(), subMerchantId: z.string().optional()}); export const text = z.string().max(255); export const bankAccountType = z.enum(["BBAN", "IBAN", "PLUSGIRO", "BANKGIRO"]); export const createdAt = z.string(); export const accountTransactionResponse = z.object({id: id.optional(), linkId: linkId.optional(), transactionDate: transactionDate.optional(), postingDate: postingDate.optional(), postingStatus: postingStatus.optional(), responseCode: responseCode.optional(), responseDescription: z.string().optional(), transactionType: transactionType.optional(), transactionConditionCode: z.string().optional(), retrievalReferenceNumber: z.string().optional(), authorizationCode: z.string().optional(), acquiringInstitutionIdentificationCode: acquiringInstitutionIdentificationCode.optional(), cardAcceptorIdentificationCode: z.string().optional(), terminalId: z.string().optional(), submerchantId: z.string().optional(), settlementAmount: settlementAmount.optional(), transactionAmount: money.optional(), feeAmount: z.object({amount: z.number().optional(), currency: currency.optional()}).optional(), merchant: merchant.optional(), text: text.optional(), paymentReference: z.string().optional(), payerName: z.string().optional(), paymentReceiver: z.string().optional(), fromAddress: z.string().optional(), fromZip: z.string().optional(), fromCity: z.string().optional(), fromBankAccountNumber: z.string().optional(), toBankAccountNumber: z.string().optional(), fromCustomerAccountNumber: z.string().optional(), fromOrganizationalNumber: z.string().optional(), fromBankName: z.string().optional(), fromBankAccountType: bankAccountType.optional(), toBankAccountType: bankAccountType.optional(), toBic: z.string().optional(), discLinkTransactionId: z.string().optional(), createdAt: createdAt.optional(), fileName: z.string().optional(), processedByEnfuceStip: z.boolean().optional(), receivedFromCollectionAgency: z.boolean().optional(), acqRefNumber: z.string().optional(), authValidityInDays: z.number().int().optional(), exchangeRate: z.string().optional(), unit: z.string().optional(), siteCountry: z.string().optional(), transSrn: z.string().optional(), enfuceNduId: z.string().optional(), cardId: z.string().optional()}); export const getApiV3AccountAccountIdResponse = z.array(accountTransactionResponse); 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 }; /** * Get transactions and authorizations given accountId * * GET /v3/account/{accountId} */ export const getApiV3AccountAccountId = async (args: GetApiV3AccountAccountIdArgs, options: GetApiV3AccountAccountIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.auditUser != null) query.append("auditUser", String(args.auditUser)) if (args.transactionsOnly != null) query.append("transactionsOnly", String(args.transactionsOnly)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/account/${encodeURIComponent(String(args.accountId))}${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(getApiV3AccountAccountIdResponse, await res.json()) };