import {z} from 'zod' export type GetApiAdminPaymentSimulatorTransactionJsonArgs = {x_account_id: string, x_reference: string, x_signature: string, x_test: boolean, x_transaction_type: string, x_gateway_reference: string}; export type GetApiAdminPaymentSimulatorTransactionJsonOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentResponseDto = z.object({x_account_id: z.string().optional(), x_amount: z.number().optional(), x_currency: z.string().optional(), x_error_code: z.string().optional(), x_gateway_reference: z.string().optional(), x_intent: z.string().optional(), x_message: z.string().optional(), x_reference: z.string().optional(), x_result: z.string().optional(), x_signature: z.string().optional(), x_test: z.boolean().optional(), x_timestamp: z.string().optional(), x_transaction_type: z.unknown()}); 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 transaction * * GET /admin/payment-simulator/transaction.json */ export const getApiAdminPaymentSimulatorTransactionJson = async (args: GetApiAdminPaymentSimulatorTransactionJsonArgs, options?: GetApiAdminPaymentSimulatorTransactionJsonOptions) => { const {baseUrl = "https://shop-name.onshopbase.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.x_account_id != null) query.append("x_account_id", String(args.x_account_id)) if (args.x_reference != null) query.append("x_reference", String(args.x_reference)) if (args.x_signature != null) query.append("x_signature", String(args.x_signature)) if (args.x_test != null) query.append("x_test", String(args.x_test)) if (args.x_transaction_type != null) query.append("x_transaction_type", String(args.x_transaction_type)) if (args.x_gateway_reference != null) query.append("x_gateway_reference", String(args.x_gateway_reference)) const url = `${baseUrl.replace(/\/+$/, '')}/admin/payment-simulator/transaction.json${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(paymentResponseDto, await res.json()) };