import {z} from 'zod' export type SendMoneyInit = {currency: 'KES' | 'GHS' | 'NGN' | 'UGX' | 'TZS' | 'XAF' | 'XOF' | 'USD' | 'CDF' | 'RWF', provider: 'MPESA-B2C' | 'MPESA-B2B' | 'PESALINK' | 'INTASEND' | 'INTASEND-XB' | 'INTASEND-XB-BANK' | 'AIRTIME', device_id?: string | undefined, callback_url?: (string | null) | undefined, batch_reference?: (string | null) | undefined, transactions: Array<{name?: (string | null) | undefined, account: string, id_number?: (string | null) | undefined, amount: unknown, bank_code?: (string | null) | undefined, category_name?: (string | null) | undefined, narrative?: (string | null) | undefined, account_type?: ('TillNumber' | 'PayBill' | null) | undefined, account_reference?: (string | null) | undefined}>, country?: ('BF' | 'CM' | 'GH' | 'CI' | 'UG' | 'NG' | 'KE' | 'TZ') | undefined, requires_approval?: ('NO' | 'YES') | undefined}; export type CreateApiApiV1SendMoneyInitiateArgs = {body: SendMoneyInit}; export type CreateApiApiV1SendMoneyInitiateOptions = {baseUrl?: string | undefined, IntaSendBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyEnum = z.enum(["KES", "GHS", "NGN", "UGX", "TZS", "XAF", "XOF"]); export const walletTypeEnum = z.enum(["SETTLEMENT", "WORKING"]); export const walletSer = z.object({wallet_id: z.string(), label: z.string().max(140).optional(), can_disburse: z.boolean().optional(), currency: currencyEnum.optional(), wallet_type: walletTypeEnum.optional(), current_balance: z.string(), available_balance: z.string(), updated_at: z.string().nullable()}); export const transactionsSer = z.object({status: z.string(), status_code: z.string(), request_reference_id: z.string().optional(), name: z.string().max(240).nullable().optional(), account: z.string().max(24), id_number: z.string().max(20).nullable().optional(), bank_code: z.union([z.enum(["1", "2", "3", "6", "7", "10", "11", "12", "14", "16", "17", "18", "19", "23", "25", "31", "32", "35", "36", "43", "48", "49", "50", "51", "53", "54", "55", "57", "60", "61", "63", "65", "66", "68", "70", "72", "74", "75", "76", "78", "79", "89", "97", "FINANCE_TRUST_BANK_UGANDA_TRANSFER", "EXIM_BANK_UGANDA_TRANSFER", "BANK_OF_INDIA_UGANDA_TRANSFER", "CENTENARY_RURAL_DEVELOPMENT_BANK_UGANDA_TRANSFER", "BANK_OF_BARODA_UGANDA_TRANSFER", "BANK_OF_AFRICA_UGANDA_TRANSFER", "CITIBANK_UGANDA_TRANSFER", "ABSA_BANK_UGANDA_TRANSFER", "GUARANTY_TRUST_BANK_UGANDA_TRANSFER", "KCB_BANK_UGANDA_TRANSFER", "ABC_CAPITAL_BANK_UGANDA_TRANSFER", "EQUITY_BANK_UGANDA_TRANSFER", "HOUSING_FINANCE_BANK_UGANDA_TRANSFER", "CAIRO_INTERNATIONAL_BANK_UGANDA_TRANSFER", "NCBA_BANK_UGANDA_TRANSFER", "ORIENT_BANK_UGANDA_TRANSFER", "ECOBANK_UGANDA_TRANSFER", "UNITED_BANK_FOR_AFRICA_TRANSFER", "DFCU_BANK_UGANDA_TRANSFER", "POSTBANK_UGANDA_TRANSFER", "DIAMOND_TRUST_BANK_UGANDA_TRANSFER", "OPPORTUNITY_BANK_UGANDA_TRANSFER", "STANBIC_BANK_UGANDA_TRANSFER", "TOP_FINANCE_BANK_UGANDA_TRANSFER", "TROPICAL_BANK_UGANDA_TRANSFER", "STANDARD_CHARTERED_BANK_UGANDA_TRANSFER"]), z.literal(""), z.unknown()]).nullable().optional(), amount: z.string(), narrative: z.string().max(240).nullable().optional()}); export const sendMoneyResponse = z.object({file_id: z.string(), device_id: z.string(), tracking_id: z.string().optional(), batch_reference: z.string().max(70).nullable().optional(), status: z.string(), status_code: z.string(), nonce: z.string().max(6).optional(), wallet: walletSer, transactions: z.array(transactionsSer), charge_estimate: z.string(), total_amount_estimate: z.string(), total_amount: z.string(), transactions_count: z.string(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); 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 Send Money Request * * POST /api/v1/send-money/initiate/ */ export const createApiApiV1SendMoneyInitiate = async (args: CreateApiApiV1SendMoneyInitiateArgs, options?: CreateApiApiV1SendMoneyInitiateOptions) => { const {baseUrl = "https://api.intasend.com", fetch: fetchFn = fetch, IntaSendBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (IntaSendBearerAuth !== undefined) headers.set('authorization', `Bearer ${IntaSendBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/send-money/initiate/${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(sendMoneyResponse, await res.json()) };