import {z} from 'zod' export type StringMap = {key?: string | undefined, value?: string | undefined}; export type WalletTransactionRequest = {type: 'TOPUP' | 'VOID', paid_credits?: string | undefined, credits_to_void?: string | undefined, granted_credits?: string | undefined, metadata?: Array | undefined}; export type CreateApiWalletsWalletIdWalletTransactionsArgs = {wallet_id: string, body: WalletTransactionRequest}; export type CreateApiWalletsWalletIdWalletTransactionsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const walletTransactionPaymentStatus = z.enum(["PENDING", "SETTLED", "FAILED"]); export const walletTransactionSource = z.enum(["MANUAL", "INTERVAL", "THRESHOLD"]); export const walletTransactionTransactionStatus = z.enum(["PURCHASED", "GRANTED", "VOIDED", "INVOICED"]); export const walletTransactionType = z.enum(["INBOUND", "OUTBOUND"]); export const currency = z.string().min(3).max(3); export const dateTimeWithZone = z.string(); export const stringMap = z.object({key: z.string().optional(), value: z.string().optional()}); export const walletTransaction = z.object({wallet_transaction_id: z.string().optional(), wallet_id: z.string().optional(), payment_status: walletTransactionPaymentStatus.optional(), source: walletTransactionSource.optional(), transaction_status: walletTransactionTransactionStatus.optional(), transaction_type: walletTransactionType.optional(), amount: z.object({value: z.number().gte(0), currency_code: currency}).optional(), credits: z.string().optional(), settled_at: dateTimeWithZone.optional(), failed_at: dateTimeWithZone.optional(), created_at: dateTimeWithZone.optional(), metadata: z.array(stringMap).optional()}); export const walletTransactions = z.object({wallet_transactions: z.array(walletTransaction).optional(), meta: z.object({total_count: z.number().int().optional(), total_pages: z.number().int().optional(), current_page: z.number().int().optional(), next_page: z.number().int().optional(), prev_page: z.number().int().optional()}).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 Wallet transaction * * POST /wallets/{wallet_id}/wallet-transactions */ export const createApiWalletsWalletIdWalletTransactions = async (args: CreateApiWalletsWalletIdWalletTransactionsArgs, options?: CreateApiWalletsWalletIdWalletTransactionsOptions) => { const {baseUrl = "https://api-m.sandbox.paypal.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/wallets/${encodeURIComponent(String(args.wallet_id))}/wallet-transactions${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(walletTransactions, await res.json()) };