import {z} from 'zod' export type GetApiV2PaymentrequestsSentOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const orderDetails = z.object({merchantNumber: z.string().optional(), orderId: z.string().optional(), productId: z.string().optional(), customerNumber: z.string().optional(), variableReference: z.string().optional(), comment1: z.string().optional(), comment2: z.string().optional(), merchantCustomerIdentification: z.string().optional(), deliveryAddressLine1: z.string().optional(), deliveryAddressLine2: z.string().optional(), deliveryCity: z.string().optional(), deliveryPostCode: z.string().optional(), deliveryCountry: z.string().optional()}); export const paymentRequestSummary = z.object({accountName: z.string().optional(), amount: z.number().int().optional(), code: z.string().min(8).max(8).optional(), currency: z.string().optional(), dateCreated: z.string().optional(), description: z.string().optional(), direction: z.string().optional(), icanTo: z.number().int().optional(), maxNumberPayments: z.number().int().optional(), myRef: z.string().optional(), orderDetails: orderDetails.optional(), status: z.enum(["AWAITING_AUTHORISATION", "AUTHORISED", "AWAITING_MULTI_AUTHORISATION", "NOT_AUTHORISED", "SETTLED", "REJECTED", "ACCEPTED", "RECEIVED", "FUNDS_CONFIRMED"]).optional(), type: z.enum(["OTHER", "DIRECT", "SHAREABLE", "PARTY_TO_PARTY", "BILL_PAYMENT", "ECOMMERCE_GOODS", "ECOMMERCE_SERVICES"]).optional()}); export const paymentRequestsSent = z.object({total: z.number().int().optional(), dateRangeTo: z.number().int().optional(), paymentRequests: z.array(paymentRequestSummary).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 a list of payment request transactions * * GET /v2/paymentrequests/sent */ export const getApiV2PaymentrequestsSent = async (options?: GetApiV2PaymentrequestsSentOptions) => { const {baseUrl = "https://api.fire.com/business", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/paymentrequests/sent${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(paymentRequestsSent, await res.json()) };