import {z} from 'zod' export type PluId = {list_code: string, code: string}; export type PropertyRecord = {property_record_id?: string | undefined, property_id: string, property_name?: string | undefined, property_value?: (string | number | number | boolean | Array | Record) | undefined, last_change?: string | undefined}; export type BillItem = {plu_ids: Array, plu_name: string, category_plu_id?: string | undefined, vat_rate: number, quantity: number, paid_amount: number, price: number, bill_item_id: string, loyalty_off: boolean, purchase_item_type_id: string, customer_id?: string | undefined, purchase_item_property_records?: Array | undefined}; export type Bill = {bill_id: string, bill_number?: string | undefined, payment_time: string, created_by?: string | undefined, currency_id: string, total_price: number, bill_items: Array}; export type CreateApiPurchasesActionsAcceptPaymentArgs = {'Accept-Language'?: string | undefined, body: {store_id: string, cashdesk_number: number, card_number?: string | undefined, customer_id?: string | undefined, payment_type: 'S' | 'A' | 'C' | 'D', max_points?: number | undefined, max_credit?: number | undefined, benefit_codes?: Array | undefined, bill: Bill}}; export type CreateApiPurchasesActionsAcceptPaymentOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const discountItem = z.object({discount_code: z.string().optional(), discount_value: z.number().optional(), discount_percent: z.number().optional(), description: z.string().optional(), bill_item_id: z.string()}); export const paymentVoucher = z.object({code: z.string().optional(), name: z.string().optional(), discount_value: z.number().optional(), discount_percent: z.number().optional(), bill_item_id: z.string().optional()}); export const cashierMessage = z.object({text: z.string().optional()}); export const acceptedPayment = z.object({recommended_discounts: z.array(discountItem).optional(), credit_points: z.number().optional(), price_of_credit_points: z.number().optional(), credit: z.number().optional(), vouchers: z.array(paymentVoucher).optional(), credit_points_of_this_transaction: z.number().optional(), total_credit_points: z.number().optional(), total_credit: z.number().optional(), messages: z.array(cashierMessage).optional()}); export const createApiPurchasesActionsAcceptPaymentResponse = z.object({data: acceptedPayment.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 }; /** * Accept payment * * POST /purchases/actions/accept-payment * * Also accepts auth: basicAuth */ export const createApiPurchasesActionsAcceptPayment = async (args: CreateApiPurchasesActionsAcceptPaymentArgs, options?: CreateApiPurchasesActionsAcceptPaymentOptions) => { const {baseUrl = "https://sandbox.crmcarecloud.com/webservice/rest-api/enterprise-interface/v1.0", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/purchases/actions/accept-payment${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(createApiPurchasesActionsAcceptPaymentResponse, await res.json()) };