import {z} from 'zod' export type MerchantRole = 'seller' | 'payout'; export type CreateApiMerchantsArgs = {body: {name: string, platform_role: MerchantRole}}; export type CreateApiMerchantsOptions = {baseUrl?: string | undefined, apiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const merchantRole = z.enum(["seller", "payout"]); export const currency = z.enum(["JPY", "USD", "EUR", "TWD", "KRW", "PLN", "GBP", "HKD", "SGD", "NZD", "AUD", "IDR", "MYR", "PHP", "THB", "CNY", "BRL", "CHF", "CAD", "VND"]); export const amount = z.number().int().gte(0); export const serializedSubmerchant = z.object({id: z.string(), live: z.boolean(), created_at: z.string(), updated_at: z.string(), account_id: z.string(), name: z.string(), platform_role: merchantRole, status: z.string(), payments_enabled: z.boolean(), payouts_enabled: z.boolean(), send_payment_instruction_email: z.boolean().optional(), send_payment_receipt_email: z.boolean().optional(), send_payment_reminder_email: z.boolean().optional(), send_payment_refund_email: z.boolean().optional(), expiry_settings: z.array(z.object({name: z.string(), slug: z.string(), currency: currency, expiry_days: amount, default_expiry_days: amount})), active_payment_methods: z.array(z.object({name: z.string(), type: z.string(), currency: currency, percentage: amount, fixed_amount_cents: amount, minimum_processing_fee_amount_cents: amount})), publishable_key: z.string()}); 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 }; /** * Merchant: Create * * POST /merchants */ export const createApiMerchants = async (args: CreateApiMerchantsArgs, options?: CreateApiMerchantsOptions) => { const {baseUrl = "https://komoju.com/api/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKey.username}:${apiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/merchants${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(serializedSubmerchant, await res.json()) };