import {z} from 'zod' export type RequestedAmount = {currency: unknown, amount: number, tip?: number | undefined}; export type CreatePaymentRequestV3 = {store_id: string, terminal_id: string, requested_amount: RequestedAmount, transaction_type: 'SALE' | 'REFUND', merchant_reference: string, epos_instance_id?: string | undefined, basket_transaction_id?: string | undefined, tab_id?: string | undefined, payment_type?: ('FULL' | 'SPLIT') | undefined, payment_method?: ('CARD' | 'CASH') | undefined}; export type CreateApiPoslinkV3PaymentRequestsArgs = {'Idempotency-Key': string, body: CreatePaymentRequestV3}; export type CreateApiPoslinkV3PaymentRequestsOptions = {baseUrl?: string | undefined, authorisation?: string | undefined, fetch?: typeof fetch | undefined}; export const requestedAmount = z.object({currency: z.string(), amount: z.number().int(), tip: z.number().int().optional()}); export const createPaymentResponseV3 = z.object({payment_request_id: z.string().optional(), created_at: z.string().optional(), terminal_id: z.string().optional(), merchant_reference: z.string().optional(), requested_amount: requestedAmount.optional(), status: z.literal("NEW").optional(), transaction_type: z.string().optional(), updated_at: z.string().optional(), epos_instance_id: z.string().optional(), store_id: z.string().optional(), tab_id: z.string().optional(), payment_type: z.enum(["FULL", "SPLIT"]).optional(), payment_method: z.enum(["CARD", "CASH"]).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 a payment request * * POST /poslink/v3/payment-requests */ export const createApiPoslinkV3PaymentRequests = async (args: CreateApiPoslinkV3PaymentRequestsArgs, options?: CreateApiPoslinkV3PaymentRequestsOptions) => { const {baseUrl = "https://api.teya.com", fetch: fetchFn = fetch, authorisation} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (authorisation !== undefined) headers.set('authorization', `Bearer ${authorisation}`) const url = `${baseUrl.replace(/\/+$/, '')}/poslink/v3/payment-requests${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(createPaymentResponseV3, await res.json()) };