import {z} from 'zod' export type CustomerObject = {id?: string | undefined, name?: string | undefined, phone?: string | undefined, email?: string | undefined, address?: string | undefined, city?: string | undefined, postalCode?: string | undefined, country: string, locale?: string | undefined, createdAt?: string | undefined, updatedAt?: string | undefined}; export type TransactionType = 'payment' | 'transfer' | 'refund' | 'settlement'; export type PayoutObjectRequest = {amount: number, currency: string, country?: string | undefined, customerId?: string | undefined, customerObject?: CustomerObject | undefined, transactionType?: TransactionType | undefined, paymentReason?: string | undefined, merchantReference?: string | undefined, merchant?: ({secretCode?: unknown | undefined}) | undefined}; export type CreateApiPayoutsArgs = {payment_type?: ('wave_money' | 'orange_money' | 'maxit' | 'mtn_money' | 'free_money' | 'moov' | 'mobicash' | 'togocell' | 'bictorys' | 'card') | undefined, 'idempotency-key'?: string | undefined, 'X-Amzn-Trace-Id'?: string | undefined, body: PayoutObjectRequest}; export type CreateApiPayoutsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const payoutObject = z.object({id: z.string().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 a payout transaction * * POST /payouts */ export const createApiPayouts = async (args: CreateApiPayoutsArgs, options?: CreateApiPayoutsOptions) => { const {baseUrl = "https://api.test.bictorys.com/pay/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.payment_type != null) query.append("payment_type", String(args.payment_type)) if (args['idempotency-key'] != null) headers.set("idempotency-key", String(args['idempotency-key'])) 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(/\/+$/, '')}/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(payoutObject, await res.json()) };