import {z} from 'zod' export type SubMerchantTypeEnum = 'Individual' | 'PersonalCompany' | 'Company'; export type SubMerchantUpdateRequest = {application_key: string, external_id: string, name?: string | undefined, type: SubMerchantTypeEnum, currency?: string | undefined, tax_number: string, tax_office?: string | undefined, title?: string | undefined, gsm_number?: string | undefined, email: string, contact_name?: string | undefined, contact_surname?: string | undefined, iban: string, address: string, merchant_name: string, language?: string | undefined, city?: string | undefined, payment_due_day?: number | undefined}; export type UpdateApiApiV1SubmerchantArgs = {body: SubMerchantUpdateRequest}; export type UpdateApiApiV1SubmerchantOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const subMerchantTypeEnum = z.enum(["Individual", "PersonalCompany", "Company"]); export const subMerchant = z.object({submerchant_id: z.string().optional(), external_id: z.string().optional(), merchant_name: z.string().optional(), name: z.string().optional(), type: subMerchantTypeEnum.optional(), currency: z.string().optional(), tax_number: z.string().optional(), tax_office: z.string().optional(), title: z.string().optional(), email: z.string().optional(), gsm_number: z.string().optional(), contact_name: z.string().optional(), contact_surname: z.string().optional(), iban: z.string().optional(), address: z.string().optional(), city: z.string().optional(), payment_due_day: z.number().int().optional()}); export const submerchantExternalStatusEnum = z.enum(["Active", "Waiting", "Cancelled"]); export const subMerchantExternalInfo = z.object({payment_org: z.string().optional(), external_status: submerchantExternalStatusEnum.optional()}); export const subMerchantResponse = z.object({response_code: z.string().optional(), message: z.string().optional(), submerchant_id: z.string().optional(), detail: subMerchant.optional(), externals: z.array(subMerchantExternalInfo).optional(), service_error_code: z.string().optional(), service_error_message: 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 }; /** * Update Submerchant * * PUT /api/v1/submerchant */ export const updateApiApiV1Submerchant = async (args: UpdateApiApiV1SubmerchantArgs, options?: UpdateApiApiV1SubmerchantOptions) => { const {baseUrl = "https://api.paylox.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/submerchant${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(subMerchantResponse, await res.json()) };