import {z} from 'zod' export type CreateApiApiV1TransactionTransactionIdAuthorizeArgs = {transactionId: string, body: {type: 'OTP', otp: string}}; export type CreateApiApiV1TransactionTransactionIdAuthorizeOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionStatus = z.enum(["PENDING", "PROCESSING", "SUCCESS", "FAILED", "CANCELLED", "REFUNDED", "RETRY", "UNKNOWN", "SCHEDULED", "CUSTOMER_ACTION_REQUIRED", "REJECTED", "IN_REVIEW", "RFI_REQUESTED", "DISPUTED", "DISPUTE_RESOLVED", "DISPUTE_WON", "DISPUTE_LOST", "DISPUTE_EVIDENCE_SUBMITTED"]); export const transaction = z.object({transactionId: z.string().optional(), customerId: z.string().optional(), sourceId: z.string().optional(), destinationId: z.string().optional(), sourceAmount: z.string().optional(), sourceCurrency: z.string().optional(), destinationAmount: z.string().optional(), destinationCurrency: z.string().optional(), type: z.enum(["DEPOSIT", "WITHDRAW", "SWAP", "REVERSAL", "SCHEDULED", "SEND"]).optional(), channel: z.enum(["BANK_ACCOUNT", "SWIFT", "MOBILE_MONEY", "UPI", "INTERAC", "WE_CHAT", "CARD", "CRYPTO", "VIRTUAL_BANK_ACCOUNT", "POOL_ACCOUNT", "ACH_BANK_ACCOUNT", "PAYBILL_TILL", "RFP", "VIRTUAL_CARD", "ALIPAY", "WALLET", "PAYMENT_LINQ", "ADMIN"]).optional(), status: transactionStatus.optional(), merchantReference: z.string().optional(), rate: z.string().optional(), fee: z.string().optional(), meta: z.object({narration: z.string().optional(), otpRequired: z.boolean().optional(), failureReason: z.object({code: z.enum(["AFX_REQUEST_FAILED", "AFX_SYSTEM_ERROR", "AFX_SERVICE_UNAVAILABLE", "AFX_INVALID_CURRENCY", "AFX_INVALID_AMOUNT", "AFX_INVALID_RECIPIENT", "AFX_RECIPIENT_NOT_FOUND", "AFX_ACCOUNT_CLOSED", "AFX_NAME_MISMATCH", "AFX_BENEFICIARY_RESTRICTED", "AFX_INVALID_SENDER", "AFX_INVALID_REQUEST", "AFX_VELOCITY_LIMIT_EXCEEDED", "AFX_AMOUNT_LIMIT_EXCEEDED", "AFX_RECIPIENT_WALLET_LIMIT_REACHED", "AFX_PAYMENT_FAILED", "AFX_COMPLIANCE_REJECTED", "AFX_PROPOSAL_EXPIRED"]), message: z.string(), retryable: z.boolean()}).optional()}).and(z.record(z.string(), z.unknown())).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const createApiApiV1TransactionTransactionIdAuthorizeResponse = z.object({data: transaction.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 }; /** * Authorize a pending transaction * * POST /api/v1/transaction/{transactionId}/authorize */ export const createApiApiV1TransactionTransactionIdAuthorize = async (args: CreateApiApiV1TransactionTransactionIdAuthorizeArgs, options?: CreateApiApiV1TransactionTransactionIdAuthorizeOptions) => { const {baseUrl = "https://sandbox.api.afriex.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/transaction/${encodeURIComponent(String(args.transactionId))}/authorize${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(createApiApiV1TransactionTransactionIdAuthorizeResponse, await res.json()) };