import {z} from 'zod' export type UUID = string; export type GetApiMerchantsMerchantIdSettlementsIdArgs = {merchant_id: UUID, id: string}; export type GetApiMerchantsMerchantIdSettlementsIdOptions = {baseUrl?: string | undefined, apiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string().min(25).max(25); export const status = z.enum(["pending", "paid", "cancelled", "bounced", "suspended"]); export const cents = z.number().int(); 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 settlementShow = z.object({id: uUID, resource: z.literal("settlement"), reference: z.string(), status: status, merchant_name: z.string(), company_name: z.string().nullable(), cycle: z.string(), transaction_amount_cents: cents, currency: currency, fee_amount_cents: cents, fee_tax_amount_cents: cents, settlement_amount_cents: cents, fx_currency: currency, fx_conversion_rate: z.string(), fx_conversion_amount_cents: cents, bank_transfer_fee_amount_cents: cents, remittance_amount_cents: cents, cutoff_time: z.string(), created_at: z.string(), download: z.object({csv: z.string().nullable(), xls: z.string().nullable()}), payments: z.object({captured_amount_total_cents: cents, processing_fees_cents: cents, total_cents: cents}), refunds: z.object({refunded_amount_total_cents: cents, refund_processing_fees_cents: cents, refunded_customer_fees_cents: cents, total_cents: cents}), platform_model: z.object({fund_transfer_total_cents: cents.optional(), payment_share_total_cents: cents.optional(), payment_share_refund_total_cents: cents.optional(), platform_fee_total_cents: cents.optional(), platform_fee_refund_total_cents: cents.optional(), submerchant_management_fees_cents: cents.optional(), total_cents: cents.optional()}).optional(), corrections: z.object({total_cents: cents}), komoju_card_charges: z.object({total_cents: cents}), disbursements: z.object({disbursement_amount_total_cents: cents, disbursement_fee_total_cents: cents, total_cents: cents}), misc: z.object({clearings_total_cents: cents, komoju_card_discount_total_cents: cents, chargeback_fixed_fee_total_cents: cents, other_fee_adjustments_total_cents: cents, total_cents: cents})}); 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 }; /** * Settlement: Show * * GET /merchants/{merchant_id}/settlements/{id} */ export const getApiMerchantsMerchantIdSettlementsId = async (args: GetApiMerchantsMerchantIdSettlementsIdArgs, options?: GetApiMerchantsMerchantIdSettlementsIdOptions) => { const {baseUrl = "https://komoju.com/api/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKey.username}:${apiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/merchants/${encodeURIComponent(String(args.merchant_id))}/settlements/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(settlementShow, await res.json()) };