import {z} from 'zod' export type ScheduleAnnotation = {merchant_uid: string, schedule_at: number, currency: string, amount: number, tax_free?: number | undefined, vat_amount?: number | undefined, name: string, buyer_name?: string | undefined, buyer_email?: string | undefined, buyer_tel?: string | undefined, buyer_addr?: string | undefined, buyer_postcode?: string | undefined, custom_data?: string | undefined, notice_url?: string | undefined, product_type?: string | undefined, cash_receipt_type?: string | undefined, card_quota?: number | undefined, interest_free_by_merchant?: boolean | undefined, use_card_point?: boolean | undefined, product_count?: number | undefined, browser_ip?: string | undefined, extra?: ({naverUseCfm?: string | undefined}) | undefined, bypass?: string | undefined, promotion_id?: string | undefined}; export type CreateApiSubscribePaymentsScheduleArgs = {body: {customer_uid: string, customer_id?: string | undefined, checking_amount?: number | undefined, card_number?: string | undefined, expiry?: string | undefined, birth?: string | undefined, pwd_2digit?: string | undefined, cvc?: string | undefined, channel_key?: string | undefined, schedules: Array}}; export type CreateApiSubscribePaymentsScheduleOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduleResultAnnotation = z.object({customer_uid: z.string().optional(), merchant_uid: z.string().max(40).optional(), imp_uid: z.string().max(32).optional(), pg_provider: z.string().max(16).optional(), pg_id: z.string().max(64).optional(), customer_id: z.string().optional(), schedule_at: z.number().int().optional(), executed_at: z.number().int().optional(), revoked_at: z.number().int().optional(), amount: z.number().optional(), currency: z.string().optional(), name: z.string().max(40).optional(), buyer_name: z.string().max(16).optional(), buyer_email: z.string().max(64).optional(), buyer_tel: z.string().max(16).optional(), buyer_addr: z.string().max(128).optional(), buyer_postcode: z.string().max(8).optional(), custom_data: z.string().optional(), schedule_status: z.string().optional(), payment_status: z.string().optional(), fail_reason: z.string().optional(), promotion_id: z.string().optional()}); export const scheduleResponse = z.object({code: z.number().int().optional(), message: z.string().optional(), response: z.array(scheduleResultAnnotation).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 }; /** * 결제 예약 API * * POST /subscribe/payments/schedule */ export const createApiSubscribePaymentsSchedule = async (args: CreateApiSubscribePaymentsScheduleArgs, options?: CreateApiSubscribePaymentsScheduleOptions) => { const {baseUrl = "https://api.iamport.kr", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/subscribe/payments/schedule${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(scheduleResponse, await res.json()) };