import {z} from 'zod' export type W8TaxTreatyBenefits = {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}; export type W8BENFormRequest = {citizen_of_country: string, us_tin_type?: ('SSN' | 'ITIN') | undefined, us_tin?: string | undefined, foreign_tin?: string | undefined, is_ftin_not_legally_required?: boolean | undefined, reference_number?: string | undefined, tax_treaty_benefits?: W8TaxTreatyBenefits | undefined, signature_name: string}; export type CreateApiPayoutUserTokenTaxInterviewW8benArgs = {body: W8BENFormRequest}; export type CreateApiPayoutUserTokenTaxInterviewW8benOptions = {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 tax form * * POST /payout/{user_token}/tax/interview/w8ben * * Also accepts auth: api_key */ export const createApiPayoutUserTokenTaxInterviewW8ben = async (args: CreateApiPayoutUserTokenTaxInterviewW8benArgs, options?: CreateApiPayoutUserTokenTaxInterviewW8benOptions) => { 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/w8ben${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()) };