import {z} from 'zod' export type BillingAddress = {street1: string, street2?: (string | null) | undefined, city: string, country: string, state: string, zip: string, phone?: (string | null) | undefined, salutation?: (string | null) | undefined, companyName?: (string | null) | undefined, fax?: (string | null) | undefined, faxExtension?: (string | null) | undefined, poBox?: (string | null) | undefined}; export type PaymentMethodTokenRequest = {paymentMethodType: string, properties: {[key: string]: string}, billingAddress: BillingAddress}; export type CreateApiAppMarketV2PaymentMethodTokensArgs = {'AD-Public-Developer-Key': string, body: PaymentMethodTokenRequest}; export type CreateApiAppMarketV2PaymentMethodTokensOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const billingAddress = z.object({street1: z.string(), street2: z.string().nullable().optional(), city: z.string(), country: z.string(), state: z.string(), zip: z.string(), phone: z.string().nullable().optional(), salutation: z.string().nullable().optional(), companyName: z.string().nullable().optional(), fax: z.string().nullable().optional(), faxExtension: z.string().nullable().optional(), poBox: z.string().nullable().optional()}); export const paymentMethodForTokenization = z.object({paymentMethodType: z.string(), properties: z.record(z.string(), z.string()), billingAddress: billingAddress, accountDisplay: z.string().max(255), default: z.boolean().optional()}); export const paymentMethodTokenResponse = z.object({id: z.string().max(255), expiration: z.string(), paymentMethod: paymentMethodForTokenization}); 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 }; /** * Tokenize payment methods * * POST /appMarket/v2/paymentMethodTokens */ export const createApiAppMarketV2PaymentMethodTokens = async (args: CreateApiAppMarketV2PaymentMethodTokensArgs, options?: CreateApiAppMarketV2PaymentMethodTokensOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", 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['AD-Public-Developer-Key'] != null) headers.set("AD-Public-Developer-Key", String(args['AD-Public-Developer-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/appMarket/v2/paymentMethodTokens${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(paymentMethodTokenResponse, await res.json()) };