import {z} from 'zod' export type GetApiCustomersPublicCustomerIdPaymentMethodsArgs = {customerId: string | null, version: string | null, authorization: string | null}; export type GetApiCustomersPublicCustomerIdPaymentMethodsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cardScheme = z.string(); export const cardDetails = z.object({pan: z.string().optional(), expiryDate: z.string().optional(), scheme: cardScheme.optional()}); export const customerPaymentMethod = z.object({id: z.string().nullable().optional(), cardDetails: cardDetails.nullable().optional()}); export const walletPaymentOption = z.string(); export const supportedPaymentMethods = z.object({cardSchemes: z.array(cardScheme).nullable().optional(), wallets: z.array(walletPaymentOption).nullable().optional()}); export const customerPaymentMethods = z.object({customerId: z.string().nullable().optional(), merchantId: z.string().nullable().optional(), savedPaymentMethods: z.array(customerPaymentMethod).nullable().optional(), supportedPaymentMethods: supportedPaymentMethods.nullable().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 }; /** * Retrieve payment method * * GET /customers/public/{customerId}/payment-methods */ export const getApiCustomersPublicCustomerIdPaymentMethods = async (args: GetApiCustomersPublicCustomerIdPaymentMethodsArgs, options?: GetApiCustomersPublicCustomerIdPaymentMethodsOptions) => { const {baseUrl = "https://api.dojo.tech", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.version != null) headers.set("version", String(args.version)) if (args.authorization != null) headers.set("authorization", String(args.authorization)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/customers/public/${encodeURIComponent(String(args.customerId))}/payment-methods${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(customerPaymentMethods, await res.json()) };