import {z} from 'zod' export type GetApiPaymentsIdActionsArgs = {id: string}; export type GetApiPaymentsIdActionsOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const actionId = z.string().min(30).max(30); export const serverTimestamp = z.string(); export const amountAllocations = z.array(z.object({id: z.string(), amount: z.number().int().gte(0).lte(9999999999), reference: z.string().max(50).optional(), commission: z.object({amount: z.number().int().gte(0).optional(), percentage: z.number().gte(0).lte(100).optional()}).optional()})); export const items = z.array(z.object({type: z.enum(["digital", "discount", "physical"]).optional(), sub_type: z.enum(["blockchain", "cbdc", "cryptocurrency", "nft", "stablecoin"]).optional(), name: z.string().max(255).optional(), quantity: z.number().int().gte(1).optional(), unit_price: z.number().int().gte(0).optional(), reference: z.string().max(255).optional(), commodity_code: z.string().max(12).optional(), unit_of_measure: z.string().max(12).optional(), total_amount: z.number().optional(), tax_rate: z.number().optional(), tax_amount: z.number().optional(), tax_exempt: z.boolean().optional(), discount_amount: z.number().optional(), wxpay_goods_id: z.string().max(32).optional(), url: z.string().max(1024).optional(), image_url: z.string().max(1024).optional(), service_ends_on: z.string().optional(), purchase_country: z.string().max(2).optional(), foreign_retailer_amount: z.number().int().gte(0).optional()})); export const paymentAction = z.object({id: actionId, type: z.enum(["Authorization", "Card Verification", "Void", "Capture", "Refund", "Payout", "Return"]), processed_on: serverTimestamp, amount: z.number().int(), approved: z.boolean().optional(), auth_code: z.string().optional(), response_code: z.string(), response_summary: z.string().optional(), authorization_type: z.enum(["Final", "Estimated", "Incremental"]).optional(), reference: z.string().optional(), amount_allocations: amountAllocations.optional(), processing: z.object({retrieval_reference_number: z.string().optional(), acquirer_reference_number: z.string().optional(), acquirer_transaction_id: z.string().optional(), capture_action_id: z.string().optional()}).optional(), items: items.optional(), metadata: z.object({}).optional()}); export const paymentActionsResponse = z.array(paymentAction); export const cardPayoutActionsResponse = z.array(paymentAction); export const getApiPaymentsIdActionsResponse = z.union([paymentActionsResponse, cardPayoutActionsResponse]); 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 }; /** * Get payment actions * * GET /payments/{id}/actions * * Also accepts auth: ApiSecretKey */ export const getApiPaymentsIdActions = async (args: GetApiPaymentsIdActionsArgs, options: GetApiPaymentsIdActionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/${encodeURIComponent(String(args.id))}/actions${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(getApiPaymentsIdActionsResponse, await res.json()) };