import {z} from 'zod' export type CreateApiV3BeneficiariesArgs = {body: {customer_id: string, country: string, currency: string, counter_party: 'FIRST_PARTY' | 'THIRD_PARTY', account_holder: {type: 'INDIVIDUAL', first_name: string, last_name: string, email: string, phone: string} | {type: 'BUSINESS', business_name: string, email: string, phone: string}, account_holder_address: {street_line_1: string, city: string, state: string, postcode: string, country: string}, receiver_meta_data: {nationality: string, transaction_purpose_id: number, transaction_purpose_remarks?: (string | null) | undefined, occupation_id?: number | undefined, occupation_remarks?: string | undefined, relationship: 'EMPLOYEE' | 'FREELANCER' | 'GIG_WORKER' | 'AFFILIATE' | 'CUSTOMER' | 'FAMILY_MEMBER' | 'FRIEND' | 'SELF' | 'OTHER' | 'SUPPLIER' | 'VENDOR' | 'SERVICE_PROVIDER' | 'MERCHANT' | 'CONTRACTOR' | 'SUBSIDIARY' | 'PARENT_COMPANY' | 'AFFILIATE_BUSINESS' | 'BANK_ACCOUNT' | 'BROKER' | 'EXCHANGE' | 'WALLET', relationship_remarks?: string | undefined, govt_id_number?: string | undefined, govt_id_issue_date?: string | undefined, govt_id_expire_date?: string | undefined}, developer_fee: {fixed: number, percentage: number}, bank_account: {bank_name: string, number: string, scheme: 'LOCAL' | 'SWIFT', type: 'CHECKING' | 'SAVINGS' | 'CURRENT'}, bank_address: {street_line_1: string, city: string, state: string, postcode: string, country: string}, bank_routing: Array<{scheme: 'ACH' | 'BANK_CODE' | 'BANK_IDENTIFIER' | 'BRANCH_CODE' | 'BRANCH_IDENTIFIER' | 'BSB' | 'IBAN' | 'IFSC' | 'SWIFT' | 'TRANSIT_NUMBER' | 'WIRE', number: string | number}>, deposit_instruction: {currency: 'USDC', rail: 'POLYGON'}, refund_instruction: {wallet_address: string, currency: 'USDC', rail: 'POLYGON'}, settlement_config?: ({auto_settlement?: boolean | undefined}) | undefined} | {customer_id: string, country: string, currency: string, counter_party: 'FIRST_PARTY' | 'THIRD_PARTY', account_holder: {type: 'INDIVIDUAL', first_name: string, last_name: string, email: string, phone: string} | {type: 'BUSINESS', business_name: string, email: string, phone: string}, account_holder_address: {street_line_1: string, city: string, state: string, postcode: string, country: string}, receiver_meta_data: {nationality: string, transaction_purpose_id: number, transaction_purpose_remarks?: (string | null) | undefined, occupation_id?: number | undefined, occupation_remarks?: string | undefined, relationship: 'EMPLOYEE' | 'FREELANCER' | 'GIG_WORKER' | 'AFFILIATE' | 'CUSTOMER' | 'FAMILY_MEMBER' | 'FRIEND' | 'SELF' | 'OTHER' | 'SUPPLIER' | 'VENDOR' | 'SERVICE_PROVIDER' | 'MERCHANT' | 'CONTRACTOR' | 'SUBSIDIARY' | 'PARENT_COMPANY' | 'AFFILIATE_BUSINESS' | 'BANK_ACCOUNT' | 'BROKER' | 'EXCHANGE' | 'WALLET', relationship_remarks?: string | undefined, govt_id_number?: string | undefined, govt_id_issue_date?: string | undefined, govt_id_expire_date?: string | undefined}, developer_fee: {fixed: number, percentage: number}, e_wallet: {scheme: string, number: string}, deposit_instruction: {currency: 'USDC', rail: 'POLYGON'}, refund_instruction: {wallet_address: string, currency: 'USDC', rail: 'POLYGON'}, settlement_config?: ({auto_settlement?: boolean | undefined}) | undefined} | {customer_id: string, country: string, currency: string, counter_party: 'FIRST_PARTY' | 'THIRD_PARTY', account_holder: {type: 'INDIVIDUAL', first_name: string, last_name: string, email: string, phone: string} | {type: 'BUSINESS', business_name: string, email: string, phone: string}, account_holder_address: {street_line_1: string, city: string, state: string, postcode: string, country: string}, receiver_meta_data: {nationality: string, transaction_purpose_id: number, transaction_purpose_remarks?: (string | null) | undefined, occupation_id?: number | undefined, occupation_remarks?: string | undefined, relationship: 'EMPLOYEE' | 'FREELANCER' | 'GIG_WORKER' | 'AFFILIATE' | 'CUSTOMER' | 'FAMILY_MEMBER' | 'FRIEND' | 'SELF' | 'OTHER' | 'SUPPLIER' | 'VENDOR' | 'SERVICE_PROVIDER' | 'MERCHANT' | 'CONTRACTOR' | 'SUBSIDIARY' | 'PARENT_COMPANY' | 'AFFILIATE_BUSINESS' | 'BANK_ACCOUNT' | 'BROKER' | 'EXCHANGE' | 'WALLET', relationship_remarks?: string | undefined, govt_id_number?: string | undefined, govt_id_issue_date?: string | undefined, govt_id_expire_date?: string | undefined}, developer_fee: {fixed: number, percentage: number}, destination_wallet: {address: string, rail: 'POLYGON' | 'ETHEREUM' | 'SOLANA', currency: 'USDC'}}}; export type CreateApiV3BeneficiariesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV3BeneficiariesResponse = z.object({data: z.object({beneficiary_id: z.string().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 }; /** * Create Beneficiary V3 * * POST /v3/beneficiaries */ export const createApiV3Beneficiaries = async (args: CreateApiV3BeneficiariesArgs, options?: CreateApiV3BeneficiariesOptions) => { const {baseUrl = "https://sandbox.api.fin.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/beneficiaries${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(createApiV3BeneficiariesResponse, await res.json()) };