import {z} from 'zod' export type Header = {requestId: string, service: string, issuerCode: string, subIssuerCode: string, keyTag?: string | undefined, iv?: (string | string) | undefined}; export type Currency = {code: unknown, label: string}; export type Amount = {amount: unknown, exponent: unknown, currency: Currency}; export type Merchant = {id?: string | undefined, name?: string | undefined, url?: string | undefined, country?: unknown | undefined}; export type DynamicLinking = {xid: string, binAcquirer?: string | undefined, browserIP?: string | undefined, cardType?: ('CREDIT' | 'DEBIT') | undefined, convertedAmount?: Amount | undefined, transactionAmount?: Amount | undefined, deviceChannel?: string | undefined, deviceOs?: string | undefined, amountInd?: string | undefined, frequencyInd?: string | undefined, maskedPan?: string | undefined, maskedVPan?: string | undefined, merchant?: Merchant | undefined, messageCategory?: string | undefined, network?: ('BANCONTACT' | 'CB' | 'MAESTRO' | 'MASTERCARD' | 'VISA') | undefined, purchaseDate?: unknown | undefined, purchaseInstalData?: string | undefined, recurringDate?: unknown | undefined, recurringExpiry?: unknown | undefined, recurringFrequency?: string | undefined, recurringAmount?: Amount | undefined, threeDSRequestorAuthenticationInd?: string | undefined, threeDSRequestorDecMaxTime?: unknown | undefined, threeDSRequestorDecReqInd?: unknown | undefined, threeRIInd?: string | undefined, transactionDate?: unknown | undefined, ua?: string | undefined}; export type ClientRequestBody = {cardHolderId: unknown, dynamicLinking: DynamicLinking, principal: {type: 'pan' | 'encryptedPan' | 'cardId' | 'tokenPan', value: string}, expiry?: ({type: 'plain' | 'encrypted', value: string}) | undefined, credentials?: ({type: 'plain' | 'encrypted', value: string}) | undefined, freeContext?: {[key: string]: string} | undefined}; export type Signature = {algorithm?: string | undefined, keyInfo?: string | undefined, value?: string | undefined}; export type Footer = {signature?: Signature | undefined}; export type RootSignature = string; export type MessageClientRequestBody = {header: Header, body: ClientRequestBody, footer?: Footer | undefined, signature?: RootSignature | undefined}; export type CreateApiUpdateCardCredentialsArgs = {Authorization?: string | undefined, body: MessageClientRequestBody}; export type CreateApiUpdateCardCredentialsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const header = z.object({requestId: z.string(), service: z.string().min(1).max(255), issuerCode: z.string().min(5).max(5), subIssuerCode: z.string().min(5).max(5), keyTag: z.string().min(2).max(2).optional(), iv: z.union([z.string().min(32).max(32), z.string().min(24).max(24)]).optional()}); export const clientResponseBody = z.object({availableMeans: z.array(z.string()).optional(), cardData: z.string().optional(), cardId: z.string().min(2).max(36).optional(), cardholderId: z.string().min(8).max(36).optional(), credentials: z.object({type: z.string(), value: z.string().min(1)}).optional(), deletedTime: z.string().optional(), label: z.string().optional(), language: z.string().min(2).max(2).optional(), freeContext: z.record(z.string(), z.string()).optional()}); export const signature = z.object({algorithm: z.string().optional(), keyInfo: z.string().optional(), value: z.string().optional()}); export const footer = z.object({signature: signature.optional()}); export const rootSignature = z.string(); export const messageUpdateCardResponse = z.object({header: header, body: clientResponseBody, footer: footer.optional(), signature: rootSignature.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 }; /** * Update a card with its credentials. In case of error, no retry will be triggered on client side. * * POST /updateCardCredentials */ export const createApiUpdateCardCredentials = async (args: CreateApiUpdateCardCredentialsArgs, options: CreateApiUpdateCardCredentialsOptions) => { 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.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/updateCardCredentials${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(messageUpdateCardResponse, await res.json()) };