import {z} from 'zod' export type CreateCoopValidationData = {amount: unknown, wallet_id?: (string | null) | undefined, api_ref?: (string | null) | undefined}; export type CreateApiApiV1PaymentCoopAgentDepositArgs = {body: CreateCoopValidationData}; export type CreateApiApiV1PaymentCoopAgentDepositOptions = {baseUrl?: string | undefined, IntaSendBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoicesSerStateEnum = z.enum(["PENDING", "PROCESSING", "FAILED", "CANCELED", "PARTIAL", "COMPLETE", "RETRY"]); export const provider906Enum = z.enum(["M-PESA", "PESALINK", "CARD-PAYMENT", "GOOGLE-PAY", "APPLE-PAY", "BITCOIN", "BANK-ACH", "COOP_B2B"]); export const invoicesSer = z.object({invoice_id: z.string(), state: invoicesSerStateEnum.optional(), provider: provider906Enum, charges: z.string().optional(), net_amount: z.string(), currency: z.string(), value: z.string(), account: z.string().max(140).nullable().optional(), api_ref: z.string().max(140).nullable().optional(), clearing_status: z.string(), mpesa_reference: z.string(), host: z.string().max(200).nullable().optional(), card_info: z.string(), retry_count: z.number().int().gte(-2147483648).lte(2147483647).optional(), failed_reason: z.string().nullable().optional(), failed_code: z.string().max(45).nullable().optional(), failed_code_link: z.string(), created_at: z.string().nullable(), updated_at: z.string().nullable()}); export const coopAgentsReferenceStatusEnum = z.enum(["PENDING", "PROCESSING", "FAILED", "COMPLETE"]); export const coopAgentsReference = z.object({invoice: invoicesSer, reference_no: z.number().int(), bank_account: z.string(), status: coopAgentsReferenceStatusEnum.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 }; /** * Generate Payment Reference for Coop-Bank Mtaani Agents * * POST /api/v1/payment/coop-agent-deposit/ */ export const createApiApiV1PaymentCoopAgentDeposit = async (args: CreateApiApiV1PaymentCoopAgentDepositArgs, options?: CreateApiApiV1PaymentCoopAgentDepositOptions) => { 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/payment/coop-agent-deposit/${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(coopAgentsReference, await res.json()) };