import {z} from 'zod' export type DeleteApiPaymentV2Args = {cif: string, paymentType: 'Card' | 'Card online' | 'CEC' | 'Bilet ordin' | 'Ordin plata' | 'Mandat postal' | 'Extras de cont' | 'Ramburs' | 'Alta incasare', invoiceSeries?: string | undefined, invoiceNumber?: string | undefined, paymentDate?: string | undefined, paymentValue?: number | undefined, clientName?: string | undefined, clientCif?: string | undefined}; export type DeleteApiPaymentV2Options = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const successResponse = z.object({errorText: z.string().optional(), 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 }; /** * Stergere incasare * * DELETE /payment/v2 */ export const deleteApiPaymentV2 = async (args: DeleteApiPaymentV2Args, options?: DeleteApiPaymentV2Options) => { const {baseUrl = "https://ws.smartbill.ro/SBORO/api", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cif != null) query.append("cif", String(args.cif)) if (args.paymentType != null) query.append("paymentType", String(args.paymentType)) if (args.invoiceSeries != null) query.append("invoiceSeries", String(args.invoiceSeries)) if (args.invoiceNumber != null) query.append("invoiceNumber", String(args.invoiceNumber)) if (args.paymentDate != null) query.append("paymentDate", String(args.paymentDate)) if (args.paymentValue != null) query.append("paymentValue", String(args.paymentValue)) if (args.clientName != null) query.append("clientName", String(args.clientName)) if (args.clientCif != null) query.append("clientCif", String(args.clientCif)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payment/v2${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(successResponse, await res.json()) };