import {z} from 'zod' export type RefundRequest = {transaction_id: string, terminal_id?: string | undefined, merchant_reference?: string | undefined, amount: number, currency?: string | undefined, basket_transaction_id?: string | undefined}; export type CreateApiPoslinkV2RefundsArgs = {'Idempotency-Key'?: string | undefined, body: RefundRequest}; export type CreateApiPoslinkV2RefundsOptions = {baseUrl?: string | undefined, authorisation?: string | undefined, fetch?: typeof fetch | undefined}; export const amount = z.object({amount: z.number().int().gte(0), currency: z.string()}); export const refundIssuerResult = z.object({approval_code: z.string().optional(), response_code: z.string().optional()}); export const dccResponse = z.object({cardholder_amount: amount, conversion_rate: z.string(), markup: z.string(), ecb_markup: z.string().optional()}); export const refundResponseV3 = z.object({transaction_id: z.string(), refund_amount: amount, status: z.enum(["SUCCESS", "FAILURE", "PENDING"]), transaction_type: z.enum(["REFUND", "REVERSAL"]), status_reason: z.enum(["ACCOUNT_INVALID", "CANNOT_VALIDATE_AUTHENTICATION", "CARD_COUNT_EXCEEDED", "CARD_DECLINED", "CARD_EXPIRED", "CARD_LIMIT_EXCEEDED", "CARD_NOT_ACTIVE", "DISABLED_ACCOUNT", "DO_NOT_HONOR", "ECOM_NEGATIVE_CARD_AUTHENTICATION_VERIFICATION", "EXCEEDS_AMOUNT_LIMIT", "FORMAT_ERROR", "INCORRECT_CARD_AUTHENTICATION", "INSUFFICIENT_FUNDS", "INVALID_CARD_NUMBER", "INVALID_TRANSACTION", "ISSUER_DECLINED", "LOST_OR_STOLEN", "MERCHANT_CONFIGURATION", "PIN_LIMIT_EXCEEDED", "PROCESSOR_UNAVAILABLE", "RESTRICTED_CARD", "SCA_PIN_REQUIRED", "SCA_REQUIRED", "SUSPECTED_FRAUD", "TEMPORARY_ISSUE_RETRY", "TIMEOUT", "TRANSACTION_NOT_ALLOWED", "UNABLE_TO_ROUTE_TRANSACTION", "UNSUPPORTED_ISSUER", "NEGATIVE_BALANCE_LIMIT", "OTHER"]).optional(), card_acceptor_id: z.string(), issuer_result: refundIssuerResult, created_at: z.string().optional(), dcc: dccResponse.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 }; /** * Process a refund of a transaction (Poslink V2) * * POST /poslink/v2/refunds */ export const createApiPoslinkV2Refunds = async (args: CreateApiPoslinkV2RefundsArgs, options?: CreateApiPoslinkV2RefundsOptions) => { 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/v2/refunds${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(refundResponseV3, await res.json()) };