import {z} from 'zod' export type Form1099KMonthlyAmounts = {january?: number | undefined, february?: number | undefined, march?: number | undefined, april?: number | undefined, may?: number | undefined, june?: number | undefined, july?: number | undefined, august?: number | undefined, september?: number | undefined, october?: number | undefined, november?: number | undefined, december?: number | undefined}; export type Form1099KPSEDetails = {pse_name?: string | undefined, pse_phone?: string | undefined}; export type Form1099KStateFiling = {state_code?: string | undefined, state_id_number?: string | undefined, state_tax_withheld?: number | 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 Form1099KRequest = {tax_year: '2023' | '2024' | '2025', supplemental_gross_amount?: number | undefined, gross_amount?: number | undefined, merchant_category_code: string, number_of_payment_transactions: number, federal_tax_withheld?: number | undefined, monthly_amounts?: Form1099KMonthlyAmounts | undefined, account_number?: string | undefined, filer_indicator?: ('PSE' | 'EPF') | undefined, transactions_reported_type: 'Payment_Card' | 'Third_Party_Network', pse_details?: Form1099KPSEDetails | undefined, state_filing?: Array | undefined, is_postal?: boolean | undefined, email_address?: string | undefined, address?: ForeignAddress | undefined}; export type CreateApiPayoutUserTokenTaxForm1099kArgs = {body: Form1099KRequest}; export type CreateApiPayoutUserTokenTaxForm1099kOptions = {baseUrl?: string | undefined, AUTHORIZERNAME?: string | undefined, fetch?: typeof fetch | undefined}; export const form1099KReturnStatus = z.object({status: z.string().optional(), status_timestamp: z.string().optional(), info: z.string().optional()}); export const form1099KStateReturnStatus = z.object({state_code: z.string().optional(), status: z.string().optional(), status_timestamp: z.string().optional(), info: z.string().optional()}); export const form1099KSubmissionResponse = z.object({submission_id: z.string().optional(), form_type: z.literal("1099-K").optional(), tax_year: z.string().optional(), status: z.enum(["Created", "Pending", "Validated", "Error"]).optional(), federal_return: form1099KReturnStatus.optional(), state_returns: z.array(form1099KStateReturnStatus).optional(), submitted_at: z.string().optional(), pdf_content: z.string().optional(), gross_amount: 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 Form 1099-K * * POST /payout/{user_token}/tax/form1099k * * Also accepts auth: api_key */ export const createApiPayoutUserTokenTaxForm1099k = async (args: CreateApiPayoutUserTokenTaxForm1099kArgs, options?: CreateApiPayoutUserTokenTaxForm1099kOptions) => { 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/form1099k${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(form1099KSubmissionResponse, await res.json()) };