import {z} from 'zod' export type Settle = {transaction_id: string, amount?: string | undefined}; export type CreateApiSimulateSettlementArgs = {body: Settle}; export type CreateApiSimulateSettlementOptions = {baseUrl?: string | undefined, identity?: string | undefined, authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiSimulateSettlementResponse = z.object({transaction_id: z.string().min(1), customer_id: z.string().min(1), card_id: z.string().min(1), raw_statement_descriptor: z.string().min(1), account_id: z.string().min(1), bond_brand_id: z.string().min(1), payment_rail: z.string().min(1), amount: z.string().min(1), amount_in_cents: z.string().min(1), currency_code: z.string().min(1), exchange_rate: z.unknown(), transaction_type: z.string().min(1), cardholder_presence_indicator: z.unknown(), original_transaction_id: z.unknown(), merchant_name: z.string().min(1), merchant_id: z.unknown(), merchant_address: z.unknown(), merchant_country: z.unknown(), merchant_city: z.string().min(1), merchant_state: z.string().min(1), merchant_zip_code: z.string().min(1), mcc: z.string().min(1), settled_date: z.string().min(1), new_balance: z.string().min(1), prior_balance: z.string().min(1), transaction_time: z.string().min(1), date_created: z.string().min(1), date_updated: z.string().min(1), fee: z.unknown(), rewards_rate: z.unknown(), rewards: z.unknown(), transaction_state: z.string().min(1), origin_timestamp: z.string().min(1), transaction_response_code: z.unknown(), acquiring_institution_identification_code: z.unknown()}); 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 }; /** * Simulate Settlement * * POST /simulate/settlement */ export const createApiSimulateSettlement = async (args: CreateApiSimulateSettlementArgs, options?: CreateApiSimulateSettlementOptions) => { const {baseUrl = "https://sandbox.bond.tech/api/v0", fetch: fetchFn = fetch, identity, authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (identity !== undefined) headers.set("Identity", identity) if (authorization !== undefined) headers.set("Authorization", authorization) const url = `${baseUrl.replace(/\/+$/, '')}/simulate/settlement${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(createApiSimulateSettlementResponse, await res.json()) };