import {z} from 'zod' export type GetApiMerchantRulesetsRulesetIdVersionsVersionArgs = {rulesetId: string, version: number}; export type GetApiMerchantRulesetsRulesetIdVersionsVersionOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMerchantRulesetsRulesetIdVersionsVersionResponse = z.object({createdAt: z.string().optional(), id: z.string(), version: z.number(), trigger: z.object({on: z.enum(["determinePaymentOptions", "determineProviderOptions", "repeat"]), filter: z.union([z.object({paymentMethodCode: z.enum(["alexBankMa7fazty", "amazonPay", "applePay", "audi2pay", "alfa", "alipay", "bankTransfer", "card", "capitecPay", "cibSmartWallet", "easypaisa", "etisalatCash", "fawryMobileWallet", "fawryPay", "googlePay", "jazzCash", "mercadoPago", "monoDirectDebit", "nbePhoneCash", "orangeCash", "oPayWallet", "pagaWallet", "payflex", "payjustnow", "payPal", "pix", "konnect", "qnbEWallet", "wafaCashWallet", "weCash", "genericRedirect", "eftPro", "upi", "cashFreeWallet", "paytmMWallet", "netBanking", "meezaWallet", "vodafoneCash", "alipayQRCode", "sepaDirectDebit", "dcb", "bankAccount", "modo", "ach", "payzoneCash", "revolutPay", "lean", "tabby", "boleto", "oxxo", "spei", "promptPay", "qris", "momoVN", "vietQR", "konbini", "touchNGo", "gCash", "maya", "pse", "pagoEfectivo", "napas", "duitNowQR", "grabPay", "shopeePay", "nequi", "picPay", "ovo", "trueMoney"]).optional()}), z.object({workflowNode: z.enum(["authorize", "startPaymentSession"]).optional(), providerConfigId: z.string().optional()}), z.object({workflowNode: z.literal("authorize").optional()})]).optional()}), rules: z.array(z.object({condition: z.union([z.object({field: z.string(), empty: z.boolean()}), z.object({field: z.string(), operator: z.enum(["=", ">", ">=", "<", "<=", "IN", "IS ONE OF", "CONTAINS", "STARTS WITH", "ENDS WITH"]), value: z.union([z.string(), z.number(), z.boolean(), z.array(z.union([z.string(), z.number()]))]), referenceField: z.string().optional()}), z.object({field: z.string(), operator: z.enum(["=", ">", ">=", "<", "<=", "IN", "IS ONE OF", "CONTAINS", "STARTS WITH", "ENDS WITH"]), value: z.union([z.string(), z.number(), z.boolean(), z.array(z.union([z.string(), z.number()]))]).optional(), referenceField: z.string()}), z.object({left: z.object({}), operator: z.enum(["AND", "OR"]), right: z.object({})}), z.object({operator: z.literal("NOT"), condition: z.object({}).optional()})]), action: z.union([z.object({type: z.literal("setCaptureMode"), params: z.object({mode: z.enum(["Instant", "Delayed", "Manual"])})}), z.object({type: z.literal("setCancelMode"), params: z.object({mode: z.enum(["Delayed", "Manual"])})}), z.object({type: z.literal("setDelay"), params: z.object({delay: z.string().optional()})}), z.object({type: z.literal("setPriority"), params: z.object({priority: z.number()})}), z.object({type: z.literal("skip")}), z.object({type: z.literal("stop")}), z.object({type: z.literal("setFutureUsage"), params: z.object({futureUsage: z.enum(["Subscription", "CardOnFile", "UnscheduledCardOnFile"])})}), z.object({type: z.literal("addNextStep"), params: z.object({code: z.string(), mapping: z.array(z.object({from: z.string().optional(), to: z.string(), value: z.unknown()})).optional()})}), z.object({type: z.literal("addWorkflowExecutionStatus"), params: z.object({status: z.string()})})])})).optional(), defaultAction: z.union([z.object({type: z.literal("setCaptureMode"), params: z.object({mode: z.enum(["Instant", "Delayed", "Manual"])})}), z.object({type: z.literal("setCancelMode"), params: z.object({mode: z.enum(["Delayed", "Manual"])})}), z.object({type: z.literal("setDelay"), params: z.object({delay: z.string().optional()})}), z.object({type: z.literal("setPriority"), params: z.object({priority: z.number()})}), z.object({type: z.literal("skip")}), z.object({type: z.literal("stop")}), z.object({type: z.literal("setFutureUsage"), params: z.object({futureUsage: z.enum(["Subscription", "CardOnFile", "UnscheduledCardOnFile"])})}), z.object({type: z.literal("addNextStep"), params: z.object({code: z.string(), mapping: z.array(z.object({from: z.string().optional(), to: z.string(), value: z.unknown()})).optional()})}), z.object({type: z.literal("addWorkflowExecutionStatus"), params: z.object({status: z.string()})})])}); 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 a Ruleset * * GET /merchant/rulesets/{rulesetId}/versions/{version} */ export const getApiMerchantRulesetsRulesetIdVersionsVersion = async (args: GetApiMerchantRulesetsRulesetIdVersionsVersionArgs, options?: GetApiMerchantRulesetsRulesetIdVersionsVersionOptions) => { const {baseUrl = "https://api.staging.payrails.io", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/merchant/rulesets/${encodeURIComponent(String(args.rulesetId))}/versions/${encodeURIComponent(String(args.version))}${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(getApiMerchantRulesetsRulesetIdVersionsVersionResponse, await res.json()) };