import {z} from 'zod' export type USAddress = {address1: string, address2?: string | undefined, city: string, state: string, zip_code: string}; export type W8ECIFormRequest = {trade_name?: string | undefined, organization_country: string, entity_type: 'PARTNERSHIP' | 'FOREIGN GOVT-CONTROLLED ENTITY' | 'FOREIGN GOVT-INTERGRAL PART' | 'PRIVATE FOUNDATION' | 'SIMPLE TRUST' | 'GRANTOR TRUST' | 'INTERNATIONAL ORGANIZATION' | 'INDIVIDUAL' | 'COMPLEX TRUST' | 'CENTRAL BANK OF ISSUE' | 'CORPORATION' | 'ESTATE' | 'TAX-EXEMPT ORGANIZATION', tin_type: 'EIN' | 'SSN' | 'ITIN', tin: string, foreign_tin?: string | undefined, is_ftin_not_legally_required?: boolean | undefined, us_business_address: USAddress, reference_number?: string | undefined, item_of_income: string, is_dealer_securities_or_ptp?: boolean | undefined, signature_name: string}; export type CreateApiPayoutUserTokenTaxInterviewW8eciArgs = {body: W8ECIFormRequest}; export type CreateApiPayoutUserTokenTaxInterviewW8eciOptions = {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-8ECI tax form * * POST /payout/{user_token}/tax/interview/w8eci * * Also accepts auth: api_key */ export const createApiPayoutUserTokenTaxInterviewW8eci = async (args: CreateApiPayoutUserTokenTaxInterviewW8eciArgs, options?: CreateApiPayoutUserTokenTaxInterviewW8eciOptions) => { 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/w8eci${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()) };