import {z} from 'zod' export type CustomerBID = string; export type GetCustomerRequest = {customerBID?: CustomerBID | undefined}; export type CreateApiV1Click2payVISACustomerCustomerIdArgs = {customerId: unknown, auditUser: string, body: GetCustomerRequest}; export type CreateApiV1Click2payVISACustomerCustomerIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const paymentInstrumentResponse = z.object({tokenDetails: z.object({tokenReferenceID: z.string().min(1).max(50), tokenRequestorID: z.string().min(1).max(11)}).optional(), cardId: z.string().optional(), status: z.enum(["ACTIVE", "DISABLED"]).optional(), nameOnCard: z.string().min(1).max(120).optional(), billingAddress: z.object({city: z.string().min(1).max(32).optional(), country: z.string().min(3).max(3), postalCode: z.string().min(1).max(9).optional(), addressLine1: z.string().min(1).max(64).optional(), addressLine2: z.string().min(1).max(64).optional(), addressLine3: z.string().min(1).max(64).optional()}).optional()}); export const getCustomerResponse = z.object({customerInformation: z.object({email: z.array(z.string().max(256)).optional(), locale: z.string().optional(), phone: z.array(z.string().min(1).max(16)).optional(), lastName: z.string().optional(), firstName: z.string().optional(), middleName: z.string().optional(), countryCode: z.string().optional(), status: z.enum(["ACTIVE", "DISABLED"]).optional()}).optional(), paymentInstruments: z.array(paymentInstrumentResponse).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 }; /** * Get Cardholder by Cardholder ID from C2P * * POST /v1/click2pay/VISA/customer/{customerId} */ export const createApiV1Click2payVISACustomerCustomerId = async (args: CreateApiV1Click2payVISACustomerCustomerIdArgs, options: CreateApiV1Click2payVISACustomerCustomerIdOptions) => { 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/click2pay/VISA/customer/${encodeURIComponent(String(args.customerId))}${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(getCustomerResponse, await res.json()) };