import {z} from 'zod' export type TransactionState = 'Created' | 'Submitted' | 'Executed' | 'Failed' | 'Settled'; export type PaymentTypeQuery = 'Local' | 'InternalTransfer' | 'FXTransfer' | 'LocalReturnPayment' | 'Fee'; export type GetApiV1TransactionsArgs = {batch_id?: string | undefined, transaction_state?: TransactionState | undefined, payment_type?: PaymentTypeQuery | undefined, customer_account_id?: string | undefined, from?: string | undefined, to?: string | undefined, reference?: unknown | undefined, external_id?: unknown | undefined, account_name?: unknown | undefined, cursor?: (unknown | null) | undefined, page_size?: number | undefined}; export type GetApiV1TransactionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const cursorString = z.object({next: z.string().nullable().optional(), size: z.number().int().gte(0)}); export const transactionBatch = z.object({batch_id: z.string(), batch_sequence_no: z.number().int().gte(0)}); export const wireBankDetails = z.object({account_number: z.string().max(27), routing_number: z.string().max(12)}); export const scanDetails = z.object({account_number: z.string().max(8), sort_code: z.string().max(6)}); export const accountDetailsIdentifier = z.union([z.object({iban: z.string()}), z.object({wire: wireBankDetails}), z.object({scan: scanDetails})]); export const beneficiaryDetails = z.object({identifier: accountDetailsIdentifier, name: z.string().max(140)}); export const address = z.object({country_code: z.string().min(2).max(2), line_1: z.string().max(50), line_2: z.string().max(50).nullable().optional(), line_3: z.string().max(50).nullable().optional(), line_4: z.string().max(50).nullable().optional()}); export const beneficiaryExtraDetails = z.object({address: address.nullable().optional(), bic_code: z.string().nullable().optional()}); export const currency = z.enum(["AED", "USD", "JOD", "EGP", "SAR", "GBP", "EUR", "HKD", "CNH", "SGD"]); export const direction = z.enum(["Inbound", "Outbound"]); export const paymentFailure = z.union([z.object({bank_error: z.string()}), z.literal("insufficient_funds"), z.literal("internal_accounts_differ"), z.object({system_error: z.string()}), z.object({compliance_error: z.string()}), z.literal("account_frozen")]); export const feeSourceRef = z.union([z.object({type: z.literal("included_in_payment")}), z.object({type: z.literal("primary_account")})]); export const purposeCode = z.enum(["charity", "commercial_investment", "corporate_card", "credit_card", "dividend", "family", "financial_services", "goods_sold", "goods_bought", "government", "insurance", "intergroup_transfer", "intra_group_dividends", "information_technology", "leasing", "loan_charges", "merchant_settlement", "mobile_wallet", "non_resident_transfer_between_accounts", "none", "pension", "personal_expenses", "prepaid_cards", "professional", "rental", "resident_transfer_between_accounts", "salaries", "tax", "telecommunications", "travel", "utility_bill"]); export const remitterDetails = z.object({identifier: accountDetailsIdentifier, name: z.string().max(140)}); export const paymentScheme = z.enum(["uae_fts", "uae_ipp", "swift", "fedwire", "ach", "sepa", "sepa_instant", "faster_payments"]); export const transactionState = z.enum(["Created", "Submitted", "Executed", "Failed", "Settled"]); export const internalTransferTransactionState = z.enum(["Created", "Booked", "Failed", "Settled"]); export const fxTransferTransactionState = z.enum(["Created", "Submitted", "Executed", "Failed", "Settled"]); export const feeTargetRef = z.object({reference: z.string(), type: z.literal("Transaction")}); export const transaction = z.union([z.object({account_id: z.string(), amount_with_fee: z.number().int().gte(0), authorised_at: z.string().nullable().optional(), batch: transactionBatch.nullable().optional(), beneficiary: beneficiaryDetails, beneficiary_extra: beneficiaryExtraDetails.nullable().optional(), created_at: z.string(), currency: currency, direction: direction, executed_at: z.string().nullable().optional(), external_id: z.string().nullable().optional(), external_reference: z.string(), failed_at: z.string().nullable().optional(), failure_reason: paymentFailure.nullable().optional(), fee_amount: z.number().int().gte(0), fee_source: feeSourceRef.nullable().optional(), id: z.string(), instructed_amount: z.number().int().gte(0), purpose: purposeCode, remitter: remitterDetails, scheme: paymentScheme.nullable().optional(), state: transactionState, submitted_at: z.string().nullable().optional(), type: z.literal("local_payment")}), z.object({account_id: z.string(), beneficiary: beneficiaryDetails, created_at: z.string(), currency: currency, direction: direction, external_reference: z.string(), id: z.string(), instructed_amount: z.number().int().gte(0), reason: z.string().max(140), remitter: remitterDetails, returned_id: z.string(), state: transactionState, type: z.literal("local_return_payment")}), z.object({account_id: z.string(), counterpart_account_id: z.string(), created_at: z.string(), currency: currency, direction: direction, external_id: z.string().max(36).nullable().optional(), external_reference: z.string(), failed_at: z.string().nullable().optional(), failure_reason: paymentFailure.nullable().optional(), id: z.string(), instructed_amount: z.number().int().gte(0), settled_at: z.string().nullable().optional(), state: internalTransferTransactionState, type: z.literal("internal_transfer")}), z.object({account_id: z.string(), counterpart_account_id: z.string(), counterpart_amount: z.number().int().gte(0), counterpart_currency: currency, created_at: z.string(), currency: currency, direction: direction, executed_at: z.string().nullable().optional(), external_reference: z.string(), failed_at: z.string().nullable().optional(), failure_reason: paymentFailure.nullable().optional(), id: z.string(), instructed_amount: z.number().int().gte(0), rate: z.number(), state: fxTransferTransactionState, submitted_at: z.string().nullable().optional(), type: z.literal("f_x_transfer")}), z.object({account_id: z.string(), amount: z.number().int().gte(0), currency: currency, fee_target: feeTargetRef, id: z.string(), timestamp: z.string(), type: z.literal("fee_debit")})]); export const listTransactionsPagedResponse = z.object({pages: cursorString, results: z.array(transaction)}); 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 all transactions across accounts * * GET /v1/transactions */ export const getApiV1Transactions = async (args: GetApiV1TransactionsArgs, options?: GetApiV1TransactionsOptions) => { const {baseUrl = "https://auth-sandbox.fuse.me", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.batch_id != null) query.append("batch_id", String(args.batch_id)) if (args.transaction_state != null) query.append("transaction_state", String(args.transaction_state)) if (args.payment_type != null) query.append("payment_type", String(args.payment_type)) if (args.customer_account_id != null) query.append("customer_account_id", String(args.customer_account_id)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.reference != null) query.append("reference", String(args.reference)) if (args.external_id != null) query.append("external_id", String(args.external_id)) if (args.account_name != null) query.append("account_name", String(args.account_name)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.page_size != null) query.append("page_size", String(args.page_size)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/transactions${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(listTransactionsPagedResponse, await res.json()) };