import {z} from 'zod' export type PaymentSimulationRequest = {application_key: string, campaign_name?: string | undefined, currency?: string | undefined, amount: number, installment_count?: string | undefined, inst_period?: number | undefined, card_number?: string | undefined, expire_month?: string | undefined, expire_year?: string | undefined, card_token?: string | undefined, is_3d: boolean, hash_data: string, customer_id?: string | undefined, is_submerchant_payment?: boolean | undefined, is_preauth?: boolean | undefined, mode?: string | undefined, language?: string | undefined, is_agricultural_payment?: boolean | undefined, agricultural_code?: string | undefined}; export type CreateApiApiV1PaymentSimulationArgs = {body: PaymentSimulationRequest}; export type CreateApiApiV1PaymentSimulationOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentSimulationResponse = z.object({response_code: z.string().optional(), message: z.string().optional(), is_3d: z.boolean().optional(), virtual_pos_name: z.string().optional(), virtual_pos_id: z.number().int().optional(), expected_cost_rate: z.number().optional(), expected_cost_amount: z.number().optional(), expected_customer_rate: z.number().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 }; /** * Payment Simulation * * POST /api/v1/payment/simulation */ export const createApiApiV1PaymentSimulation = async (args: CreateApiApiV1PaymentSimulationArgs, options?: CreateApiApiV1PaymentSimulationOptions) => { const {baseUrl = "https://api.paylox.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/payment/simulation${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(paymentSimulationResponse, await res.json()) };