import {z} from 'zod' export type W8BENETaxTreatyBenefits = {is_claiming_treaty_benefits?: boolean | undefined, beneficiary_country?: string | undefined, claiming_article_paragraph?: string | undefined, rate_of_withholding?: string | undefined, type_of_income?: ('Paid by US Obligors—General' | 'Paid by US Corporations' | 'Qualifying for Direct Dividend Rate' | 'Pensions and Annuities' | 'Social Security' | 'Industrial Equipment' | 'Know-How/ Other Industrial Royalties' | 'Patents' | 'Film & TV' | 'Copyrights' | 'Others') | undefined, additional_conditions?: string | undefined, limitation_on_benefits?: string | undefined, limitation_on_benefits_other_detail?: string | undefined}; export type ForeignAddress = {address1?: string | undefined, address2?: string | undefined, city?: string | undefined, state?: string | undefined, country?: string | undefined, postal_code?: string | undefined}; export type DisregardedEntityBranch = {chapter_4_status?: string | undefined, address?: ForeignAddress | undefined, giin?: string | undefined}; export type W8BENEChapter4Certification = {sponsoring_entity_name?: string | undefined, sponsored_ffi_type?: ('INVESTMENT_ENTITY' | 'CONTROLLED_FOREIGN_CORPORATION') | undefined, certification_line?: string | undefined, iga_country?: string | undefined, iga_model?: ('MODEL1IGA' | 'MODEL2IGA') | undefined, iga_treated_as?: string | undefined, trustee_or_sponsor_name?: string | undefined, is_trustee_us?: boolean | undefined, formed_on?: string | undefined, bankruptcy_date?: string | undefined, irs_determination_letter_date?: string | undefined, securities_market_name?: string | undefined, affiliate_entity_name?: string | undefined, affiliate_securities_market_name?: string | undefined}; export type SubstantialUSOwner = {name?: string | undefined, address?: string | undefined, tin_type?: ('SSN' | 'EIN') | undefined, tin?: string | undefined}; export type W8Certification = {has_capacity_to_sign: boolean, signer_name: string}; export type W8BENEFormRequest = {organization_name: string, country_of_organization: string, disregarded_entity_name?: string | undefined, chapter_3_status: 'SIMPLE TRUST' | 'CENTRAL BANK OF ISSUE' | 'GRANTOR TRUST' | 'TAX EXEMPT ORGANIZATION' | 'PRIVATE FOUNDATION' | 'DISREGARDED ENTITY' | 'CORPORATION' | 'COMPLEX TRUST' | 'ESTATE' | 'INTERNATIONAL ORGANIZATION' | 'PARTNERSHIP' | 'FOREIGN CONTROLLED ENTITY' | 'FOREIGN INTERGRAL PART', chapter_4_status: 'NONPARTICIPATING FFI' | 'PARTICIPATING FFI' | 'REPORTING MODEL1 FFI' | 'REPORTING MODEL2 FFI' | 'REGISTERED FFI' | 'SPONSORED FFI' | 'NONREGISTERING LOCALBANK' | 'FFI LOWVALUE ACCOUNT' | 'SPONSORED INVESTMENT VEHICLE' | 'LIMITED LIFEDEBT INVESTMENT' | 'NO FINANCIAL ACCOUNT' | 'OWNER DOCUMENTED FFI' | 'RESTRICTED DISTRIBUTOR' | 'NONREPORTING IGA FFI' | 'FOREIGN GOVERNMENT' | 'INTERNATIONAL ORGANIZATION' | 'EXEMPT RETIREMENT PLANS' | 'EXEMPT BENEFICIAL OWNERS' | 'TERRITORY FINANCIAL INSTITUTION' | 'EXCEPTED NONFINANCIAL GROUP' | 'EXCEPTED NONFINANCIAL STARTUP' | 'EXCEPTED NONFINANCIAL LIQUIDATION BANKRUPTCY' | '501C ORGANIZATION' | 'NONPROFIT ORGANIZATION' | 'PUBLICLY TRADED NFFE' | 'EXCEPTED TERRITORY NFFE' | 'ACTIVE NFFE' | 'PASSIVE NFFE' | 'EXCEPTED INTER AFFILIATE FFI' | 'DIRECT REPORTING NFFE' | 'SPONSORED DIRECT REPORTING FFI' | 'NON FINANCIAL ACCOUNT', us_tin_type?: ('SSN' | 'ITIN') | undefined, us_tin?: string | undefined, giin?: string | undefined, foreign_tin?: string | undefined, is_ftin_not_legally_required?: boolean | undefined, reference_number?: string | undefined, is_hybrid_entity_treaty_claim?: boolean | undefined, tax_treaty_benefits?: W8BENETaxTreatyBenefits | undefined, limitation_on_benefits?: ('GOVERNMENT' | 'TAX-EXEMPT PENSION TRUST OR PENSION FUND' | 'OTHER TAX-EXEMPT ORGANIZATION' | 'PUBLICLY TRADED CORPORATION' | 'SUBSIDIARY OF A PUBLICLY TRADED CORPORATION' | 'COMPANY THAT MEETS THE OWNERSHIP AND BASE EROSION TEST' | 'COMPANY THAT MEETS THE DERIVATIVE BENEFITS TEST' | 'COMPANY WITH AN ITEM OF INCOME THAT MEETS ACTIVE TRADE OR BUSINESS TEST' | 'FAVORABLE DISCRETIONARY DETERMINATION BY THE U.S. COMPETENT AUTHORITY RECEIVED' | 'NO LOB ARTICLE IN TREATY' | 'OTHER') | undefined, limitation_on_benefits_other_detail?: string | undefined, disregarded_entity_branch?: DisregardedEntityBranch | undefined, chapter_4_certification?: W8BENEChapter4Certification | undefined, substantial_us_owners?: Array | undefined, certification: W8Certification}; export type CreateApiPayoutUserTokenTaxInterviewW8beneArgs = {body: W8BENEFormRequest}; export type CreateApiPayoutUserTokenTaxInterviewW8beneOptions = {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-8BEN-E tax form * * POST /payout/{user_token}/tax/interview/w8bene * * Also accepts auth: api_key */ export const createApiPayoutUserTokenTaxInterviewW8bene = async (args: CreateApiPayoutUserTokenTaxInterviewW8beneArgs, options?: CreateApiPayoutUserTokenTaxInterviewW8beneOptions) => { 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/w8bene${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()) };