import {z} from 'zod' export type GetApiVouchersPurchasesTransactionIdArgs = {transactionId: string}; export type GetApiVouchersPurchasesTransactionIdOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dtoConfirmation = z.object({confirmationNumber: z.string(), externalReferenceId: z.string(), transactionTime: z.string()}); 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 dtoVoucherReceipt = z.object({accountId: z.string(), confirmationNumber: z.string(), currency: z.string(), currencyDivisor: z.number().int(), deliveryType: z.string(), epin: z.string(), expiresAt: z.string(), instructions: z.string(), notes: z.string(), recipientCustomerServiceNumber: z.string(), redemptionUrl: z.string(), send: z.number().int(), senderCustomerServiceNumber: z.string(), terms: z.string(), voucherId: z.string()}); export const dtoValueType = z.enum(["ZEND", "PRICE", "COST"]); export const dtoPurchaseValue = z.object({type: dtoValueType, value: z.number().int()}); export const dtoVoucherPurchase = z.object({brand: z.string(), brandName: z.string(), confirmation: dtoConfirmation.optional(), 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, receipt: dtoVoucherReceipt.optional(), regions: z.array(z.string()).optional(), send: z.number().int(), sendCurrency: z.string(), sendCurrencyDivisor: z.number().int(), shortNotes: z.string(), status: dtoTransactionStatus, subTypes: z.array(z.string()), transactionId: z.string(), updatedAt: z.string(), value: dtoPurchaseValue.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 }; /** * Get purchase transaction by id * * GET /vouchers/purchases/{transactionId} */ export const getApiVouchersPurchasesTransactionId = async (args: GetApiVouchersPurchasesTransactionIdArgs, options: GetApiVouchersPurchasesTransactionIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/vouchers/purchases/${encodeURIComponent(String(args.transactionId))}${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(dtoVoucherPurchase, await res.json()) };