import {z} from 'zod' export type GetApiPoslinkV2PaymentRequestsArgs = {store_id: string, terminal_id?: string | undefined, status?: ('NEW' | 'IN_PROGRESS' | 'SUCCESSFUL' | 'CANCELLING' | 'CANCELLED' | 'FAILED') | undefined, transaction_type?: ('SALE' | 'REFUND') | undefined, start_date_time?: string | undefined, end_date_time?: string | undefined, limit?: number | undefined, offset?: number | undefined, sort?: ('ASC' | 'DESC') | undefined}; export type GetApiPoslinkV2PaymentRequestsOptions = {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 card = z.object({last4: z.string().optional(), issuing_country: z.string().optional(), brand: z.string().optional(), type: z.string().optional(), bin: z.string().optional()}); export const paymentMethodDetails = z.object({card: card.optional(), entry_mode: z.string().optional(), verification_method: z.string().optional(), application_name: z.string().optional(), application_id: z.string().optional(), mid: z.string().optional(), response_code: z.string().optional(), authorisation_code: z.string().optional()}); export const dccData = z.object({transaction_amount: z.number().int(), transaction_currency: z.string().min(3).max(3), exchange_rate: z.string().min(1), conversion_fee: z.string().min(1), disclaimer: z.string().min(1).max(500)}); export const transactionMetadata = z.object({payment_method_details: paymentMethodDetails.optional(), dcc: dccData.optional()}); export const retrievePaymentResponse = z.object({payment_request_id: z.string().optional(), requested_amount: requestedAmount.optional(), status: z.enum(["NEW", "IN_PROGRESS", "SUCCESSFUL", "CANCELLING", "CANCELLED", "FAILED"]).optional(), transaction_type: z.enum(["SALE", "REFUND"]).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), source_reference_id: z.string().optional(), merchant_reference: z.string().optional(), status_reason: z.enum(["CANCELLED_BY_EPOS", "CANCELLED_BY_USER", "DECLINED_ONLINE", "DECLINED_OFFLINE", "TIMEOUT", "CONNECTION_ERROR", "COMM_TIMEOUT", "CARD_PROCESSING_ERROR", "EXPIRED", "UNKNOWN", "TERMINAL_UNREACHABLE", "NOTIFICATION_EXPIRED", "SUPERSEDED_BY_NEW_TRANSACTION", "TRANSACTION_ALREADY_IN_PROGRESS", "FEATURE_NOT_ACTIVATED_UNREFERENCED_REFUNDS", "UNSUPPORTED_CURRENCY"]).optional(), epos_instance_id: z.string().optional(), gateway_payment_id: z.string().optional(), transaction_id: z.string().optional(), store_id: z.string().optional(), terminal_id: z.string().optional(), metadata: transactionMetadata.optional(), transaction_timestamp: z.string().optional()}); export const paging = z.object({offset: z.number().int().optional(), limit: z.number().int().optional(), total_pages: z.number().int().optional(), total_count: z.number().int().optional()}); export const getAllPaymentRequestsResponse = z.object({payment_requests: z.array(retrievePaymentResponse).optional(), pagination: paging.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 }; /** * List payments and refunds * * GET /poslink/v2/payment-requests */ export const getApiPoslinkV2PaymentRequests = async (args: GetApiPoslinkV2PaymentRequestsArgs, options?: GetApiPoslinkV2PaymentRequestsOptions) => { const {baseUrl = "https://api.teya.com", fetch: fetchFn = fetch, authorisation} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.store_id != null) query.append("store_id", String(args.store_id)) if (args.terminal_id != null) query.append("terminal_id", String(args.terminal_id)) if (args.status != null) query.append("status", String(args.status)) if (args.transaction_type != null) query.append("transaction_type", String(args.transaction_type)) if (args.start_date_time != null) query.append("start_date_time", String(args.start_date_time)) if (args.end_date_time != null) query.append("end_date_time", String(args.end_date_time)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sort != null) query.append("sort", String(args.sort)) if (authorisation !== undefined) headers.set('authorization', `Bearer ${authorisation}`) const url = `${baseUrl.replace(/\/+$/, '')}/poslink/v2/payment-requests${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getAllPaymentRequestsResponse, await res.json()) };