import {z} from 'zod' export type CreateApiReceiptsExternalMerchantUidArgs = {merchant_uid: string, body: {channel_key: string, name: string, amount: number, product_type?: ('real' | 'digital') | undefined, identifier: string, identifier_type?: ('person' | 'business' | 'phone' | 'taxcard') | undefined, type?: ('person' | 'company') | undefined, buyer_name?: string | undefined, buyer_email?: string | undefined, buyer_tel?: string | undefined, tax_free?: number | undefined, vat_amount?: number | undefined, corp_reg_no?: string | undefined, pay_method?: ('trans' | 'vbank') | undefined}}; export type CreateApiReceiptsExternalMerchantUidOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const externalReceiptAnnotation = z.object({merchant_uid: z.string(), receipt_tid: z.string().optional(), apply_num: z.string(), type: z.string(), amount: z.number().int(), vat: z.number().int(), receipt_url: z.string().optional(), applied_at: z.number().int(), cancelled_at: z.number().int().optional()}); export const externalReceiptResponse = z.object({code: z.number().int().optional(), message: z.string().optional(), response: externalReceiptAnnotation.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 /receipts/external/{merchant_uid} */ export const createApiReceiptsExternalMerchantUid = async (args: CreateApiReceiptsExternalMerchantUidArgs, options?: CreateApiReceiptsExternalMerchantUidOptions) => { 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(/\/+$/, '')}/receipts/external/${encodeURIComponent(String(args.merchant_uid))}${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(externalReceiptResponse, await res.json()) };