import {z} from 'zod' export type GetApiBillpayPurchasesArgs = {_limit: number, _offset: number, createdAt?: string | undefined, status?: ('ACCEPTED' | 'PENDING' | 'AUTHORIZED' | 'IN_PROGRESS' | 'DONE' | 'FAILED') | undefined}; export type GetApiBillpayPurchasesOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dtoError = z.object({code: z.string(), description: z.string(), message: z.string()}); export const dtoPurchaseField = z.object({key: z.string(), value: z.string()}); export const dtoTransactionStatus = z.enum(["ACCEPTED", "PENDING", "AUTHORIZED", "IN_PROGRESS", "DONE", "FAILED"]); export const dtoTransactionLogItem = z.object({dateTime: z.string(), status: dtoTransactionStatus, statusMessage: z.string()}); export const dtoPriceType = z.enum(["FIXED", "RANGE"]); export const dtoProductType = z.enum(["TOPUP", "ESIM", "VOUCHER", "BILL_PAY", "WALLET_RECHARGE", "REFUND", "BULK_CHECKOUT"]); export const dtoHomeAddress = z.object({city: z.string(), country: z.string(), line1: z.string(), line2: z.string(), subdivision: z.string(), zip: z.string()}); export const dtoRecipient = z.object({dateOfBirth: z.string().optional(), email: z.string().optional(), firstName: z.string(), gender: z.string().optional(), homeAddress: dtoHomeAddress, lastName: z.string(), middleName: z.string().optional(), mothersMaidenName: z.string().optional(), nationality: z.string().optional(), occupation: z.string().optional(), phone: z.string().optional()}); export const dtoSender = z.object({dateOfBirth: z.string(), email: z.string().optional(), employerAddress: z.string().optional(), employerName: z.string().optional(), firstName: z.string(), gender: z.string().optional(), homeAddress: dtoHomeAddress, lastName: z.string(), middleName: z.string().optional(), mothersMaidenName: z.string().optional(), nationality: z.string().optional(), occupation: z.string().optional(), phone: z.string(), relationToRecipient: z.string().optional(), sourceOfFunds: z.string().optional()}); export const dtoValueType = z.enum(["ZEND", "PRICE", "COST"]); export const dtoPurchaseValue = z.object({type: dtoValueType, value: z.number().int()}); export const dtoBillPayPurchase = z.object({brand: z.string(), brandName: z.string(), cost: z.number().int(), costBase: z.number().int(), costCurrency: z.string(), costCurrencyDivisor: z.number().int(), costFee: z.number().int(), country: z.string(), createdAt: z.string(), error: dtoError.optional(), fields: z.array(dtoPurchaseField), log: z.array(dtoTransactionLogItem), notes: z.string(), offerId: z.string(), price: z.number().int(), priceBase: z.number().int(), priceCurrency: z.string(), priceCurrencyDivisor: z.number().int(), priceFee: z.number().int(), priceType: dtoPriceType, productType: dtoProductType, recipient: dtoRecipient, regions: z.array(z.string()).optional(), send: z.number().int(), sendCurrency: z.string(), sendCurrencyDivisor: z.number().int(), sender: dtoSender, shortNotes: z.string(), status: dtoTransactionStatus, subTypes: z.array(z.string()), transactionId: z.string(), updatedAt: z.string(), value: dtoPurchaseValue.optional()}); export const dtoBillPayPurchasesResponse = z.object({limit: z.number().int(), list: z.array(dtoBillPayPurchase), offset: z.number().int(), total: z.number().int()}); 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 }; /** * Get list of BillPay purchases * * GET /billpay/purchases */ export const getApiBillpayPurchases = async (args: GetApiBillpayPurchasesArgs, options: GetApiBillpayPurchasesOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args._limit != null) query.append("_limit", String(args._limit)) if (args._offset != null) query.append("_offset", String(args._offset)) if (args.createdAt != null) query.append("createdAt", String(args.createdAt)) if (args.status != null) query.append("status", String(args.status)) if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/billpay/purchases${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(dtoBillPayPurchasesResponse, await res.json()) };