import {z} from 'zod' export type CardId = string; export type ApplicationSelection = 'credit' | 'debit'; export type Card = {cardId: CardId, applicationSelection?: ApplicationSelection | undefined}; export type Currency = string; export type Money = {amount?: number | undefined, currency?: Currency | undefined}; export type TransactionType = 'RETAIL' | 'CASH' | 'ATM' | 'CREDIT' | 'UNIQUE' | 'P2P_CREDIT' | 'P2P_DEBIT' | 'BALANCE_INQUIRY' | 'CASH_DISBURSEMENT'; export type CardEntryMode = 'MANUAL_ENTRY' | 'MAGNETIC_STRIPE_READ' | 'CHIP_READ' | 'CONTACTLESS' | 'ELECTRONIC_COMMERCE'; export type TransactionData = {transactionAmount: Money, settlementAmount?: Money | undefined, transactionType: TransactionType, cardEntryMode: CardEntryMode}; export type ThreeDsData = {authenticationValue?: string | undefined, eci?: string | undefined, dsTransactionId?: string | undefined, protocolVersion?: ('2.1.0' | '2.2.0') | undefined}; export type MerchantId = string; export type SubMerchantId = string; export type MerchantName = string; export type MerchantCity = string; export type Country = string; export type MerchantCategoryCode = string; export type AcquirerId = string; export type TerminalId = string; export type PartialApprovalCapable = boolean; export type MerchantData = {merchantId: MerchantId, subMerchantId?: SubMerchantId | undefined, merchantName: MerchantName, merchantCity: MerchantCity, merchantCountry: Country, merchantCategoryCode: MerchantCategoryCode, acquirerId: AcquirerId, acquirerCountry: Country, terminalId: TerminalId, partialApprovalCapable: PartialApprovalCapable}; export type AuthRequest = {card: Card, transactionData: TransactionData, threeDsData?: ThreeDsData | undefined, merchantData: MerchantData}; export type CreateApiV1AuthorizationsRequestArgs = {auditUser: string, body: AuthRequest}; export type CreateApiV1AuthorizationsRequestOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const mti = z.string().max(4); export const docId = z.string().max(18); export const authResponseCode = z.string().max(2); export const retrievalReferenceNumber = z.string().max(12); export const authResponse = z.object({metadata: z.object({mti: mti, id: docId}), transactionData: z.object({authResponseCode: authResponseCode, retrievalReferenceNumber: retrievalReferenceNumber})}); 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 }; /** * Send test authorization request * * POST /v1/authorizations/request */ export const createApiV1AuthorizationsRequest = async (args: CreateApiV1AuthorizationsRequestArgs, options: CreateApiV1AuthorizationsRequestOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.auditUser != null) query.append("auditUser", String(args.auditUser)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/authorizations/request${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(authResponse, await res.json()) };