import {z} from 'zod' export type GetApiPaymentsTxnIdTypeTxnIdArgs = {txnId: string}; export type GetApiPaymentsTxnIdTypeTxnIdOptions = {baseUrl?: string | undefined, tebexCheckoutAuthBasic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const payment = z.object({transaction_id: z.string().optional(), status: z.object({id: z.number().int().optional(), description: z.string().optional()}).optional(), payment_sequence: z.string().optional(), created_at: z.string().optional(), price: z.object({amount: z.number().optional(), currency: z.string().optional()}).optional(), fees: z.object({tax: z.object({amount: z.number().optional(), currency: z.string().optional()}).optional(), gateway: z.object({amount: z.number().optional(), currency: z.string().optional()}).optional()}).optional(), customer: z.object({first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), ip: z.string().optional(), username: z.string().nullable().optional(), marketing_consent: z.boolean().optional(), country: z.string().optional(), postal_code: z.string().nullable().optional()}).optional(), products: z.array(z.object({id: z.string().nullable().optional(), name: z.string().optional(), quantity: z.number().int().optional(), base_price: z.object({amount: z.number().optional(), currency: z.string().optional()}).optional(), paid_price: z.object({amount: z.number().optional(), currency: z.string().optional()}).optional(), variables: z.array(z.string()).optional(), expires_at: z.string().nullable().optional(), custom: z.object({}).optional(), username: z.string().nullable().optional()})).optional(), coupons: z.array(z.object({})).optional(), gift_cards: z.array(z.object({})).optional(), recurring_payment_reference: z.string().nullable().optional(), custom: z.object({}).optional()}).nullable(); 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 }; /** * Fetch a payment by its transaction ID * * GET /payments/{txnId}?type=txn_id */ export const getApiPaymentsTxnIdTypeTxnId = async (args: GetApiPaymentsTxnIdTypeTxnIdArgs, options?: GetApiPaymentsTxnIdTypeTxnIdOptions) => { const {baseUrl = "https://checkout.tebex.io/api", fetch: fetchFn = fetch, tebexCheckoutAuthBasic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (tebexCheckoutAuthBasic !== undefined) headers.set('authorization', `Basic ${btoa(`${tebexCheckoutAuthBasic.username}:${tebexCheckoutAuthBasic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/${encodeURIComponent(String(args.txnId))}?type=txn_id${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(payment, await res.json()) };