import {z} from 'zod' export type RequestBillingAddress = {name?: string | undefined, address_line1?: string | undefined, address_line2?: string | undefined, city?: string | undefined, province?: string | undefined, country?: ('AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | 'AO' | 'AQ' | 'AR' | 'AS' | 'AT' | 'AU' | 'AW' | 'AX' | 'AZ' | 'BA' | 'BB' | 'BD' | 'BE' | 'BF' | 'BG' | 'BH' | 'BI' | 'BJ' | 'BL' | 'BM' | 'BN' | 'BO' | 'BQ' | 'BR' | 'BS' | 'BT' | 'BV' | 'BW' | 'BY' | 'BZ' | 'CA' | 'CC' | 'CD' | 'CF' | 'CG' | 'CH' | 'CI' | 'CK' | 'CL' | 'CM' | 'CN' | 'CO' | 'CR' | 'CU' | 'CV' | 'CW' | 'CX' | 'CY' | 'CZ' | 'DE' | 'DJ' | 'DK' | 'DM' | 'DO' | 'DZ' | 'EC' | 'EE' | 'EG' | 'EH' | 'ER' | 'ES' | 'ET' | 'FI' | 'FJ' | 'FK' | 'FM' | 'FO' | 'FR' | 'GA' | 'GB' | 'GD' | 'GE' | 'GF' | 'GG' | 'GH' | 'GI' | 'GL' | 'GM' | 'GN' | 'GP' | 'GQ' | 'GR' | 'GS' | 'GT' | 'GU' | 'GW' | 'GY' | 'HK' | 'HM' | 'HN' | 'HR' | 'HT' | 'HU' | 'ID' | 'IE' | 'IL' | 'IM' | 'IN' | 'IO' | 'IQ' | 'IR' | 'IS' | 'IT' | 'JE' | 'JM' | 'JO' | 'JP' | 'KE' | 'KG' | 'KH' | 'KI' | 'KM' | 'KN' | 'KP' | 'KR' | 'KW' | 'KY' | 'KZ' | 'LA' | 'LB' | 'LC' | 'LI' | 'LK' | 'LR' | 'LS' | 'LT' | 'LU' | 'LV' | 'LY' | 'MA' | 'MC' | 'MD' | 'ME' | 'MF' | 'MG' | 'MH' | 'MK' | 'ML' | 'MM' | 'MN' | 'MO' | 'MP' | 'MQ' | 'MR' | 'MS' | 'MT' | 'MU' | 'MV' | 'MW' | 'MX' | 'MY' | 'MZ' | 'NA' | 'NC' | 'NE' | 'NF' | 'NG' | 'NI' | 'NL' | 'NO' | 'NP' | 'NR' | 'NU' | 'NZ' | 'OM' | 'PA' | 'PE' | 'PF' | 'PG' | 'PH' | 'PK' | 'PL' | 'PM' | 'PN' | 'PR' | 'PS' | 'PT' | 'PW' | 'PY' | 'QA' | 'RE' | 'RO' | 'RS' | 'RU' | 'RW' | 'SA' | 'SB' | 'SC' | 'SD' | 'SE' | 'SG' | 'SH' | 'SI' | 'SJ' | 'SK' | 'SL' | 'SM' | 'SN' | 'SO' | 'SR' | 'SS' | 'ST' | 'SV' | 'SX' | 'SY' | 'SZ' | 'TC' | 'TD' | 'TF' | 'TG' | 'TH' | 'TJ' | 'TK' | 'TL' | 'TM' | 'TN' | 'TO' | 'TR' | 'TT' | 'TV' | 'TW' | 'TZ' | 'UA' | 'UG' | 'UM' | 'US' | 'UY' | 'UZ' | 'VA' | 'VC' | 'VE' | 'VG' | 'VI' | 'VN' | 'VU' | 'WF' | 'WS' | 'YE' | 'YT' | 'ZA' | 'ZM' | 'ZW') | undefined, postal_code?: string | undefined, phone_number?: string | undefined, phone_country_code?: string | undefined, phone_type?: ('m' | 'h' | 'w') | undefined, email_address?: string | undefined}; export type Address = {name?: string | undefined, address_line1?: string | undefined, address_line2?: string | undefined, city?: string | undefined, province?: string | undefined, country?: ('AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | 'AO' | 'AQ' | 'AR' | 'AS' | 'AT' | 'AU' | 'AW' | 'AX' | 'AZ' | 'BA' | 'BB' | 'BD' | 'BE' | 'BF' | 'BG' | 'BH' | 'BI' | 'BJ' | 'BL' | 'BM' | 'BN' | 'BO' | 'BQ' | 'BR' | 'BS' | 'BT' | 'BV' | 'BW' | 'BY' | 'BZ' | 'CA' | 'CC' | 'CD' | 'CF' | 'CG' | 'CH' | 'CI' | 'CK' | 'CL' | 'CM' | 'CN' | 'CO' | 'CR' | 'CU' | 'CV' | 'CW' | 'CX' | 'CY' | 'CZ' | 'DE' | 'DJ' | 'DK' | 'DM' | 'DO' | 'DZ' | 'EC' | 'EE' | 'EG' | 'EH' | 'ER' | 'ES' | 'ET' | 'FI' | 'FJ' | 'FK' | 'FM' | 'FO' | 'FR' | 'GA' | 'GB' | 'GD' | 'GE' | 'GF' | 'GG' | 'GH' | 'GI' | 'GL' | 'GM' | 'GN' | 'GP' | 'GQ' | 'GR' | 'GS' | 'GT' | 'GU' | 'GW' | 'GY' | 'HK' | 'HM' | 'HN' | 'HR' | 'HT' | 'HU' | 'ID' | 'IE' | 'IL' | 'IM' | 'IN' | 'IO' | 'IQ' | 'IR' | 'IS' | 'IT' | 'JE' | 'JM' | 'JO' | 'JP' | 'KE' | 'KG' | 'KH' | 'KI' | 'KM' | 'KN' | 'KP' | 'KR' | 'KW' | 'KY' | 'KZ' | 'LA' | 'LB' | 'LC' | 'LI' | 'LK' | 'LR' | 'LS' | 'LT' | 'LU' | 'LV' | 'LY' | 'MA' | 'MC' | 'MD' | 'ME' | 'MF' | 'MG' | 'MH' | 'MK' | 'ML' | 'MM' | 'MN' | 'MO' | 'MP' | 'MQ' | 'MR' | 'MS' | 'MT' | 'MU' | 'MV' | 'MW' | 'MX' | 'MY' | 'MZ' | 'NA' | 'NC' | 'NE' | 'NF' | 'NG' | 'NI' | 'NL' | 'NO' | 'NP' | 'NR' | 'NU' | 'NZ' | 'OM' | 'PA' | 'PE' | 'PF' | 'PG' | 'PH' | 'PK' | 'PL' | 'PM' | 'PN' | 'PR' | 'PS' | 'PT' | 'PW' | 'PY' | 'QA' | 'RE' | 'RO' | 'RS' | 'RU' | 'RW' | 'SA' | 'SB' | 'SC' | 'SD' | 'SE' | 'SG' | 'SH' | 'SI' | 'SJ' | 'SK' | 'SL' | 'SM' | 'SN' | 'SO' | 'SR' | 'SS' | 'ST' | 'SV' | 'SX' | 'SY' | 'SZ' | 'TC' | 'TD' | 'TF' | 'TG' | 'TH' | 'TJ' | 'TK' | 'TL' | 'TM' | 'TN' | 'TO' | 'TR' | 'TT' | 'TV' | 'TW' | 'TZ' | 'UA' | 'UG' | 'UM' | 'US' | 'UY' | 'UZ' | 'VA' | 'VC' | 'VE' | 'VG' | 'VI' | 'VN' | 'VU' | 'WF' | 'WS' | 'YE' | 'YT' | 'ZA' | 'ZM' | 'ZW') | undefined, postal_code?: string | undefined, phone_number?: string | undefined, phone_country_code?: string | undefined, email_address?: string | undefined}; export type Custom = {ref1?: string | undefined, ref2?: string | undefined, ref3?: string | undefined, ref4?: string | undefined, ref5?: string | undefined}; export type BrowserData = {accept_header: string, java_enabled: boolean, language: string, color_depth?: number | undefined, screen_height: number, screen_width: number, time_zone: number, user_agent: string, javascript_enabled: boolean}; export type Type3DSecure = {browser?: BrowserData | undefined, enabled: boolean, version?: number | undefined, auth_required?: boolean | undefined, disable_method_url?: boolean | undefined, threeDS_server_transaction_id?: string | undefined, xid?: string | undefined, cavv?: string | undefined, eci?: number | undefined}; export type Card = {number: string, name: string, expiry_month: string, expiry_year: string, cvd?: string | undefined, '3d_secure'?: Type3DSecure | undefined, visa_checkout_call_id?: string | undefined, complete?: boolean | undefined}; export type ApplePay = {passthrough?: boolean | undefined, apple_pay_merchant_id: string, payment_token: string, complete?: boolean | undefined}; export type GooglePay = {name?: string | undefined, '3d_secure'?: Type3DSecure | undefined, transaction_payload: string, complete?: boolean | undefined}; export type ProfilePurchase = {'3d_secure'?: Type3DSecure | undefined, complete?: boolean | undefined, customer_code: string, card_id: number}; export type TokenPurchase = {complete?: boolean | undefined, code: string, name: string, '3d_secure'?: Type3DSecure | undefined, visa_checkout_call_id?: string | undefined}; export type Type3dSecureToken = {complete?: boolean | undefined, threeDS_session_data: string, auth_required?: boolean | undefined, disable_method_url?: boolean | undefined, threeDS_server_transaction_id?: string | undefined}; export type Level2 = {customer_code: string, order_tax1_price: number, order_tax2_price: number}; export type CardOnFile = {type: 'not_card_on_file' | 'first_installment' | 'subsequent_installment' | 'first_recurring' | 'subsequent_recurring' | 'subsequent_customer_initiated' | 'subsequent_unscheduled', series_id?: number | undefined}; export type ProcessorTokenRequest = {payment_token: string, expiry_month: string, expiry_year: string}; export type PaymentRequest = {order_number?: string | undefined, amount: number, payment_method: 'card' | 'token' | 'payment_profile' | 'cash' | 'cheque' | 'apple_pay' | 'google_pay' | '3d_secure_token' | 'processor_token', ship_same_as_ord?: boolean | undefined, device_channel?: ('02' | '03') | undefined, language?: string | undefined, customer_ip?: string | undefined, term_url?: string | undefined, comments?: string | undefined, billing?: RequestBillingAddress | undefined, shipping?: Address | undefined, custom?: Custom | undefined, card?: Card | undefined, apple_pay?: ApplePay | undefined, google_pay?: GooglePay | undefined, payment_profile?: ProfilePurchase | undefined, token?: TokenPurchase | undefined, '3d_secure'?: Type3dSecureToken | undefined, recurring_payment?: boolean | undefined, level2?: Level2 | undefined, card_on_file?: CardOnFile | undefined, processor_token?: ProcessorTokenRequest | undefined}; export type CreateApiPaymentsArgs = {body: PaymentRequest}; export type CreateApiPaymentsOptions = {baseUrl?: string | undefined, Passcode?: string | undefined, fetch?: typeof fetch | undefined}; export const custom = z.object({ref1: z.string().max(256).optional(), ref2: z.string().max(256).optional(), ref3: z.string().max(256).optional(), ref4: z.string().max(256).optional(), ref5: z.string().max(256).optional()}); export const cvdResult = z.number().int(); export const avsResponse = z.object({id: z.enum(["0", "5", "9", "A", "B", "C", "D", "E", "G", "I", "M", "N", "P", "R", "S", "U", "W", "X", "Y", "Z"]).optional(), message: z.string().optional(), processed: z.boolean().optional()}); export const sourceData = z.object({td_token_status: z.string().min(1).max(1).optional()}); export const processorTokenResponse = z.object({payment_token: z.string().min(15).max(19).optional(), reporting_token: z.string().max(23).optional(), status: z.enum(["success", "fail"]).optional(), source: z.string().min(4).max(12).optional(), source_data: sourceData.optional()}); export const cavvResult = z.string(); export const cardPurchaseResponse = z.object({card_type: z.enum(["AM", "DI", "JB", "MC", "NN", "VI", "PV", "MD"]).max(2).optional(), last_four: z.string().max(4).optional(), card_bin: z.string().min(6).max(8).optional(), cvd_result: cvdResult.optional(), eci: z.number().int().optional(), address_match: z.number().int().optional(), postal_result: z.number().int().optional(), avs: avsResponse.optional(), processor_token: processorTokenResponse.optional(), cavv_result: cavvResult.optional(), status_id: z.number().int().optional(), status: z.enum(["1", "2", "3", "4"]).optional(), error_id: z.number().int().optional(), error: z.enum(["1", "2", "3"]).optional()}); export const schema3DSecureResponse = z.object({status: z.string(), downgraded: z.boolean().optional(), reason_merchant: z.string().optional(), reason_cardholder: z.string().optional(), method_url: z.string().optional()}); export const link = z.object({ref: z.string().optional(), href: z.string().max(256).optional(), method: z.string().optional()}); export const cardOnFileResponse = z.object({type: z.enum(["not_card_on_file", "first_installment", "subsequent_installment", "first_recurring", "subsequent_recurring", "subsequent_customer_initiated", "subsequent_unscheduled"]).optional(), series_id: z.number().optional()}); export const paymentResponse = z.object({id: z.string().max(9).optional(), authorizing_merchant_id: z.number().int().optional(), approved: z.string().optional(), message_id: z.number().int().optional(), message: z.string().max(256).optional(), auth_code: z.string().max(32).optional(), created: z.string().optional(), order_number: z.string().max(32).optional(), type: z.enum(["P", "R", "VP", "VR", "PA", "PAC"]).max(16).optional(), risk_score: z.number().optional(), amount: z.number().optional(), payment_method: z.enum(["card", "token", "payment_profile", "cash", "cheque", "apple_pay", "google_pay"]).optional(), custom: custom.optional(), card: cardPurchaseResponse.optional(), "3d_secure": schema3DSecureResponse.optional(), merchant_data: z.string().max(32).optional(), contents: z.string().optional(), links: z.array(link).optional(), card_on_file: cardOnFileResponse.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 }; /** * Make payment * * POST /payments * * Also accepts auth: Sub-Merchant-Id */ export const createApiPayments = async (args: CreateApiPaymentsArgs, options?: CreateApiPaymentsOptions) => { const {baseUrl = "https://api.na.bambora.com/v1", fetch: fetchFn = fetch, Passcode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Passcode !== undefined) headers.set("Authorization", Passcode) const url = `${baseUrl.replace(/\/+$/, '')}/payments${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(paymentResponse, await res.json()) };