import {z} from 'zod' export type GetApiFinancialActionsArgs = {payment_id?: string | undefined, action_id?: string | undefined, limit?: number | undefined, pagination_token?: string | undefined}; export type GetApiFinancialActionsOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const financialActionBreakdown = z.object({breakdown_type: z.string(), fx_rate_applied: z.number(), holding_currency: z.string(), holding_currency_amount: z.number(), processing_currency: z.string(), processing_currency_amount: z.number(), transaction_currency: z.string().optional(), transaction_currency_amount: z.number().optional(), processing_to_transaction_currency_fx_rate: z.number().optional(), transaction_to_holding_currency_fx_rate: z.number().optional(), fee_detail: z.string().optional(), reserve_rate: z.string().optional(), reserve_release_date: z.string().optional(), reserve_deducted_date: z.string().optional(), tax_fx_rate: z.number().optional(), scheme_fx_adjustment_processing_currency: z.string().optional(), scheme_fx_adjustment_rate_applied: z.number().optional(), entity_country_tax_currency: z.string().optional(), tax_currency_amount: z.number().optional()}); export const financialAction = z.object({payment_id: z.string(), action_id: z.string(), action_type: z.string(), entity_id: z.string(), sub_entity_id: z.string().optional(), currency_account_id: z.string(), payment_method: z.string().optional(), processing_channel_id: z.string().optional(), mid: z.string().optional(), response_code: z.string().optional(), response_description: z.string().optional(), region: z.enum(["Domestic", "International", "Intra", "IntraEEA", "IntraEuropean_SEPA"]).optional(), card_type: z.enum(["CREDIT", "DEBIT", "PREPAID", "CHARGE"]).optional(), card_category: z.enum(["CONSUMER", "COMMERCIAL"]).optional(), issuer_country: z.string().optional(), merchant_category_code: z.string().optional(), fx_trade_id: z.string().optional(), acquirer_reference_number: z.string().optional(), account_funding_transaction: z.boolean().optional(), scheme_fx_adjustment_eligible: z.boolean().optional(), processed_on: z.string(), requested_on: z.string(), breakdown: z.array(financialActionBreakdown).optional(), bin: z.string().optional(), issuing_bank: z.string().optional(), payment_type: z.string().optional()}); export const financialActionsSelfLink = z.object({href: z.string().optional()}); export const financialActionsNextLink = z.object({href: z.string().optional()}); export const financialActionListResponse = z.object({count: z.number().int().optional(), limit: z.number().int().gte(1).lte(100).optional(), data: z.array(financialAction).optional(), _links: z.object({self: financialActionsSelfLink.optional(), next: financialActionsNextLink.optional()}).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 financial actions * * GET /financial-actions * * Also accepts auth: ApiSecretKey */ export const getApiFinancialActions = async (args: GetApiFinancialActionsArgs, options: GetApiFinancialActionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.payment_id != null) query.append("payment_id", String(args.payment_id)) if (args.action_id != null) query.append("action_id", String(args.action_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.pagination_token != null) query.append("pagination_token", String(args.pagination_token)) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/financial-actions${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(financialActionListResponse, await res.json()) };