import {z} from 'zod' export type GetApiV4OrchestrationPaymentUniqueReferenceNumberArgs = {uniqueReferenceNumber: unknown}; export type GetApiV4OrchestrationPaymentUniqueReferenceNumberOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const orchestrationPaymentResponse = z.object({uuid: z.string().optional(), unique_reference_number: z.string().optional(), sender_end_to_end_id: z.string().optional(), payment_type: z.enum(["REGULAR", "RETURN", "REVERSAL"]).optional(), connector_role: z.enum(["SENDING", "RECEIVING", "INTERMEDIARY"]).optional(), payment_id: z.string().optional(), payment_status: z.string().optional(), previous_payment_status: z.string().optional(), orchestration_template: z.string().optional(), orchestration_status: z.string().optional(), current_activity: z.string().optional(), current_activity_status: z.string().optional(), initial_context: z.object({}).optional(), current_context: z.object({}).optional(), error_message: z.object({}).optional(), activity_list: z.object({}).optional(), created_at: z.string().optional(), modified_at: 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 }; /** * Get orchestration payment * * GET /v4/orchestration/payment/{uniqueReferenceNumber} */ export const getApiV4OrchestrationPaymentUniqueReferenceNumber = async (args: GetApiV4OrchestrationPaymentUniqueReferenceNumberArgs, options: GetApiV4OrchestrationPaymentUniqueReferenceNumberOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/v4/orchestration/payment/${encodeURIComponent(String(args.uniqueReferenceNumber))}${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(orchestrationPaymentResponse, await res.json()) };