import {z} from 'zod' export type Address = {line_1?: string | undefined, line_2?: string | undefined, city?: string | undefined, state?: string | undefined, postcode?: string | undefined, country_code?: ('al' | 'al' | 'dz' | 'as' | 'ad' | 'ao' | 'ai' | 'aq' | 'ag' | 'ar' | 'am' | 'aw' | 'au' | 'at' | 'az' | 'bs' | 'bh' | 'bd' | 'bb' | 'by' | 'be' | 'bz' | 'bj' | 'bm' | 'bt' | 'bo' | 'ba' | 'bw' | 'br' | 'io' | 'bn' | 'bg' | 'bf' | 'bi' | 'cv' | 'kh' | 'cm' | 'ca' | 'ky' | 'cf' | 'td' | 'cl' | 'cn' | 'co' | 'km' | 'cg' | 'cd' | 'cr' | 'hr' | 'cu' | 'cy' | 'cz' | 'dk' | 'dj' | 'dm' | 'do' | 'ec' | 'eg' | 'sv' | 'gq' | 'er' | 'ee' | 'sz' | 'et' | 'fk' | 'fo' | 'fj' | 'fi' | 'fr' | 'gf' | 'pf' | 'ga' | 'gm' | 'ge' | 'de' | 'gh' | 'gi' | 'gr' | 'gl' | 'gd' | 'gu' | 'gt' | 'gg' | 'gn' | 'gw' | 'gy' | 'ht' | 'hn' | 'hk' | 'hu' | 'is' | 'in' | 'id' | 'ir' | 'iq' | 'ie' | 'im' | 'il' | 'it' | 'jm' | 'jp' | 'je' | 'jo' | 'kz' | 'ke' | 'ki' | 'kp' | 'kr' | 'kw' | 'kg' | 'la' | 'lv' | 'lb' | 'ls' | 'lr' | 'ly' | 'li' | 'lt' | 'lu' | 'mo' | 'mk' | 'mg' | 'mw' | 'my' | 'mv' | 'ml' | 'mt' | 'mh' | 'mq' | 'mr' | 'mu' | 'yt' | 'mx' | 'fm' | 'md' | 'mc' | 'mn' | 'me' | 'ms' | 'ma' | 'mz' | 'mm' | 'na' | 'nr' | 'np' | 'nl' | 'nc' | 'nz' | 'ni' | 'ne' | 'ng' | 'nu' | 'nf' | 'mp' | 'no' | 'om' | 'pk' | 'pw' | 'ps' | 'pa' | 'pg' | 'py' | 'pe' | 'ph' | 'pl' | 'pt' | 'pr' | 'qa' | 'ro' | 'ru' | 'rw' | 're' | 'bl' | 'sh' | 'kn' | 'lc' | 'mf' | 'pm' | 'vc' | 'ws' | 'sm' | 'st' | 'sa' | 'sn' | 'rs' | 'sc' | 'sl' | 'sg' | 'sx' | 'sk' | 'si' | 'sb' | 'so' | 'za' | 'gs' | 'ss' | 'es' | 'lk' | 'sd' | 'sr' | 'se' | 'ch' | 'sx' | 'sy' | 'tw' | 'tj' | 'tz' | 'th' | 'tg' | 'tk' | 'to' | 'tt' | 'tn' | 'tr' | 'tm' | 'tc' | 'tv' | 'ug' | 'ua' | 'ae' | 'gb' | 'us' | 'uy' | 'uz' | 'vu' | 've' | 'vn' | 'vg' | 'vi' | 'wf' | 'eh' | 'ye' | 'zm' | 'zw') | undefined}; export type Phone = {country_code?: string | undefined, number?: string | undefined}; export type CustomerRequest = {customer_id?: string | undefined, reference?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, dob?: string | undefined, custom_data?: string | undefined, billing?: ({address?: Address | undefined, email?: string | undefined, phone?: Phone | undefined}) | undefined, shipping?: ({address_match?: boolean | undefined, first_name?: string | undefined, last_name?: string | undefined, address?: Address | undefined, email?: string | undefined, phone?: Phone | undefined}) | undefined}; export type CreateApiPaymentsRecurringArgs = {'Company-Id'?: string | undefined, Mid?: string | undefined, body: {transaction?: ({order_id: string, amount: number, currency: 'AED' | 'AUD' | 'CAD' | 'CHF' | 'CNY' | 'DKK' | 'EUR' | 'GBP' | 'HKD' | 'JPY' | 'MXN' | 'SEK' | 'USD' | 'ZAR', capture?: boolean | undefined, custom_data?: string | undefined, custom1?: string | undefined, custom2?: string | undefined}) | undefined, payment?: ({card_id: string, reference?: string | undefined, subscription_reason?: ('recurring' | 'instalment' | 'unscheduled' | 're-submission' | 're-authorisation' | 'delayed charge' | 'no-show') | undefined, scheme_reference_data?: string | undefined, transaction_link_id?: string | undefined}) | undefined, customer?: CustomerRequest | undefined}}; export type CreateApiPaymentsRecurringOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const cardReasons = z.enum(["6012_data", "above_max_amount", "above_max_refund_amount", "above_max_transactions", "acs_technical_issue", "bad_gateway", "below_min_amount", "below_min_refund_amount", "bin_bank", "bin_country", "bin_level", "bin_product", "bin_scheme", "blocklist_bin", "blocklist_card", "blocklist_email", "blocklist_ip", "blocklist_phone", "card_authentication_failed", "card_not_active", "cardholder_cancellation", "cardholder_cancelled", "challenge_failed", "challenge_limit_exceeded", "challenge_not_completed", "challenge_time_out", "closed_account", "exceeds_authentication_frequency_limit", "exceeds_max_amount", "exceeds_max_refund_amount", "exceeds_max_transactions", "expired_card", "fraud_scoring", "fraud_scoring_error", "generic_decline", "insufficient_funds", "invalid_avs", "invalid_card_number", "invalid_cvv", "invalid_pin", "invalid_transaction", "low_confidence", "lost_or_stolen", "max_amount_day", "max_amount_month", "max_card_attempts", "max_card_success", "max_daily_refunds_amount", "max_daily_refunds_count", "max_email_day", "max_email_month", "max_ip_day", "max_ip_month", "max_monthly_refunds_amount", "max_monthly_refunds_count", "max_phone_day", "max_phone_month", "max_post_day", "max_post_month", "max_refunds_count", "medium_confidence", "no_card_record", "restricted_card", "scheme_status_reason", "security_failure", "suspected_fraud", "transaction_disputed", "transaction_fraud", "transaction_not_permitted_to_cardholder"]); export const issuerResponseCodes = z.enum(["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5C", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6P", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9G", "B1", "N0", "N3", "N4", "N7", "P5", "P6", "R0", "R1", "R3", "XA", "XD"]); export const retryAdvice = z.object({category: z.enum(["update_credentials", "retry_later", "do_not_retry", "token_requirements_not_met", "cancelled", "not_eligible", "scheme_blocked", "card_product_limitations", "customer_action_required", "update_details", "unknown"]).optional(), detail: z.enum(["lifecycle", "policy", "fraud_security", "non_reloadable_prepaid", "single_use_virtual_card", "multi_use_virtual_card", "card_product_not_supported_by_merchant"]).nullable().optional(), retry_after: z.string().nullable().optional(), acquirer_code: z.string().nullable().optional()}).nullable(); export const createApiPaymentsRecurringResponse = z.object({transaction_id: z.string().optional(), status: z.enum(["success", "declined", "blocked"]).optional(), reason: cardReasons.optional(), issuer_response_code: issuerResponseCodes.optional(), check: z.object({avs_line1: z.enum(["matched", "not_matched", "not_checked", "not_provided"]).optional(), avs_postcode: z.enum(["matched", "not_matched", "not_checked", "not_provided"]).optional()}).optional(), retry_advice: retryAdvice.optional(), links: z.array(z.object({rel: z.literal("self").optional(), href: z.string().optional()})).optional(), transaction_link_id: z.string().max(36).nullable().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 }; /** * Recurring payments * * POST /payments/recurring */ export const createApiPaymentsRecurring = async (args: CreateApiPaymentsRecurringArgs, options?: CreateApiPaymentsRecurringOptions) => { const {baseUrl = "https://test-api.acquired.com/v1", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Company-Id'] != null) headers.set("Company-Id", String(args['Company-Id'])) if (args.Mid != null) headers.set("Mid", String(args.Mid)) if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/recurring${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(createApiPaymentsRecurringResponse, await res.json()) };