import {z} from 'zod' export type EmailAddress = string; export type BillingEntityCode = 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 PaymentMethodToken = string; export type TaxCodes = Array; export type CustomerMetadata = {key: string, value: string, display_in_invoice: boolean}; export type CustomerUpdateRequest = {name?: string | undefined, email?: EmailAddress | undefined, billing_entity_code?: BillingEntityCode | undefined, address?: Address | undefined, phone?: unknown | undefined, payment_method_token?: PaymentMethodToken | undefined, tax_codes?: TaxCodes | undefined, metadata?: Array | undefined}; export type UpdateApiCustomersExternalCustomerIdArgs = {external_customer_id: string, body: CustomerUpdateRequest}; export type UpdateApiCustomersExternalCustomerIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const emailAddress = z.string().min(3).max(254); export const billingEntityCode = z.string(); 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 paymentMethodToken = z.string().min(1).max(36); export const paymentMethodType = z.enum(["CARD", "PAYPAL", "ACH_DEBIT", "APPLE_PAY"]); 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 customerMetadata = z.object({key: z.string().max(20), value: z.string().max(100), display_in_invoice: z.boolean()}); export const customerWithId = z.object({name: z.string().min(1).max(512), external_id: z.string().min(1).max(64), email: emailAddress, billing_entity_code: billingEntityCode.optional(), address: address.optional(), phone: z.string().optional(), payment_method_token: paymentMethodToken.optional(), payment_method_type: paymentMethodType.optional(), taxes: z.array(taxResponse).optional(), metadata: z.array(customerMetadata).optional(), id: 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 }; /** * Update a customer by ID * * PUT /customers/{external_customer_id} */ export const updateApiCustomersExternalCustomerId = async (args: UpdateApiCustomersExternalCustomerIdArgs, options?: UpdateApiCustomersExternalCustomerIdOptions) => { 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(/\/+$/, '')}/customers/${encodeURIComponent(String(args.external_customer_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(customerWithId, await res.json()) };