import {z} from 'zod' export type CreateApiDisbursementsPayoutsArgs = {body: {sourceCurrency: 'USDT' | 'GHS' | 'KES' | 'UGX' | 'TZS' | 'USD' | 'EUR' | 'GBP' | 'ZMW' | 'ZAR' | 'USDC' | 'CNGN' | 'NGN' | 'XAF' | 'XOF', destinationCurrency: 'CNY' | 'CNH', amount: number, business: string, description?: string | undefined, customerReference: string, paymentDestination: 'bank_account', paymentScheme: 'cnaps' | 'swift', quoteReference?: string | undefined, beneficiary: {firstName: string, lastName?: string | undefined, accountHolderName: string, type: 'individual' | 'corporate', accountNumber: string, bankSwiftCode?: string | undefined, phone?: string | undefined, incorporationCountry?: string | undefined, bankAddress: {country: string, state: string, zip: string, city: string, street: string}, address: {country: string, state: string, zip: string, city: string, street: string}}, feeBearer?: 'business' | undefined, purposeOfFund: 'goods_trade' | 'services_trade' | 'payment_for_services' | 'professional_services' | 'information_services' | 'transportation_services' | 'software_purchase' | 'construction_services' | 'employee_payroll', files: Array, relationshipWithBeneficiary: 'self' | 'parents' | 'child' | 'siblings' | 'other_relatives' | 'spouse' | 'ex_spouse' | 'vendor' | 'supplier' | 'employee' | 'friends' | 'business_partner' | 'customer' | 'agent' | 'creditor' | 'debtor' | 'subsidiary_company' | 'branch_office' | 'holding_company' | 'franchisee', sender: {type: 'corporate' | 'individual', name: string, email: string, phone: string, nationality?: string | undefined, address: {country: string, state: string, zip: string, city: string, street: string}, idType: 'national_id' | 'international_passport' | 'drivers_license' | 'business_registration_number', idNumber: string, countryOfIncorporation?: string | undefined}}}; export type CreateApiDisbursementsPayoutsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiDisbursementsPayoutsResponse = z.object({success: z.boolean(), message: z.string().optional(), data: z.object({id: z.number().int().optional(), reference: z.string().optional(), customerReference: z.string().optional(), status: z.string().optional(), documentsRequired: z.array(z.object({})).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 }; /** * Initiate CNY Payout * * POST /disbursements/payouts */ export const createApiDisbursementsPayouts = async (args: CreateApiDisbursementsPayoutsArgs, options?: CreateApiDisbursementsPayoutsOptions) => { const {baseUrl = "https://sandboxapi.fincra.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/disbursements/payouts${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(createApiDisbursementsPayoutsResponse, await res.json()) };