import {z} from 'zod' export type CreditCard = {first_name: string, last_name: string, full_name?: string | undefined, number: string, verification_value?: string | undefined, month: string, year: string, company?: string | undefined, address1?: string | undefined, address2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined, phone_number?: string | undefined, house_number_or_name?: string | undefined, street?: string | undefined, street_line2?: string | undefined, phone_number_country_code?: string | undefined, phone_number_area_code?: string | undefined, shipping_address1?: string | undefined, shipping_address2?: string | undefined, shipping_city?: string | undefined, shipping_state?: string | undefined, shipping_zip?: string | undefined, shipping_country?: string | undefined, shipping_phone_number?: string | undefined, shipping_house_number_or_name?: string | undefined, shipping_street?: string | undefined, shipping_street_line2?: string | undefined, shipping_phone_number_country_code?: string | undefined, shipping_phone_number_area_code?: string | undefined}; export type BankAccount = {first_name: string, last_name: string, bank_routing_number: string, bank_account_number: string, bank_account_type?: ('checking' | 'savings') | undefined, bank_account_holder_type?: ('business' | 'personal') | undefined, full_name?: string | undefined}; export type ApplePay = {payment_data: {version: unknown, data: unknown, signature: unknown, header?: {ephemeralPublicKey: unknown, transactionId: unknown, publicKeyHash: unknown} | undefined}, test_card_number: unknown}; export type GooglePay = {payment_data: {signature?: string | undefined, protocolVersion?: string | undefined, signedMessage?: string | undefined}, test_card_number?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, address_1?: string | undefined, address_2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined}; export type VerifyComposer = {transaction?: ({order_id?: string | undefined, description?: string | undefined, retain_on_success?: string | undefined, ip?: string | undefined, browser_info?: string | undefined, email?: string | undefined, gateway_specific_fields?: Record | undefined, stored_credential_initiator?: string | undefined, stored_credential_reason_type?: string | undefined, sub_merchant_key?: string | undefined, sca_provider_key?: string | undefined, sca_authentication_parameters?: ({three_ds_requestor_challenge_ind?: string | undefined, test_scenario?: boolean | undefined, exemption_type?: Array | undefined, acquiring_bank_fraud_rate?: string | undefined}) | undefined, callback_url?: string | undefined, callback_format?: string | undefined, shipping_address?: ({name?: string | undefined, address1?: string | undefined, address2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined, phone_number?: string | undefined, house_number_or_name?: string | undefined, street?: string | undefined, street_line2?: string | undefined, phone_number_country_code?: string | undefined, phone_number_area_code?: string | undefined}) | undefined, billing_address?: ({name?: string | undefined, address1?: string | undefined, address2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined, phone_number?: string | undefined, house_number_or_name?: string | undefined, street?: string | undefined, street_line2?: string | undefined, phone_number_country_code?: string | undefined, phone_number_area_code?: string | undefined}) | undefined, ignore_failed_authentication_result?: string | undefined, workflow_key?: string | undefined, order_data?: Record | undefined, customer_data?: Record | undefined, risk_data?: Record | undefined, merchant_metadata?: Record | undefined, payment_method_token?: string | undefined, credit_card?: CreditCard | undefined, bank_account?: BankAccount | undefined, apple_pay?: ApplePay | undefined, google_pay?: GooglePay | undefined, sca_authentication_token?: string | undefined}) | undefined}; export type CreateApiTransactionsVerifyArgs = {body: VerifyComposer}; export type CreateApiTransactionsVerifyOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const gatewaySpecificFields = z.array(z.string()); export const paymentSnapshot = z.object({gateway_tokens: z.array(z.string()).optional(), attempts: z.number().int().optional(), messages: z.object({}).optional(), mode: z.string().optional(), custom_error_used: z.boolean().optional(), override_default_error_codes: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), payment_token: z.string().optional(), previous_transaction_tokens: z.array(z.string()).optional()}); export const verifyParameters = z.object({on_test_gateway: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), succeeded: z.boolean().optional(), state: z.string().optional(), token: z.string().optional(), transaction_type: z.string().optional(), order_id: z.string().optional(), ip: z.string().optional(), description: z.string().optional(), email: z.string().optional(), merchant_name_descriptor: z.string().optional(), merchant_location_descriptor: z.string().optional(), merchant_profile_key: z.string().optional(), gateway_specific_fields: gatewaySpecificFields.optional(), gateway_specific_response_fields: z.object({}).optional(), gateway_transaction_id: z.string().optional(), sub_merchant_key: z.string().optional(), gateway_latency_ms: z.string().optional(), warning: z.string().optional(), application_id: z.string().optional(), retain_on_success: z.boolean().optional(), payment_method_added: z.string().optional(), stored_credential_initiator: z.string().optional(), stored_credential_reason_type: z.string().optional(), message_key: z.string().optional(), message: z.string().optional(), gateway_token: z.string().optional(), gateway_type: z.string().optional(), response: z.object({}).optional(), shipping_address: z.object({}).optional(), attempt_3dsecure: z.string().optional(), payment_method: z.object({}).optional(), workflow_key: z.string().optional(), order_data: z.object({}).optional(), customer_data: z.object({}).optional(), risk_data: z.object({}).optional(), merchant_metadata: z.object({}).optional(), gateway_response: z.object({}).optional(), pan_retry: z.boolean().optional(), payment_snapshot: paymentSnapshot.optional()}); export const verifyComposerResponse = z.object({transaction: verifyParameters.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 }; /** * Create a verify with workflows * * POST /transactions/verify */ export const createApiTransactionsVerify = async (args: CreateApiTransactionsVerifyArgs, options?: CreateApiTransactionsVerifyOptions) => { const {baseUrl = "https://core.spreedly.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/verify${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(verifyComposerResponse, await res.json()) };