import {z} from 'zod' export type InstallmentRequest = {locale?: ('tr' | 'en') | undefined, price: number, binNumber?: string | undefined, conversationId?: string | undefined}; export type CreateApiPaymentIyziposInstallmentArgs = {Authorization: string, 'Content-Type': string, body: InstallmentRequest}; export type CreateApiPaymentIyziposInstallmentOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const installmentPrice = z.object({installmentPrice: z.number().optional(), totalPrice: z.number().optional(), installmentNumber: z.number().int().optional()}); export const installmentDetail = z.object({binNumber: z.string().optional(), price: z.number().optional(), cardType: z.enum(["CREDIT_CARD", "DEBIT_CARD", "PREPAID_CARD"]).optional(), cardAssociation: z.enum(["VISA", "MASTER_CARD", "AMERICAN_EXPRESS", "TROY"]).optional(), cardFamilyName: z.enum(["Bonus", "Axess", "World", "Maximum", "Paraf", "CardFinans", "Advantage"]).optional(), force3ds: z.union([z.literal(0), z.literal(1)]).optional(), bankCode: z.number().int().optional(), bankName: z.string().optional(), forceCvc: z.union([z.literal(0), z.literal(1)]).optional(), commercial: z.union([z.literal(0), z.literal(1)]).optional(), dccEnabled: z.union([z.literal(0), z.literal(1)]).optional(), agricultureEnabled: z.union([z.literal(0), z.literal(1)]).optional(), installmentPrices: z.array(installmentPrice).optional()}); export const installmentResponse = z.object({status: z.enum(["success", "failure"]).optional(), locale: z.string().optional(), systemTime: z.number().int().optional(), conversationId: z.string().optional(), installmentDetails: z.array(installmentDetail).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 }; /** * Taksit Sorgulama * * POST /payment/iyzipos/installment */ export const createApiPaymentIyziposInstallment = async (args: CreateApiPaymentIyziposInstallmentArgs, options?: CreateApiPaymentIyziposInstallmentOptions) => { const {baseUrl = "https://sandbox-api.iyzipay.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) const url = `${baseUrl.replace(/\/+$/, '')}/payment/iyzipos/installment${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(installmentResponse, await res.json()) };