import {z} from 'zod' export type W9FormRequest = {tin_type: 'SSN' | 'EIN' | 'ITIN', tin: string, federal_tax_classification: 'Individual' | 'Sole Proprietor' | 'Single Member LLC Not Elected' | 'Single Member LLC Elected' | 'C Corporation' | 'S Corporation' | 'Partnership' | 'Trust or Estate' | 'LLC C Corporation' | 'LLC S Corporation' | 'LLC Partnership' | 'Others', business_name?: string | undefined, is_backup_withholding?: boolean | undefined, exempt_payee_code?: ('1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '10' | '11' | '12' | '13') | undefined, exempt_fatca_code?: ('A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M') | undefined, account_numbers?: string | undefined, is_line_3b?: boolean | undefined, other_description?: string | undefined}; export type CreateApiPayoutUserTokenTaxInterviewW9Args = {body: W9FormRequest}; export type CreateApiPayoutUserTokenTaxInterviewW9Options = {baseUrl?: string | undefined, AUTHORIZERNAME?: string | undefined, fetch?: typeof fetch | undefined}; export const taxFormError = z.object({id: z.string().optional(), name: z.string().optional(), message: z.string().optional()}); export const taxFormSubmissionResponse = z.object({submission_id: z.string().optional(), form_type: z.enum(["W9", "W8BEN", "W8BENE", "W8ECI", "W8IMY"]).optional(), status: z.enum(["Submitted", "Pending", "Validated", "Error"]).optional(), submitted_at: z.string().optional(), errors: z.array(taxFormError).optional(), pdf_content: 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 }; /** * Submit W-9 tax form * * POST /payout/{user_token}/tax/interview/w9 * * Also accepts auth: api_key */ export const createApiPayoutUserTokenTaxInterviewW9 = async (args: CreateApiPayoutUserTokenTaxInterviewW9Args, options?: CreateApiPayoutUserTokenTaxInterviewW9Options) => { const {baseUrl = "https://api.masspay.io/v1.0.0", fetch: fetchFn = fetch, AUTHORIZERNAME} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AUTHORIZERNAME !== undefined) headers.set("Authorization", AUTHORIZERNAME) const url = `${baseUrl.replace(/\/+$/, '')}/payout/${encodeURIComponent(String(args.user_token))}/tax/interview/w9${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(taxFormSubmissionResponse, await res.json()) };