import {z} from 'zod' export type TokenId = string; export type TransactionType = 'PURCHASE' | 'REFUND'; export type FirstAuthenticationFactor = 'LOGIN_PASSWORD' | 'DEVICE_UNLOCK' | 'OTP_SMS' | 'OTP_EMAIL' | 'FIDO_BIOMETRIC' | 'FIDO_PASSCODE' | 'FIDO_NO_AUTH' | 'VISA_CTF_BINDING'; export type SecondAuthenticationFactor = 'LOGIN_PASSWORD' | 'DEVICE_UNLOCK' | 'OTP_SMS' | 'OTP_EMAIL' | 'FIDO_BIOMETRIC' | 'FIDO_PASSCODE' | 'FIDO_NO_AUTH' | 'VISA_CTF_BINDING'; export type AuthenticationTransaction = {firstFactor: FirstAuthenticationFactor, secondFactor?: SecondAuthenticationFactor | undefined}; export type VisaSignedPayload = string; export type Amount = number; export type TransactionData = {amount: Amount, currencyCode: string}; export type DeviceIdDeprecated = string; export type IpAddressSimpleDesc = string; export type Location = string; export type DeviceTransaction = {deviceId?: DeviceIdDeprecated | undefined, ipAddress: IpAddressSimpleDesc, location?: Location | undefined}; export type CardholderEmail = string; export type CardholderLine1 = string; export type CardholderLine2 = string; export type CardholderCity = string; export type CardholderPostalCode = string; export type Country = string; export type CardholderSubdivision = string; export type CardholderBillingAddress = {line1: CardholderLine1, line2?: CardholderLine2 | undefined, city: CardholderCity, postalCode: CardholderPostalCode, country: Country, subdivision?: CardholderSubdivision | undefined}; export type CardholderTransaction = {email: CardholderEmail, billingAddress?: CardholderBillingAddress | undefined}; export type CreateApiTransactionsArgs = {authorization: string, 'x-correlation-id': string, body: {tokenId: TokenId, transactionType: TransactionType, authentication?: AuthenticationTransaction | undefined, visaSignedPayload?: VisaSignedPayload | undefined, transactionData?: TransactionData | undefined, device?: DeviceTransaction | undefined, cardholder?: CardholderTransaction | undefined}}; export type CreateApiTransactionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionId = z.string().min(1).max(128); export const encryptedPaymentCredentials = z.string().max(8196); export const eci = z.string().min(2).max(2); export const cryptogramExpiryDate = z.string().max(64); export const isDAF = z.boolean(); export const createApiTransactionsResponse = z.object({transactionId: transactionId, encryptedData: encryptedPaymentCredentials, eci: eci.optional(), cryptogramExpiryDate: cryptogramExpiryDate.optional(), isDAF: isDAF.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 Transaction * * POST /transactions */ export const createApiTransactions = async (args: CreateApiTransactionsArgs, options?: CreateApiTransactionsOptions) => { const {baseUrl = "https://api.d1.thalescloud.io/authz/v1", 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.authorization != null) headers.set("authorization", String(args.authorization)) if (args['x-correlation-id'] != null) headers.set("x-correlation-id", String(args['x-correlation-id'])) const url = `${baseUrl.replace(/\/+$/, '')}/transactions${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(createApiTransactionsResponse, await res.json()) };