import {z} from 'zod' export type CreateApiMerchantRulesetsArgs = {'x-idempotency-key': string, body: {rulesetConfig?: ({trigger: {on: 'determinePaymentOptions' | 'determineProviderOptions' | 'repeat', filter?: ({paymentMethodCode?: ('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') | undefined} | {workflowNode?: ('authorize' | 'startPaymentSession') | undefined, providerConfigId?: string | undefined} | {workflowNode?: 'authorize' | undefined}) | undefined}, rules?: (Array<{condition: {field: string, empty: boolean} | {field: string, operator: '=' | '>' | '>=' | '<' | '<=' | 'IN' | 'IS ONE OF' | 'CONTAINS' | 'STARTS WITH' | 'ENDS WITH', value: string | number | boolean | Array, referenceField?: string | undefined} | {field: string, operator: '=' | '>' | '>=' | '<' | '<=' | 'IN' | 'IS ONE OF' | 'CONTAINS' | 'STARTS WITH' | 'ENDS WITH', value?: (string | number | boolean | Array) | undefined, referenceField: string} | {left: Record, operator: 'AND' | 'OR', right: Record} | {operator: 'NOT', condition?: Record | undefined}, action: {type: 'setCaptureMode', params: {mode: 'Instant' | 'Delayed' | 'Manual'}} | {type: 'setCancelMode', params: {mode: 'Delayed' | 'Manual'}} | {type: 'setDelay', params: {delay?: unknown | undefined}} | {type: 'setPriority', params: {priority: number}} | {type: 'skip'} | {type: 'stop'} | {type: 'setFutureUsage', params: {futureUsage: 'Subscription' | 'CardOnFile' | 'UnscheduledCardOnFile'}} | {type: 'addNextStep', params: {code: string, mapping?: (Array<{from?: string | undefined, to: string, value: unknown}>) | undefined}} | {type: 'addWorkflowExecutionStatus', params: {status: string}}}>) | undefined, defaultAction: {type: 'setCaptureMode', params: {mode: 'Instant' | 'Delayed' | 'Manual'}} | {type: 'setCancelMode', params: {mode: 'Delayed' | 'Manual'}} | {type: 'setDelay', params: {delay?: unknown | undefined}} | {type: 'setPriority', params: {priority: number}} | {type: 'skip'} | {type: 'stop'} | {type: 'setFutureUsage', params: {futureUsage: 'Subscription' | 'CardOnFile' | 'UnscheduledCardOnFile'}} | {type: 'addNextStep', params: {code: string, mapping?: (Array<{from?: string | undefined, to: string, value: unknown}>) | undefined}} | {type: 'addWorkflowExecutionStatus', params: {status: string}}}) | undefined, workspaceId?: string | undefined}}; export type CreateApiMerchantRulesetsOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiMerchantRulesetsResponse = 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 }; /** * Create a new Ruleset * * POST /merchant/rulesets */ export const createApiMerchantRulesets = async (args: CreateApiMerchantRulesetsArgs, options?: CreateApiMerchantRulesetsOptions) => { const {baseUrl = "https://api.staging.payrails.io", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-idempotency-key'] != null) headers.set("x-idempotency-key", String(args['x-idempotency-key'])) if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/merchant/rulesets${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(createApiMerchantRulesetsResponse, await res.json()) };