import {z} from 'zod' export type Currency = string; export type Address = {line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postal_code?: string | undefined, country?: string | undefined}; export type Timezone = string; export type BillingEntityCode = string; export type BillingEntityCreateRequest = {name: string, default_currency?: Currency | undefined, finalize_zero_amount_invoice?: boolean | undefined, address?: Address | undefined, legal_name?: (string | null) | undefined, legal_number?: (string | null) | undefined, email?: (string | null) | undefined, timezone?: Timezone | undefined, code: BillingEntityCode}; export type CreateApiBillingEntitiesArgs = {body: BillingEntityCreateRequest}; export type CreateApiBillingEntitiesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.string().min(3).max(3); export const address = z.object({line1: z.string().min(1).max(254).optional(), line2: z.string().min(1).max(512).optional(), city: z.string().min(1).max(512).optional(), state: z.string().min(1).max(512).optional(), postal_code: z.string().min(1).max(512).optional(), country: z.string().min(2).max(2).optional()}); export const timezone = z.string(); export const billingEntityCode = z.string(); export const taxCode = z.string(); export const taxResponse = z.object({name: z.string(), rate: z.string().optional(), description: z.string().optional(), code: taxCode.optional(), id: z.string().optional(), created_at: z.string().optional()}); export const billingEntityResponse = z.object({name: z.string(), default_currency: currency.optional(), finalize_zero_amount_invoice: z.boolean().optional(), address: address.optional(), legal_name: z.string().nullable().optional(), legal_number: z.string().nullable().optional(), email: z.string().nullable().optional(), timezone: timezone.optional(), code: billingEntityCode.optional(), is_default: z.boolean().optional(), id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), taxes: z.array(taxResponse).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 }; /** * Create a billing entity * * POST /billing-entities */ export const createApiBillingEntities = async (args: CreateApiBillingEntitiesArgs, options?: CreateApiBillingEntitiesOptions) => { const {baseUrl = "https://api-m.sandbox.paypal.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/billing-entities${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(billingEntityResponse, await res.json()) };