import {z} from 'zod' export type PwiBuyer = {id: string, name: string, surname: string, identityNumber: string, email: string, gsmNumber: string, registrationAddress: string, city: string, country: string, ip: string, zipCode?: string | undefined, registrationDate?: string | undefined, lastLoginDate?: string | undefined}; export type PwiAddress = {address: string, contactName: string, city: string, country: string, zipCode?: string | undefined}; export type PwiBasketItem = {id: string, price: unknown, name: string, category1: string, category2?: string | undefined, itemType: 'PHYSICAL' | 'VIRTUAL', subMerchantKey?: string | undefined, subMerchantPrice?: string | undefined}; export type PwiInitializeRequest = {locale?: ('tr' | 'en') | undefined, conversationId?: string | undefined, price: unknown, basketId: string, paymentGroup?: ('PRODUCT' | 'LISTING' | 'SUBSCRIPTION') | undefined, callbackUrl: string, currency: 'TRY' | 'USD' | 'EUR' | 'GBP' | 'NOK' | 'CHF', paidPrice: unknown, paymentChannel?: ('WEB' | 'MOBILE' | 'MOBILE_WEB' | 'MOBILE_IOS' | 'MOBILE_ANDROID' | 'MOBILE_WINDOWS' | 'MOBILE_TABLET' | 'MOBILE_PHONE') | undefined, enabledInstallments?: (Array<1 | 2 | 3 | 6 | 9 | 12>) | undefined, buyer: PwiBuyer, shippingAddress: PwiAddress, billingAddress: PwiAddress, basketItems: Array}; export type CreateApiPaymentPayWithIyzicoInitializeArgs = {Authorization: string, 'Content-Type': string, body: PwiInitializeRequest}; export type CreateApiPaymentPayWithIyzicoInitializeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const pwiInitializeResponse = z.object({status: z.enum(["success", "failure"]).optional(), locale: z.enum(["tr", "en"]).optional(), systemTime: z.number().int().optional(), conversationId: z.string().optional(), token: z.string().optional(), tokenExpireTime: z.number().int().optional(), payWithIyzicoPageUrl: z.string().optional(), signature: z.string().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 }; /** * iyzico ile Öde (PWI) Başlatma * * POST /payment/pay-with-iyzico/initialize */ export const createApiPaymentPayWithIyzicoInitialize = async (args: CreateApiPaymentPayWithIyzicoInitializeArgs, options?: CreateApiPaymentPayWithIyzicoInitializeOptions) => { 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/pay-with-iyzico/initialize${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(pwiInitializeResponse, await res.json()) };