import {z} from 'zod' export type ModelsPaymentOptionResponseCardTypeEnum = 'AllTypes' | 'Debit' | 'Credit' | 'Credit-Business'; export type PaymentOptionRequest = {application_key: string, amount?: number | undefined, currency?: string | undefined, campaign_name?: string | undefined, is_3d?: boolean | undefined, card_type?: ModelsPaymentOptionResponseCardTypeEnum | undefined, store_code?: string | undefined, customer_id?: string | undefined, mode?: string | undefined, language?: string | undefined, plus_inst_preferred?: boolean | undefined, plus_inst_seperated?: boolean | undefined, is_submerchant_payment?: boolean | undefined, is_preauth?: boolean | undefined, is_agricultural_payment?: boolean | undefined, agricultural_code?: string | undefined}; export type CreateApiApiV1PrepaymentInstallmentOptionsArgs = {body: PaymentOptionRequest}; export type CreateApiApiV1PrepaymentInstallmentOptionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const modelsPaymentOptionResponseCardTypeEnum = z.enum(["AllTypes", "Debit", "Credit", "Credit-Business"]); export const cardBank = z.object({id: z.number().int().optional(), name: z.string().optional(), eft_code: z.string().optional(), short_name: z.string().optional(), logo_url: z.string().optional()}); export const modelsPaymentOptionResponseInstallment = z.object({installment_count: z.number().int().optional(), installment_amount: z.number().optional(), cost_rate: z.number().optional(), customer_rate: z.number().optional(), additional_rate: z.number().optional(), margin_rate: z.number().optional(), min_customer_rate: z.number().optional(), max_customer_rate: z.number().optional(), plus_installment: z.number().int().optional(), plus_installment_description: z.string().optional(), min_amount: z.number().int().optional(), max_amount: z.number().int().optional()}); export const modelsPaymentOptionResponsePosDevice = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const cardOption = z.object({card_family_id: z.number().int().optional(), card_family: z.string().optional(), card_family_logo: z.string().optional(), card_type: modelsPaymentOptionResponseCardTypeEnum.optional(), card_banks: z.array(cardBank).optional(), currency: z.string().optional(), inst_period: z.number().int().optional(), campaign_name: z.string().optional(), excluded_bins: z.array(z.string()).optional(), installments: z.array(modelsPaymentOptionResponseInstallment).optional(), is_physical: z.boolean().optional(), physical_devices: z.array(modelsPaymentOptionResponsePosDevice).optional(), bank_ids: z.array(z.number().int()).optional()}); export const paymentOptionResponse = z.object({response_code: z.string().optional(), message: z.string().optional(), is_3d: z.boolean().optional(), installment_options: z.array(cardOption).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 }; /** * Installment Options * * POST /api/v1/prepayment/installment_options */ export const createApiApiV1PrepaymentInstallmentOptions = async (args: CreateApiApiV1PrepaymentInstallmentOptionsArgs, options?: CreateApiApiV1PrepaymentInstallmentOptionsOptions) => { 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/prepayment/installment_options${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(paymentOptionResponse, await res.json()) };