import {z} from 'zod' export type GetApiTransactionsTransactionIdArgs = {transaction_id: string}; export type GetApiTransactionsTransactionIdOptions = {baseUrl?: string | undefined, identity?: string | undefined, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const aCH = z.object({card_id: z.string(), external_account_id: z.string(), class_code: z.string(), direction: z.string(), network: z.string(), description: z.string(), failure_reason: z.string().optional(), return_code: z.string().optional()}); export const card = z.object({card_id: z.string(), mcc: z.string().optional(), mcc_description: z.string(), currency: z.string().optional(), exchange_rate: z.number().optional(), merchant_id: z.string().optional(), merchant_name: z.string().optional(), merchant_city: z.string().optional(), merchant_state: z.string().optional(), merchant_country: z.string().optional(), merchant_postal_code: z.string().optional(), cardholder_presence: z.boolean().optional(), statement_descriptor: z.string(), arn: z.string().optional(), fraud_rule_triggered: z.string().optional(), merchant_currency: z.string().optional(), merchant_amount: z.string().optional()}); export const transactionSchema = z.object({transaction_id: z.string(), bond_brand_id: z.string(), customer_id: z.string(), account_id: z.string(), payment_type: z.string(), transaction_type: z.string().optional(), previous_transaction_id: z.string().optional(), state: z.string().optional(), amount: z.string(), currency: z.string().optional(), created_time: z.string(), updated_time: z.string(), details: z.union([aCH, card]).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 }; /** * Get a transaction by id * * GET /transactions/{transaction_id} */ export const getApiTransactionsTransactionId = async (args: GetApiTransactionsTransactionIdArgs, options?: GetApiTransactionsTransactionIdOptions) => { const {baseUrl = "https://sandbox.bond.tech/api/v0", fetch: fetchFn = fetch, identity, authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (identity !== undefined) headers.set("Identity", identity) if (authorization !== undefined) headers.set("Authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/${encodeURIComponent(String(args.transaction_id))}${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(transactionSchema, await res.json()) };