import {z} from 'zod' export type DtoPurchaseField = {key: string, value: string}; export type DtoHomeAddress = {city: string, country: string, line1: string, line2: string, subdivision: string, zip: string}; export type DtoRecipient = {dateOfBirth?: string | undefined, email?: string | undefined, firstName: string, gender?: string | undefined, homeAddress: DtoHomeAddress, lastName: string, middleName?: string | undefined, mothersMaidenName?: string | undefined, nationality?: string | undefined, occupation?: string | undefined, phone?: string | undefined}; export type DtoSender = {dateOfBirth: string, email?: string | undefined, employerAddress?: string | undefined, employerName?: string | undefined, firstName: string, gender?: string | undefined, homeAddress: DtoHomeAddress, lastName: string, middleName?: string | undefined, mothersMaidenName?: string | undefined, nationality?: string | undefined, occupation?: string | undefined, phone: string, relationToRecipient?: string | undefined, sourceOfFunds?: string | undefined}; export type DtoValueType = 'ZEND' | 'PRICE' | 'COST'; export type DtoPurchaseValue = {type: DtoValueType, value: number}; export type DtoBillPayPurchaseInput = {fields: Array, offerId: string, recipient: DtoRecipient, sender: DtoSender, transactionId: string, value?: DtoPurchaseValue | undefined}; export type CreateApiBillpayPurchasesArgs = {body: DtoBillPayPurchaseInput}; export type CreateApiBillpayPurchasesOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dtoTransactionStatus = z.enum(["ACCEPTED", "PENDING", "AUTHORIZED", "IN_PROGRESS", "DONE", "FAILED"]); export const dtoBillPayPurchaseResponse = z.object({status: dtoTransactionStatus, transactionId: z.string()}); 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 }; /** * Create a BillPay purchase * * POST /billpay/purchases */ export const createApiBillpayPurchases = async (args: CreateApiBillpayPurchasesArgs, options: CreateApiBillpayPurchasesOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/billpay/purchases${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(dtoBillPayPurchaseResponse, await res.json()) };