import {z} from 'zod' export type Address = {line1: string, line2?: (string | null) | undefined, city: string, state: string, postal_code: string, country: string}; export type BeneficialOwner = {first_name: string, last_name: string, date_of_birth: string, address: Address, ssn_last_4: string, ownership_percentage?: number | undefined}; export type AuthorizedSigner = {first_name?: string | undefined, last_name?: string | undefined, email?: string | undefined, title?: string | undefined, phone?: string | undefined}; export type InlineDocumentDownloadRequest = {filename: string, filetype: string, download_url: string}; export type CreateMerchantRequest = {reference_id?: (string | null) | undefined, legal_business_name: string, business_address: Address, business_email: string, business_phone: string, entity_type: 'corporation' | 'llc' | 'sole_proprietorship' | 'partnership' | 'non_profit', formation_state: string, ein: string, dba_name?: string | undefined, annual_revenue?: number | undefined, years_in_business?: number | undefined, industry?: string | undefined, business_description?: string | undefined, website?: string | undefined, estimated_monthly_net_terms_volume?: number | undefined, existing_net_terms_info?: {[key: string]: unknown} | undefined, beneficial_owners?: Array | undefined, authorized_signer?: AuthorizedSigner | undefined, plaid_access_token?: string | undefined, bank_account_number?: string | undefined, bank_routing_number?: string | undefined, metadata?: {[key: string]: unknown} | undefined, bank_statements?: Array | undefined, financial_statements?: Array | undefined, credit_references?: Array | undefined, personal_guarantee?: Array | undefined, other_documents?: Array | undefined}; export type CreateApiMerchantsArgs = {body: CreateMerchantRequest}; export type CreateApiMerchantsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({line1: z.string(), line2: z.string().nullable().optional(), city: z.string(), state: z.string(), postal_code: z.string(), country: z.string()}); export const documentIngestionResult = z.object({document_id: z.string().nullable().optional(), document_type: z.enum(["bank_statements", "financial_statements", "credit_references", "personal_guarantee", "other"]).optional(), filename: z.string().optional(), download_url: z.string().optional(), status: z.enum(["queued", "failed"]).optional(), error_code: z.string().nullable().optional(), error_message: z.string().nullable().optional()}); export const documentIngestionSummary = z.object({requested_count: z.number().int().optional(), queued_count: z.number().int().optional(), failed_count: z.number().int().optional(), results: z.array(documentIngestionResult).optional()}); export const merchant = z.object({id: z.string().optional(), reference_id: z.string().nullable().optional(), legal_business_name: z.string().optional(), dba_name: z.string().nullable().optional(), business_address: address.optional(), business_email: z.string().optional(), business_phone: z.string().optional(), entity_type: z.enum(["corporation", "llc", "sole_proprietorship", "partnership", "non_profit"]).optional(), formation_state: z.string().optional(), industry: z.string().nullable().optional(), website: z.string().nullable().optional(), underwriting_status: z.string().optional(), seller_amount_approved: z.number().int().nullable().optional(), active_subscription: z.object({id: z.string().optional(), tier: z.string().optional()}).nullable().optional(), metadata: z.record(z.string(), z.unknown()).optional(), document_ingestion: documentIngestionSummary.optional(), created_at: z.string().optional(), updated_at: z.string().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 Merchant * * POST /merchants * * Also accepts auth: basicAuth */ export const createApiMerchants = async (args: CreateApiMerchantsArgs, options?: CreateApiMerchantsOptions) => { const {baseUrl = "https://app-sandbox.resolvepay.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/merchants${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(merchant, await res.json()) };