import {z} from 'zod' export type GetApiPaymentMethodsArgs = {processing_channel_id: string}; export type GetApiPaymentMethodsOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentMethod = z.object({type: z.enum(["accel", "ach", "alipay_cn", "alipay_hk", "alipay_plus", "alma", "amex", "bancontact", "blik", "boost", "bpi", "card_scheme", "cartes_bancaires", "china_union_pay", "connect_wallet", "bacs", "dana", "dci", "diners", "discover", "eps", "gcash", "ideal", "jcb", "kakaopay", "klarna", "knet", "mada", "mastercard", "mbway", "multibanco", "nyce", "omannet", "p24", "paypal", "paypay", "pulse", "qpay", "rabbit_line_pay", "sepa", "sequra", "shazam", "star", "stcpay", "swish", "tamara", "tng", "truemoney", "upi", "visa", "wechatpay"]), partner_merchant_id: z.string().optional()}); export const paymentMethodsResponse = z.object({methods: z.array(paymentMethod)}); 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 available payment methods * * GET /payment-methods * * Also accepts auth: ApiSecretKey */ export const getApiPaymentMethods = async (args: GetApiPaymentMethodsArgs, options: GetApiPaymentMethodsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.processing_channel_id != null) query.append("processing_channel_id", String(args.processing_channel_id)) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/payment-methods${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(paymentMethodsResponse, await res.json()) };