import {z} from 'zod' export type QueryBinRequest = {application_key: string, amount?: number | undefined, currency?: string | undefined, bin?: string | undefined, card_token?: string | undefined, store_code?: string | undefined, customer_id?: string | undefined, campaign_name?: string | undefined, mode?: string | undefined, language?: string | undefined, is_3d?: boolean | 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 CreateApiApiV1PrepaymentBinInstallmentOptionsArgs = {body: QueryBinRequest}; export type CreateApiApiV1PrepaymentBinInstallmentOptionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const modelsQueryBinResponseCardTypeEnum = z.enum(["Unknown", "Debit", "Credit", "Credit-Business"]); 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 modelsQueryBinResponsePosDevice = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const binOption = z.object({card_family_id: z.number().int().optional(), card_family: z.string().optional(), card_family_logo: z.string().optional(), card_type: modelsQueryBinResponseCardTypeEnum.optional(), currency: z.string().optional(), inst_period: z.number().int().optional(), campaign_name: z.string().optional(), installments: z.array(modelsPaymentOptionResponseInstallment).optional(), is_physical: z.boolean().optional(), physical_devices: z.array(modelsQueryBinResponsePosDevice).optional()}); export const queryBinResponse = z.object({response_code: z.string().optional(), message: z.string().optional(), is_3d: z.boolean().optional(), installments: z.array(binOption).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 }; /** * Bin Installment Options * * POST /api/v1/prepayment/bin_installment_options */ export const createApiApiV1PrepaymentBinInstallmentOptions = async (args: CreateApiApiV1PrepaymentBinInstallmentOptionsArgs, options?: CreateApiApiV1PrepaymentBinInstallmentOptionsOptions) => { 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/bin_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(queryBinResponse, await res.json()) };