import {z} from 'zod' export type CreateApiV3OrderreturnArgs = {id: number, echo?: boolean | undefined, includeCustomerMatches?: boolean | undefined, body: {merchantId: number, totalAmount: number, balance: number, lineItemCount: number, lineItems: Array<{id?: number | undefined, name?: string | undefined, price?: number | undefined, quantity?: string | undefined, discountAmount?: number | undefined, totalAmount?: number | undefined, taxCategory?: (Array<{id?: string | undefined, name?: string | undefined, code?: string | undefined, taxIds?: Array | undefined, totalTax?: string | undefined, isPrimary?: boolean | undefined, primaryExists?: boolean | undefined, display?: string | undefined, taxRate?: number | undefined}>) | undefined, initials?: string | undefined, taxes?: (Array<{id?: number | undefined, name?: string | undefined, description?: string | undefined, rate?: string | undefined, display?: string | undefined, taxCode?: string | undefined, taxCategoryId?: number | undefined}>) | undefined, amount?: string | undefined, taxAmount?: string | undefined, priceDiscountAmount?: string | undefined, taxRate?: number | undefined}>, shippingLineItems: Array<{id?: number | undefined, name?: string | undefined, price?: number | undefined, quantity?: string | undefined, discountAmount?: number | undefined, totalAmount?: number | undefined, taxCategory?: (Array<{id?: string | undefined, name?: string | undefined, code?: string | undefined, taxIds?: Array | undefined, totalTax?: string | undefined, isPrimary?: boolean | undefined, primaryExists?: boolean | undefined, display?: string | undefined, taxRate?: number | undefined}>) | undefined, initials?: string | undefined, taxes?: (Array<{id?: number | undefined, name?: string | undefined, description?: string | undefined, rate?: string | undefined, display?: string | undefined, taxCode?: string | undefined, taxCategoryId?: number | undefined}>) | undefined, amount?: string | undefined, taxAmount?: string | undefined, priceDiscountAmount?: string | undefined, taxRate?: number | undefined}>, payments: Array<{merchantId?: number | undefined, tenderType?: string | undefined, amount?: number | undefined, type?: string | undefined, paymentToken?: string | undefined}>}}; export type CreateApiV3OrderreturnOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiV3OrderreturnResponse = z.object({id: z.number().int().optional(), merchantId: z.number().int().optional(), orderId: z.number().int().optional(), created: z.string().optional(), modified: z.string().optional(), totalAmount: z.string().optional(), lineItemCount: z.number().int().optional(), balance: z.string().optional(), lineItems: z.array(z.object({})).optional(), shippingLineItems: z.array(z.object({})).optional(), discounts: z.array(z.object({})).optional(), payments: z.array(z.object({created: z.string().optional(), paymentToken: z.string().optional(), originalId: z.number().int().optional(), id: z.number().int().optional(), isDuplicate: z.boolean().optional(), merchantId: z.number().int().optional(), batchId: z.number().int().optional(), tenderType: z.string().optional(), currency: z.string().optional(), amount: z.string().optional(), cardAccount: z.object({cardType: z.string().optional(), entryMode: z.string().optional(), last4: z.string().optional(), token: z.string().optional(), expiryMonth: z.string().optional(), expiryYear: z.string().optional(), hasContract: z.boolean().optional(), cardPresent: z.boolean().optional()}).optional(), posData: z.object({}).optional(), authOnly: z.boolean().optional(), authCode: z.string().optional(), status: z.string().optional(), risk: z.object({}).optional(), customerMatches: z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), number: z.string().optional(), created: z.string().optional(), modified: z.string().optional(), loyaltyStatus: z.string().optional(), loyaltyEnrolled: z.string().optional()})).optional(), settledAmount: z.string().optional(), settledCurrency: z.string().optional(), cardPresent: z.boolean().optional(), authMessage: z.string().optional(), reference: z.string().optional(), invoice: z.string().optional(), customerName: z.string().optional(), type: z.string().optional(), reviewIndicator: z.number().int().optional(), source: z.string().optional(), shouldGetCreditCardLevel: z.boolean().optional()})).optional(), taxes: z.array(z.object({})).optional(), adjustmentAmount: 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 }; /** * Create Order Return * * POST /v3/orderreturn */ export const createApiV3Orderreturn = async (args: CreateApiV3OrderreturnArgs, options?: CreateApiV3OrderreturnOptions) => { const {baseUrl = "https://sandbox.api.mxmerchant.com/checkout", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.id != null) query.append("id", String(args.id)) if (args.echo != null) query.append("echo", String(args.echo)) if (args.includeCustomerMatches != null) query.append("includeCustomerMatches", String(args.includeCustomerMatches)) if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/orderreturn${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(createApiV3OrderreturnResponse, await res.json()) };