import {z} from 'zod' export type GetApiTransactionsSearchArgs = {type?: ('checkout' | 'payout' | 'refund') | undefined, customer_name?: string | undefined, customer_phone?: string | undefined, payment_reference?: string | undefined, 'X-Amzn-Trace-Id'?: string | undefined}; export type GetApiTransactionsSearchOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionType = z.enum(["payment", "transfer", "refund", "settlement"]); export const customerObject = z.object({id: z.string().optional(), name: z.string().optional(), phone: z.string().optional(), email: z.string().optional(), address: z.string().optional(), city: z.string().optional(), postalCode: z.string().optional(), country: z.string(), locale: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const orderType = z.enum(["flat", "order", "invoice", "paymentlink"]); export const paymentStatus = z.enum(["succeeded", "failed", "cancelled", "pending", "processing", "reversed", "authorized"]); export const transactionObjectResponse = z.object({id: z.string().optional(), merchantId: z.string().optional(), type: transactionType.optional(), customerId: z.string().optional(), customerObject: customerObject.optional(), pspName: z.enum(["wave_money", "orange_money", "maxit", "mtn_money", "free_money", "moov", "mobicash", "togocell", "bictorys", "card"]), paymentMeans: z.string().optional(), paymentChannel: z.enum(["Terminal", "Online"]).optional(), amount: z.number().optional(), merchantFees: z.number().optional(), customerFees: z.number().optional(), transactionFeeAmountHT: z.number().optional(), transactionFeeAmountTax: z.number().optional(), currency: z.string().optional(), paymentReference: z.string().optional(), merchantReference: z.string().optional(), orderType: orderType.optional(), orderId: z.string().optional(), status: paymentStatus, deviceId: z.string().optional(), originIp: z.string().optional(), timestamp: z.string()}); export const getApiTransactionsSearchResponse = z.array(transactionObjectResponse); 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 }; /** * search transactions by customer name or customer phone * * GET /transactions/search */ export const getApiTransactionsSearch = async (args: GetApiTransactionsSearchArgs, options?: GetApiTransactionsSearchOptions) => { const {baseUrl = "https://api.test.bictorys.com/pay/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.customer_name != null) query.append("customer_name", String(args.customer_name)) if (args.customer_phone != null) query.append("customer_phone", String(args.customer_phone)) if (args.payment_reference != null) query.append("payment_reference", String(args.payment_reference)) if (args['X-Amzn-Trace-Id'] != null) headers.set("X-Amzn-Trace-Id", String(args['X-Amzn-Trace-Id'])) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiTransactionsSearchResponse, await res.json()) };