import {z} from 'zod' export type EcommerceTransactionPaymentsPublicAmount = {currency: 'AED' | 'AFN' | 'ALL' | 'AMD' | 'ANG' | 'AOA' | 'ARS' | 'AUD' | 'AWG' | 'AZN' | 'BAM' | 'BBD' | 'BDT' | 'BGN' | 'BHD' | 'BIF' | 'BMD' | 'BND' | 'BOB' | 'BOV' | 'BRL' | 'BSD' | 'BTN' | 'BWP' | 'BYN' | 'BYR' | 'BZD' | 'CAD' | 'CDF' | 'CHE' | 'CHF' | 'CHW' | 'CLF' | 'CLP' | 'CNY' | 'COP' | 'COU' | 'CRC' | 'CUC' | 'CUP' | 'CVE' | 'CZK' | 'DJF' | 'DKK' | 'DOP' | 'DZD' | 'EGP' | 'ERN' | 'ETB' | 'EUR' | 'FJD' | 'FKP' | 'GBP' | 'GEL' | 'GHS' | 'GIP' | 'GMD' | 'GNF' | 'GTQ' | 'GYD' | 'HKD' | 'HNL' | 'HRK' | 'HTG' | 'HUF' | 'IDR' | 'ILS' | 'INR' | 'IQD' | 'IRR' | 'ISK' | 'JMD' | 'JOD' | 'JPY' | 'KES' | 'KGS' | 'KHR' | 'KMF' | 'KPW' | 'KRW' | 'KWD' | 'KYD' | 'KZT' | 'LAK' | 'LBP' | 'LKR' | 'LRD' | 'LSL' | 'LYD' | 'MAD' | 'MDL' | 'MGA' | 'MKD' | 'MMK' | 'MNT' | 'MOP' | 'MRO' | 'MRU' | 'MUR' | 'MVR' | 'MWK' | 'MXN' | 'MXV' | 'MYR' | 'MZN' | 'NAD' | 'NGN' | 'NIO' | 'NOK' | 'NPR' | 'NZD' | 'OMR' | 'PAB' | 'PEN' | 'PGK' | 'PHP' | 'PKR' | 'PLN' | 'PYG' | 'QAR' | 'RON' | 'RSD' | 'RUB' | 'RWF' | 'SAR' | 'SBD' | 'SCR' | 'SDG' | 'SEK' | 'SGD' | 'SHP' | 'SLE' | 'SLL' | 'SOS' | 'SRD' | 'SSP' | 'STD' | 'STN' | 'SVC' | 'SYP' | 'SZL' | 'THB' | 'TJS' | 'TMT' | 'TND' | 'TOP' | 'TRY' | 'TTD' | 'TWD' | 'TZS' | 'UAH' | 'UGX' | 'USD' | 'USN' | 'UYI' | 'UYU' | 'UZS' | 'VED' | 'VEF' | 'VES' | 'VND' | 'VUV' | 'WST' | 'XAF' | 'XCD' | 'XCG' | 'XOF' | 'XPF' | 'YER' | 'ZAR' | 'ZMW' | 'ZWL', value: number}; export type EcommerceTransactionPaymentsPublicAddress = {name?: string | undefined, city?: string | undefined, country: string, state?: string | undefined, street_address_line1?: string | undefined, street_address_line2?: string | undefined, street_address_line3?: string | undefined, zipcode?: string | undefined}; export type EcommerceTransactionPaymentsPublicBrowserData = {accept_language?: string | undefined, ip_address: string, screen_height: number, screen_width: number, user_agent?: string | undefined}; export type EcommerceTransactionPaymentsPublicCardholder = {email?: string | undefined, name: string, phone_number?: string | undefined}; export type EcommerceTransactionPaymentsPublicCustomerContact = {phone_number?: string | undefined, email?: string | undefined}; export type EcommerceTransactionPaymentsPublicLineItem = {description: string, quantity: number, unit_price: number}; export type EcommerceTransactionPaymentsPublicApplePayAuthenticationData = {application_primary_account_number: string, eci_code?: string | undefined, expiry_month: string, expiry_year: string, last_4_card_digits?: string | undefined, online_payment_cryptogram: string}; export type EcommerceTransactionPaymentsPublicCard = {cvc: string, expiry_month: string, expiry_year: string, number: string}; export type EcommerceTransactionPaymentsPublicLegacyToken = {token_id: string, recurring_payment_type: 'CARD_ON_FILE' | 'RECURRING' | 'INSTALLMENT'}; export type EcommerceTransactionPaymentsPublicToken = {token_id: string, recurring_payment_type: 'CARD_ON_FILE' | 'RECURRING' | 'INSTALLMENT', token_type?: ('CARD_TOKEN' | 'WALLET_TOKEN' | null) | undefined}; export type EcommerceTransactionPaymentsPublicPaymentMethod = {apple_pay_authentication_data?: EcommerceTransactionPaymentsPublicApplePayAuthenticationData | undefined, card?: EcommerceTransactionPaymentsPublicCard | undefined, legacy_token?: EcommerceTransactionPaymentsPublicLegacyToken | undefined, token?: EcommerceTransactionPaymentsPublicToken | undefined, type: 'CARD' | 'TOKEN' | 'APPLE_PAY' | 'LEGACY_TOKEN'}; export type EcommerceTransactionPaymentsPublicProcessTransactionRequestV2 = {amount: EcommerceTransactionPaymentsPublicAmount, billing_address?: EcommerceTransactionPaymentsPublicAddress | undefined, browser_data?: EcommerceTransactionPaymentsPublicBrowserData | undefined, cardholder?: EcommerceTransactionPaymentsPublicCardholder | undefined, customer_contact?: EcommerceTransactionPaymentsPublicCustomerContact | undefined, initiator: 'CUSTOMER' | 'MERCHANT', line_items?: Array | undefined, merchant_reference_id?: string | undefined, payment_method: EcommerceTransactionPaymentsPublicPaymentMethod, redirect_url?: string | undefined, shipping_address?: EcommerceTransactionPaymentsPublicAddress | undefined, store_payment_method?: boolean | undefined, store_id: string, type: 'SALE' | 'PRE_AUTHORISATION' | 'ACCOUNT_VERIFICATION'}; export type CreateApiV3TransactionsOnlineArgs = {'Idempotency-Key': string, 'User-Agent': string, body: EcommerceTransactionPaymentsPublicProcessTransactionRequestV2}; export type CreateApiV3TransactionsOnlineOptions = {baseUrl: string, EcommerceTransactionPaymentsPublicOauth?: string | undefined, fetch?: typeof fetch | undefined}; export const ecommerceTransactionPaymentsPublicIssuerResult = z.object({approval_code: z.string().optional(), response_code: z.string().optional()}); export const ecommerceTransactionPaymentsPublicTokenCreationResponse = z.object({brand: z.enum(["AMEX", "CALLIO", "CARDCO", "UNION_PAY", "DINERS", "DISCOVER", "DOXX", "GENERALI", "JCB", "KH", "MAESTRO", "MASTERCARD", "OTHER", "OTP", "PATIKA", "PREMIUM", "SODEXO", "UP", "VISA", "VITAMIN"]).nullable().optional(), failure_reason: z.string().nullable().optional(), last4: z.string().min(4).max(4).nullable().optional(), token_id: z.string().optional(), token_status: z.enum(["SUCCESS", "FAILED"]), token_type: z.enum(["CARD_TOKEN", "WALLET_TOKEN"]).nullable().optional()}); export const ecommerceTransactionPaymentsPublicOnlineTransaction = z.object({authentication_id: z.string(), created_at: z.string(), issuer_result: ecommerceTransactionPaymentsPublicIssuerResult.optional(), status: z.enum(["SUCCESS", "FAILURE", "PENDING"]), status_reason: z.enum(["ACCOUNT_INVALID", "BLOCKED_CARD", "BLOCKED_CROSS_BORDER_TRANSACTION", "AMOUNT_TOO_SMALL", "CANNOT_VALIDATE_AUTHENTICATION", "CARD_COUNT_EXCEEDED", "CARD_DECLINED", "CARD_EXPIRED", "CARD_LIMIT_EXCEEDED", "CARD_NOT_ACTIVE", "DISABLED_ACCOUNT", "DO_NOT_HONOR", "ECOM_NEGATIVE_CARD_AUTHENTICATION_VERIFICATION", "EXCEEDS_AMOUNT_LIMIT", "FORMAT_ERROR", "INCORRECT_CARD_AUTHENTICATION", "INSUFFICIENT_FUNDS", "INVALID_CARD_NUMBER", "INVALID_TRANSACTION", "ISSUER_DECLINED", "LOST_OR_STOLEN", "MERCHANT_CONFIGURATION", "NEGATIVE_BALANCE", "PIN_LIMIT_EXCEEDED", "PROCESSOR_UNAVAILABLE", "RESTRICTED_CARD", "SCA_PIN_REQUIRED", "SCA_REQUIRED", "SUSPECTED_FRAUD", "TEMPORARY_ISSUE_RETRY", "TIMEOUT", "TRANSACTION_NOT_ALLOWED", "UNABLE_TO_ROUTE_TRANSACTION", "UNSUPPORTED_ISSUER", "FEATURE_NOT_ACTIVATED_MANUAL_ENTRY", "FEATURE_NOT_ACTIVATED_PAY_BY_LINK", "FEATURE_NOT_ACTIVATED_ECOMMERCE", "FEATURE_NOT_ACTIVATED_UNREFERENCED_REFUNDS", "OTHER"]).optional(), token_response: ecommerceTransactionPaymentsPublicTokenCreationResponse.optional(), transaction_id: z.string(), updated_at: z.string()}); export const ecommerceTransactionPaymentsPublicRedirectTransactionResponse = z.object({authentication_id: z.string(), created_at: z.string(), redirect_html: z.string(), status: z.enum(["SUCCESS", "FAILURE", "PENDING"]), transaction_id: z.string(), updated_at: z.string()}); export const ecommerceTransactionPaymentsPublicProcessTransactionResponseV2 = z.object({online_transaction: ecommerceTransactionPaymentsPublicOnlineTransaction.optional(), redirect_transaction_response: ecommerceTransactionPaymentsPublicRedirectTransactionResponse.optional(), type: z.enum(["ONLINE_TRANSACTION", "REDIRECT_TRANSACTION_RESPONSE"]).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 }; /** * Process an online transaction v3 * * POST /v3/transactions/online */ export const createApiV3TransactionsOnline = async (args: CreateApiV3TransactionsOnlineArgs, options: CreateApiV3TransactionsOnlineOptions) => { const {baseUrl, fetch: fetchFn = fetch, EcommerceTransactionPaymentsPublicOauth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) if (EcommerceTransactionPaymentsPublicOauth !== undefined) headers.set('authorization', `Bearer ${EcommerceTransactionPaymentsPublicOauth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/transactions/online${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(ecommerceTransactionPaymentsPublicProcessTransactionResponseV2, await res.json()) };