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 BulkPayoutObjectRequest = {amount: number, currency: string, country?: string | undefined, customerId?: string | undefined, customerObject?: CustomerObject | undefined, transactionType?: TransactionType | undefined, paymentReason?: string | undefined, merchantReference?: string | undefined}; export type CreateApiBatchPayoutArgs = {payment_type: 'wave_money' | 'orange_money' | 'maxit' | 'mtn_money' | 'free_money' | 'moov' | 'mobicash' | 'togocell' | 'bictorys' | 'card', country_code: 'SN' | 'CI' | 'ML', 'X-Batch-Reference'?: string | undefined, 'idempotency-key'?: string | undefined, 'X-Amzn-Trace-Id'?: string | undefined, body: Array}; export type CreateApiBatchPayoutOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const batchPayoutObjectResponse = 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 bulk payout * * POST /batch-payout */ export const createApiBatchPayout = async (args: CreateApiBatchPayoutArgs, options?: CreateApiBatchPayoutOptions) => { 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.country_code != null) query.append("country_code", String(args.country_code)) if (args['X-Batch-Reference'] != null) headers.set("X-Batch-Reference", String(args['X-Batch-Reference'])) 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(/\/+$/, '')}/batch-payout${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(batchPayoutObjectResponse, await res.json()) };